Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+.
Some of the major Pros of Matplotlib are:
Install it using this command:(write the command on the command python command prompt/ ANACONDA prompt)
pip install matplotlib
or
conda install matplotlib
Once Matplotlib is installed, import it in your applications by adding the __import__
keyword:
The version string is stored under __version__
attribute.
import matplotlib as mat
print(mat.__version__)
3.5.2
Most of the Matplotlib utilities lies under the pyplot
submodule, and are usually imported under the plt
alias:
import matplotlib.pyplot as plt
Example: Draw a line in a diagram from position (0,0) to position (6,250):
import numpy as np
xpoints = np.array([0, 6]) ## Data
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints) ## Plotting Function
plt.show() ## To show the plot
Note that, To show a plot, plot.show()
function is used. But, there is an alternative :
plt.show()
is our friend. plt.show()
starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures.%matplotlib
magic command after starting Notebook. At this point, any plt plot command will cause a figure window to open, and further commands can be run to update the plot. plt.show()
in Matplotlib mode is not required.To save a figure in PNG, or JPEG we use the function savefig()
xpoints = np.array([0, 6]) ## Data
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints) ## Plotting Function
plt.savefig("Test.jpeg")
x= np.arange(start=0,stop=10) ## np.arange() => Return evenly spaced values within a given interval
print("x:",x,"\n")
y= np.arange(start=11,stop=21)
print("y:",y,"\n")
x: [0 1 2 3 4 5 6 7 8 9] y: [11 12 13 14 15 16 17 18 19 20]
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x1fb5bbadc10>
plt.scatter(x,y,s=3) # s=> shape parameter
<matplotlib.collections.PathCollection at 0x1fb5b82b430>
plt.scatter(x,y,c='r') # c=> color parameter
<matplotlib.collections.PathCollection at 0x1fb5b8d57c0>
plt.scatter(x,y,c="black",s=6)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("My Scatter Plot")
Text(0.5, 1.0, 'My Scatter Plot')
plt.annotate(<label>, <coords>)
using the value itself as label:
plt.scatter(x,y,c="r",s=6)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("My Scatter Plot")
## Creating Labels:
labs=[]
for i in range(len(x)):
labs.append('('+str(x[i])+','+str(y[i])+')') ## Levels of the points
plt.annotate(labs[i], # text
(x[i],y[i]), # coordinates to position the label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y),
ha='center') # horizontal alignment can be left, right or center
plt.scatter(x,y,c="black",s=6)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.xticks(ticks=[]) ## Removing X-axis ticks/labels
plt.title("My Scatter Plot")
Text(0.5, 1.0, 'My Scatter Plot')
plt.scatter(x,y,c="black",s=6)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.yticks(ticks=[]) ## Removing Y-axis ticks/labels
plt.title("My Scatter Plot")
Text(0.5, 1.0, 'My Scatter Plot')
## 1st Plot:
plt.subplot(1,2,1) ## Creating ploting region. 1st plot of the (1x2) ploting region
plt.scatter(x,y)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("Without point labels")
# 2nd Plot:
plt.subplot(1,2,2) ## Creating ploting region. 1st plot of the (1x2) ploting region
plt.scatter(x,y,c="r",s=6)
plt.xlabel("X axis")
plt.title("With point labels")
labs=[]
for i in range(len(x)):
labs.append('('+str(x[i])+','+str(y[i])+')')
plt.annotate(labs[i],(x[i],y[i]),textcoords="offset points",xytext=(0,6),ha='center',fontsize=6)
## Adjustment of subplot layout
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
x= np.linspace(0,20,1000)
y1= np.sin(x)
y2= np.cos(x)
plt.subplot(2,1,1)
plt.plot(x,y1)
plt.xticks(ticks=[]) ## Remove the x-axis ticks and labels.
plt.title("sin(x)")
#Plotting 2nd line
plt.subplot(2,1,2)
plt.plot(x,y2)
plt.title("cos(x)")
Text(0.5, 1.0, 'cos(x)')
plt.plot(x,y1,'g',linestyle="dashed",linewidth=2) ## alternatively, linestyle="--"
## See the help for more details
[<matplotlib.lines.Line2D at 0x1fb5d00af70>]
plt.plot(x,y1,'-g',label='sin(x)')
plt.plot(x,y2,'--r',label='cos(x)')
[<matplotlib.lines.Line2D at 0x1fb636e4e20>]
plt.plot(x,y1,'-g',label='sin(x)')
plt.plot(x,y2,'--r',label='cos(x)')
plt.axis('equal')
plt.legend()
<matplotlib.legend.Legend at 0x1fb6240ba90>
plt.plot(x,y1)
plt.title("sin(x)")
plt.xlabel("x")
plt.xlim(0,13) ## Changing the x limit from [0,20]-> [0,13]
(0.0, 13.0)
## Creating Data
import pandas as pd
Country_data= pd.DataFrame(np.random.choice(["India","Australia","USA","UK","Germany","China","Russia"],size=100,replace=True))
Country_data= pd.DataFrame({'Country':list(set(Country_data[0])),'Freq':list(Country_data.value_counts())},index=None)
Country_data
Country | Freq | |
---|---|---|
0 | USA | 20 |
1 | Russia | 17 |
2 | Australia | 16 |
3 | Germany | 16 |
4 | India | 13 |
5 | UK | 10 |
6 | China | 8 |
plt.bar(Country_data['Country'],Country_data['Freq'])
plt.xlabel("Country Name")
plt.ylabel("Frequency")
plt.title("Country Data")
Text(0.5, 1.0, 'Country Data')
plt.bar(x=Country_data['Country'],height= Country_data['Freq'],width=0.4,color ='orange')
plt.xlabel("Country Name")
plt.ylabel("Frequency")
plt.title("Country Data")
Text(0.5, 1.0, 'Country Data')
plt.barh(y=Country_data['Country'],width= Country_data['Freq'],height=0.4,color ='orange')
plt.xlabel("Frequency")
plt.ylabel("Country Name")
plt.title("Country Data")
Text(0.5, 1.0, 'Country Data')
## 1st Plot:
plt.subplot(1,2,2)
plt.bar(x=Country_data['Country'],height= Country_data['Freq'],width=0.4,color ='orange')
plt.xlabel("Country Name")
plt.ylabel("Frequency")
plt.title("Country Data")
x_coordinate= np.arange(0,len(Country_data["Country"].values))
y_coordinate= Country_data["Freq"].values
labs=[]
for i in range(Country_data.shape[0]):
labs.append(str(Country_data["Freq"].values[i]))
plt.annotate(labs[i],(x_coordinate[i],y_coordinate[i]),textcoords="offset points",xytext=(0,3),ha='center',fontsize=6)
## 2nd Plot:
plt.subplot(1,2,1)
plt.barh(y=Country_data['Country'],width= Country_data['Freq'],height=0.4,color ='maroon')
plt.xlabel("Frequency")
plt.ylabel("Country Name")
plt.title("Country Data")
y_coordinate= np.arange(0,len(Country_data["Country"].values))
x_coordinate= Country_data["Freq"].values+0.5
labs=[]
for i in range(Country_data.shape[0]):
labs.append(str(Country_data["Freq"].values[i]))
plt.annotate(labs[i],(x_coordinate[i],y_coordinate[i]),textcoords="offset points",xytext=(0,3),ha='center',fontsize=6)
## Adjustment of subplot layout
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
df = pd.DataFrame({
'Name': ['Arnab','Mainak','Kunal','Sourav'],
'Age': [45,38,60,89],
'Height(in cm)': [150, 180, 160, 170]
})
df
Name | Age | Height(in cm) | |
---|---|---|---|
0 | Arnab | 45 | 150 |
1 | Mainak | 38 | 180 |
2 | Kunal | 60 | 160 |
3 | Sourav | 89 | 170 |
# plotting graph
df.plot(x="Name", y=["Age", "Height(in cm)"], kind="bar")
<AxesSubplot:xlabel='Name'>
# plotting Height
ax = df.plot(x="Name", y="Height(in cm)", kind="bar")
# plotting age on the same axis
df.plot(x="Name", y="Age", kind="bar", ax=ax, color="maroon")
<AxesSubplot:xlabel='Name'>
plt.boxplot(x=np.random.normal(0,1,100))
{'whiskers': [<matplotlib.lines.Line2D at 0x1fb6e898df0>, <matplotlib.lines.Line2D at 0x1fb6e8a6100>], 'caps': [<matplotlib.lines.Line2D at 0x1fb6e8a63d0>, <matplotlib.lines.Line2D at 0x1fb6e8a66a0>], 'boxes': [<matplotlib.lines.Line2D at 0x1fb6e898c40>], 'medians': [<matplotlib.lines.Line2D at 0x1fb6e8a6970>], 'fliers': [<matplotlib.lines.Line2D at 0x1fb6e8a6c40>], 'means': []}
plt.boxplot(x=np.random.normal(0,1,100),patch_artist=True)
plt.show()
np.random.seed(10)
dataSet1 = np.random.normal(100, 10, 220)
dataSet2 = np.random.normal(80, 20, 200)
dataSet3 = np.random.normal(60, 35, 220)
dataSet4 = np.random.normal(50, 40, 200)
dataSet = [dataSet1, dataSet2, dataSet3, dataSet4]
plt.boxplot(dataSet)
plt.show()
np.random.seed(10)
dataSet1 = np.random.normal(100, 10, 220)
dataSet2 = np.random.normal(80, 20, 200)
dataSet3 = np.random.normal(60, 35, 220)
dataSet4 = np.random.normal(50, 40, 200)
dataSet = [dataSet1, dataSet2, dataSet3, dataSet4]
plt.boxplot(dataSet,vert=False,patch_artist=True)
plt.show()
np.random.seed(10)
dataSet1 = np.random.normal(100, 10, 220)
dataSet2 = np.random.normal(80, 20, 200)
dataSet3 = np.random.normal(60, 35, 220)
dataSet4 = np.random.normal(50, 40, 200)
dataSet = [dataSet1, dataSet2, dataSet3, dataSet4]
figure = plt.figure(figsize =(10, 7))
ax = figure.add_subplot(111)
bp = ax.boxplot(dataSet, patch_artist = True,notch ='True', vert = 0)
colors = ['#00FF00','#0F00FF', '#F00FF0','#FFFF0F']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
for whisker in bp['whiskers']:
whisker.set(color ='#8E008B',linewidth = 1.4,linestyle =":")
for cap in bp['caps']:
cap.set(color ='#8E008B',linewidth = 2.1)
for median in bp['medians']:
median.set(color ='blue',linewidth = 3)
for flier in bp['fliers']:
flier.set(marker ='D',color ='#d7298c',alpha = 0.6)
ax.set_yticklabels(['dataSet1', 'dataSet2','dataSet3', 'dataSet4'])
plt.title("Customized box plot using attributes")
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
## Preparing Data
plt.pie(x=Country_data['Freq'],labels=Country_data['Country'])
plt.show()
plt.figure(figsize=(20,6))
plt.pie(x=Country_data['Freq'],labels=Country_data['Country'])
plt.legend()
plt.show()
## Preparing Data
X= np.random.normal(loc=0,scale=1,size=1000)
## Histogram
plt.hist(X,color="maroon")
plt.show()
## Data 1: X1 From N(0,1)
X1= np.random.normal(loc=0,scale=1,size=1000)
## Data 2: X2 From Log Normal(0,1)
X2= np.random.lognormal(mean=0,sigma=1,size=1000)
## Plotting Histograms:
plt.hist(X1,label="Normal Distribution")
plt.hist(X2,label="Log Normal Distribution")
plt.legend(loc="upper right")
plt.title(label=" My Histograms")
plt.show()
## Plotting Histograms:
plt.hist(X1,label="Normal Distribution",alpha=0.5)
plt.hist(X2,label="Log Normal Distribution",alpha=0.5)
plt.legend(loc="upper right")
plt.title(label=" My Histograms")
plt.show()
## Preparing Data
Mean= [3.5,4.6,5.1]
se= [0.05,0.005,0.6]
y=[1,2,3]
plt.errorbar(Mean,y,xerr=se,fmt=".k",)
<ErrorbarContainer object of 3 artists>