Matplotlib-Python

Source: Deep Learning on Medium


Go to the profile of Develop It

#Python’s Most Popular 2D plotting library.
 #Produce dozens of different types of plots and charts with just a few lines of code.
 #Easy to plot NumPy arrays, Pandas data frame, and Python lists.

#PyPlot module provides a MATLAB-like interface.
 #Gives you full control of line styles, font properties, axes properties etc.
 #Create a Blank Chart, then add one element at a time to it.
 #(title, axis, curve, bars, annotation, etc)

#On MatPlotLib website there are many a lot charts along with sample Code.
 #matplotlib.org

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt

#1 Simple Plot.
plt.plot([2, 8, 3, 7, 1, 0, 9, 2, 3, 5, 8])
plt.show()

#2 Points having x and y values, add title and axis labels.
plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 8, 27, 64, 125, 216, 343, 512, 729])
plt.title(‘Cubic Plot’, fontsize = 12, color = ‘b’)
plt.xlabel(‘Values’)
plt.ylabel(‘Cubic Values’)
plt.show()

#3 Change Figure size, plot blue dots, set x and y axis scales to 0–5 and 0–150 respectively.
plt.figure(figsize = (3, 6)) #3 inches by 6 inches
plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 8, 27, 64, 125, 216, 343, 512, 729], ‘kx’) #kx — (black — x)
plt.axis([0, 10, 0, 800]) #[xmin, xmax, ymin, ymax]
plt.annotate(‘cube it’, (7, 100)) #(7, 100) is the location where you want the annotation to be.
plt.show()

#4 Bar Charts
plt.clf() #clear figure
x = np.arange(9)
y = [9.3, 2.5, 8.1, 4.5, 7.6, 1.9, 0.1, 2.5, 13]
plt.xticks(x, [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’])
plt.bar(x, y, color = [‘r’, ‘b’, ‘g’, ‘m’, ‘k’, ‘c’, ‘lime’, ‘tan’, ‘m’])
plt.show()

#5 Three sets of 20 random dots
d = {‘Red X’: np.random.rand(20),
 ‘Blue O’: np.random.rand(20),
 ‘Green -’: np.random.rand(20),
 ‘Magenta *’: np.random.rand(20)} #We have created a dictionary.
df = pd.DataFrame(d)
df.plot(style = [‘bo’, ‘g-’, ‘m*’, ‘rx’]) #dataframes are able to call the matplotlib library directly.
plt.show()

#6 Time Series — One Year of Random Floats.
time_series = pd.Series(np.random.randn(360), index = pd.date_range(‘1/1/2018’, periods = 360))
df = pd.DataFrame(np.random.randn(360, 4), index = time_series.index, columns = list(‘ABCD’))
df.cumsum().plot() #cumsum — cumulative sum
plt.show()

#7 Random Dots in a Scatter Plot
N = 100
x = np.random.rand(N) #values between 0 and 1 which are floating point.
y = np.random.rand(N)
colors = np.random.rand(N)
sizes = (20 * np.random.rand(N))**2
plt.scatter(x, y, s = sizes, c = colors, alpha = 0.7)
plt.show()

#8 Load CSV file and show multiple chart types
df = pd.read_csv(‘Weather_Monthly.txt’)
print(df)
plt.bar(df[‘month’], df[‘record_high’], color = ‘r’)
plt.bar(df[‘month’], df[‘record_low’], color = ‘b’)
#x = np.arange(12)
#plt.xticks(x, [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’])
 #This changes the axis labels for ticks but the values are pointed as they were plotted without this.
 #So we are not using this here. 
plt.plot(df[‘month’], df[‘avg_high’], color = ‘g’)
plt.plot(df[‘month’], df[‘avg_low’], color = ‘m’)
plt.figlegend() #This shows a legend outside the plot area
plt.show()

#9.1 Subplots
fig = plt.figure()
fig.suptitle(‘Subplots’)
fig.add_subplot(221)
plt.plot([np.exp(n) for n in range(1, 20)])
fig.add_subplot(222, facecolor =’r’)
fig.add_subplot(223)
plt.plot([np.cos(n) for n in range(20)])
fig.add_subplot(224)
plt.plot([np.sin(n) for n in range(20)])
plt.show()

#9.2
fig, plots = plt.subplots(2, sharex = True)
fig.suptitle(‘Plots sharing X-axis’)
x = range(0, 100, 10)
y = [np.log(n) for n in x]
plots[0].plot(x, y, color = ‘b’)
plots[1].scatter(x, y, color = ‘g’)
plt.show()

#10 Save Figure to an Image File
plt.figure(figsize = (16, 9), dpi = 50)
plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 8, 27, 64, 125, 216, 343, 512, 729])
plt.title(‘Cubic Plot’, fontsize = 12, color = ‘b’)
plt.xlabel(‘Values’)
plt.ylabel(‘Cubic Values’)
plt.savefig(‘Cubic_Plot.png’)
plt.show()