Matplotlib
import [Link] as plt
from [Link] import rcParams
rcParams['[Link]'] = 15, 6
from matplotlib import pyplot as plt1
[Link]([1,2,3,4,5])
[Link]()
[Link]([1,2,1.5,3,2])
[Link]()
[Link]([2,4,6,8,10],[1,2,1.5,5,3])
[Link]()
Graph with title
x = [2,4,6,8,10]
y = [1,2,1.5,5,3]
[Link] (x,y)
[Link] ('First Graph')
[Link] ('Month ')
[Link] ('Sales')
[Link]()
Graph generation with Square
x= [Link] (0,16,2)
[Link](x, [x1**2 for x1 in x])
[Link]()
Multi Line Graph
x= [Link] (0,16,2)
[Link](x, [x1 for x1 in x])
[Link](x, [x1**2 for x1 in x])
[Link](x, [x1**3 for x1 in x])
[Link]()
Limit the x and Y axis values
x= [Link] (0,100,2)
[Link](x, [x1 for x1 in x])
[Link](x, [x1**2 for x1 in x])
[Link](x, [x1**3 for x1 in x])
[Link] ([0,20,0,80])
[Link]()
Using XLIM and Y LIM
x= [Link] (0,100,2)
[Link](x, [x1 for x1 in x])
[Link](x, [x1**2 for x1 in x])
[Link](x, [x1**3 for x1 in x])
[Link] ([0,20])
[Link] ([0,80])
[Link]()
[Link]
[Link]
from matplotlib import style
import [Link]
[Link] ('ggplot')
x =[10,12,14,16]
y= [20,30,15,25]
x1= [11,13,15,17]
y1= [32,28,34,24]
[Link] (x,y,'g',label = 'Sales',linewidth = 10)
[Link] (x1,y1,'y',label = 'Profit',linewidth = 3)
[Link] ('First Graph')
[Link] ('X -Axis')
[Link] ('Y-Axis')
[Link]()
[Link](True, color = 'k')
Line Chart with Label
x = ['10','12','14','16','18']
y = [15,10,12,18,20]
[Link] (x,y)
#[Link] (x - axis position , y axis position ie hight , value/text to be displayed ,
# ha = Horizontal alignment va = vertical alignment)
for i in range(len(x)):
print ('i=',i, 'and y[i] =',y[i])
[Link](i,y[i],y[i],ha ='center', va= 'bottom')
Bar Graph
[Link] ([10,12,14,16,18],[15,10,12,18,20],label = 'Example 1',color = 'g')
[Link] ([11,13,15,17,19],[20,17,25,12,28], label = 'Example 2',color = 'b')
[Link] ('Graph Two')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Bar Graph
[Link] ([10,12,14,16,18],[15,10,12,18,20],label = 'Example 1',color = 'g')
[Link] ([11,13,15,17,19],[20,17,25,12,28], label = 'Example 2',color = 'b')
[Link] ([11.1,13.1,15.1,17.1,19.1],[12,20,20,22,28], label = 'Example 3',color = 'r')
[Link] ('Graph Two')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Two Bars Overlapping
[Link] ([10,12,14,16,18],[15,10,12,18,20],label = 'Example 1',color = 'g')
[Link] ([11,13,15,17,19],[20,17,25,12,28], label = 'Example 2',color = 'b')
[Link] ([11,13,15,17,19],[12,20,20,22,28], label = 'Example 3',color = 'r')
[Link] ('Graph Two')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Bar Graph with width – Example 1
[Link] ([10,12,14,16,18],[15,10,12,18,20],label = 'Example 1',color = 'g')
[Link] ([11,13,15,17,19],[20,17,25,12,28], label = 'Example 2',width=1,color = 'b')
[Link] ([11,13,15,17,19],[12,20,20,22,28], label = 'Example 3',width=0.7,color = 'c')
[Link] ('Graph Two')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Bar Graph with width – Example 2
[Link] ([10,12,14,16,18],[15,10,12,18,20],label = 'Example 1',width=1.2,color = 'g')
[Link] ([10,12,14,16,18],[20,17,25,12,28], label = 'Example 2',width=0.7,color = 'b')
[Link] ('Graph Two')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Bar graph with Label
# Fc = Face color # ec = Edge Color
x = ['10','12','14','16','18']
y = [15,10,12,18,20]
[Link] (x,y,label = 'Example 1',width=.8,fc = 'lightgreen',ec = 'r')
#[Link] (x - axis position , y axis position ie hight , value/text to be displayed ,
# ha = Horizontal alignment va = vertical alignment )
for i in range(len(x)):
[Link](i,y[i],y[i],ha ='center', va= 'bottom')
[Link]()
[Link]()
Histogram
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [0,10,20,30,40,50]
[Link] (age,age_group)
[Link] ('Graph Three')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
Histogram With Formatting
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [0,10,20,30,40,50]
[Link] (age,age_group,histtype = 'bar',rwidth =0.5,color = 'b',label ='Age Group')
[Link] ('Graph Three')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Histogram With different Bin Size
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [10,30,30,35,40,50]
[Link] (age,age_group,histtype = 'bar',rwidth =0.5,color = 'b',label ='Age Group')
[Link] ('Graph Three')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Histogram with Steps
age = [10,12,45,32,42,22,21,28,35,14,16,20,33,33,35,38,32,42,45,48,18,19,26]
age_group= [10,20,30,40,50]
[Link] (age,age_group,histtype = 'step',color = 'b',label ='Age Group')
[Link] ('Graph Three')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Histogram from Numpy Series
y=[Link] (10,10)
[Link] (y)
[Link] ()
Scatter plot
x = [1,2,3,4,5,6,7,8,9,10]
y = [18,16,11,15,13,18,13,16,14,18]
[Link] (x,y,label = 'Sales',color ='k')
[Link] ('Graph Four')
[Link] ('X- Axis')
[Link] ('Y-Axis')
# Legend Positions - ``'upper left', 'upper right', 'lower left', 'lower right'``
[Link](loc= 'upper right')
[Link]()
Area Graph- Basic
days = [1,2,3,4,5]
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
[Link] (days,sleep,eat,work,play)
[Link] ('Graph Five')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
Area Graph
Example 1
days = [1,2,3,4,5]
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
[Link] (days,sleep,eat,work,play,labels= ('sleep','eat','work','play'),
colors= ['m','r','k','g'])
[Link] ('Graph Five')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Example 2
days = [1,2,3,4,5]
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
[Link] (days,sleep,eat,work,play,colors= ['m','r','k','g'])
[Link] ([],[],color = 'm',label='sleep',linewidth = 2)
[Link] ([],[],color = 'r',label='eat',linewidth = 2)
[Link] ([],[],color = 'k',label='work',linewidth = 2)
[Link] ([],[],color = 'g',label='play',linewidth = 2)
[Link] ('Graph Five')
[Link] ('X- Axis')
[Link] ('Y-Axis')
[Link]()
[Link]()
Pie Chart – Basic
hours = [5,8,3,4]
[Link] (hours)
[Link]()
Pie Chart – autopct with one decimal point
hours = [5,8,3,4]
activity = ['Sleep','Work','Eat','Play']
col=['g','r','c','y']
[Link](hours,labels = activity,colors = col,startangle = 90,shadow = True,
explode = (0,0,0,0.1),autopct = '%1.1f%%')
[Link] ('Pie Chart')
[Link]('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
'''
Other Parameters for [Link]
'scaled' Set equal scaling (i.e., make circles circular) by
changing dimensions of the plot box. This is the same as
``ax.set_aspect('equal', adjustable='box', anchor='C')``.
Additionally, further autoscaling will be disabled.
'tight' Set limits just large enough to show all data, then
disable further autoscaling.
'auto' Automatic scaling (fill plot box with data).
'image' 'scaled' with axis limits equal to data limits.
'square' Square plot; similar to 'scaled', but initially forcing
``xmax-xmin == ymax-ymin``.
'''
[Link]()
Pie Chart – autopct and Lebel examples
hours = [5,8,3,4]
activity = ['Sleep','Work','Eat','Play']
col=['g','r','c','y']
[Link] (221) # autopct with three decimal point
[Link](hours,labels = activity,autopct = '%1.3f%%')
[Link]('equal')
[Link](loc='upper left')
[Link] (222)# autopct with integer
[Link](hours,labels = activity,autopct = '%d%%')
[Link] ('Pie Chart')
[Link]('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
[Link](loc='upper right')
[Link]()
Donut Chart
[Link](figsize= (5,5))
hours = [5,8,3,4]
[Link] (hours)
inner_circle = [Link]((0,0), 0.70, fc = 'w')
'''
[Link]()
The gca() function in pyplot module of matplotlib library
is used to get the current Axes instance on the current figure
matching the given keyword args, or create one.
'''
[Link]().add_artist(inner_circle)
[Link]()
Plot Circles
import [Link] as plt
# (0,0) - Centre of Circle. .2- Radius of Circle
# clip_on=False - Otherwise only portion of circle visible
circle1=[Link]((0,0),.2,color='r',clip_on=False)
circle2=[Link]((.5,.5),.2,color='b')
circle3=[Link]((1,1),.2,color='g',clip_on=False)
# gcf ()- get current figure
fig = [Link]()
# gca () - get current axis
# add_artist() The Axes. add_artist() function is used to add an Artist to the axes.
[Link]().add_artist(circle1)
[Link]().add_artist(circle2)
[Link]().add_artist(circle3)
[Link]()
Donut Chart
# library
import [Link] as plt
# create data
names = ['Group A', 'Group B', 'Group C', 'Group D']
size = [12,11,3,30]
# Create a circle at the center of the plot
my_circle = [Link]( (0,0), 0.7, color='white')
# Give color names
[Link](size, labels=names, colors=['red','green','blue','skyblue'])
# gcf – Get Current Figure and gca – Get Current axis
p = [Link]()
[Link]().add_artist(my_circle)
# Show the graph
[Link]()
Donut Chart
sleep = [7,6,4,5,8]
eat = [2,3,4,2,1]
work =[8,9,10,8,9]
play=[4,5,3,3,2]
[Link](figsize= (10,10))
[Link](sleep,pctdistance = 0.92,autopct = '%1.1f%%, \n (sleep)', radius = 1.0)
[Link](eat,pctdistance = 0.88,autopct = '%1.1f%% \n (eat)', radius = 0.80)
[Link](work,pctdistance = 0.85,autopct = '%1.1f%% \n (work', radius = 0.60)
[Link](play,pctdistance = 0.80,autopct = '%1.1f%% \n (play)', radius = 0.40)
inner_circle = [Link]((0,0), 0.20, fc = 'white')
[Link]().add_artist(inner_circle)
[Link]()
Multiple Graph
Example 1
x1=[Link](10,30,2)
y1 = [1,5,6,7,8,3,4,6,5,4]
x2= [Link] (10,30,2)
y2 = [5,4,2,8,4,6,3,4,5,4]
[Link] (211)
[Link](x1,y1)
[Link] ('Graph 1')
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link](212)
[Link](x2,y2)
[Link] ('Graph 2')
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link]()
Example 2 b = colour o -circle
def f(t):
return ([Link](-t)*[Link](2*[Link]*t))
t1=[Link](10,20,2)
t2= [Link] (11,21,2)
[Link] (211)
[Link](t1,f(t1),'bo',t2,f(t2))
[Link] ('Graph 1')
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link](212)
[Link](t2,[Link](2*[Link]*t2))
[Link] ('Graph 2')
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link]()
tight_layout()
The tight_layout() function in pyplot module of matplotlib library is used to automatically
adjust subplot parameters to give specified padding.
x1=[Link](10,30,2)
y1 = [1,5,6,7,8,3,4,6,5,4]
x2= [Link] (10,30,2)
y2 = [5,4,2,8,4,6,3,4,5,4]
[Link] (211)
[Link](x1,y1)
[Link] ('Graph 1')
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link](212)
[Link](x2,y2)
[Link] ('Graph 2')
[Link] ('X-Axis')
[Link] ('Y-Axis')
plt.tight_layout(pad=1, h_pad=1, w_pad=1) # The labels are not overlapping
[Link]()
Create Graph from data set
import pandas as pd
from matplotlib import pyplot as plt
[Link]('seaborn')
data =pd.read_csv('C:\\Noble\\Training\\Top Mentor\\Training\\Data Set\\
Matplotlib_data.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']
[Link](ages, py_salaries, label='Python')
[Link](ages, js_salaries, label='JavaScript')
[Link](ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')
[Link]()
[Link]('Median Salary (USD) by Age')
[Link]('Ages')
[Link]('Median Salary (USD)')
plt.tight_layout()
[Link]()
Create Subplot with Figure and Axis
import pandas as pd
from matplotlib import pyplot as plt
[Link]('seaborn')
data = pd.read_csv('C:\\Noble\\Training\\Top Mentor\\Training\\Data Set\\
Matplotlib_data.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']
# Fig1 and fig 2 are figure
# ax1 and ax2 are axis
fig1, ax1 = [Link]()
fig2, ax2 = [Link]()
print ('Axis 1 ',ax1)
print ('Axis 2 ',ax2)
[Link](ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')
[Link](ages, py_salaries, label='Python')
[Link](ages, js_salaries, label='JavaScript')
[Link]()
ax1.set_title('Median Salary (USD) by Age')
ax1.set_ylabel('Median Salary (USD)')
[Link]()
ax2.set_xlabel('Ages')
ax2.set_ylabel('Median Salary (USD)')
plt.tight_layout()
[Link]()
[Link]('[Link]')
[Link]('[Link]')
Graph with different line style
y = [Link] (10)
[Link](y,'--',y*2, ':', y*3,'-', y*4,'-.')
[Link]()
Control Marker style
y = [Link] (10)
[Link](y,'*',y+5, 'o',y*2,'d')
[Link]()
Save Graph
x= [Link] (0,100,2)
[Link](x, [x1 for x1 in x], label = 'Linear Graph')
[Link](x, [x1**2 for x1 in x], label = 'Square Graph')
[Link](x, [x1**3 for x1 in x], label = 'Three times of x')
[Link] ([0,20])
[Link] ([0,80])
[Link] ('X-Axis')
[Link] ('Y-Axis')
[Link]()
[Link] ('Multi Line [Link]')
[Link]()