matplotlib starting concept
In [1]:
import matplotlib.pyplot as plt
In [2]:
%matplotlib inline
In [4]:
#plt.show() it is used (if you not using jupyter notebook)
In [5]:
import numpy as np
x=np.linspace(0,5,11)
y=x**2
In [6]:
x
Out[6]:
In [7]:
y
Out[7]:
In [8]:
#functional
plt.plot(x,y)
Out[8]:
In [9]:
plt.plot(x,y)
plt.show() # it is used (if you not using jupyter notebook)
In [10]:
'string'
Out[10]:
In [11]:
print('string')
In [14]:
plt.plot(x,y,'r-') #change color as red
Out[14]:
In [15]:
plt.plot(x,y)
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Title')
Out[15]:
In [16]:
plt.subplot()
Out[16]:
In [18]:
plt.subplot(1,2,1) #rows,column,plot number , 1X2 me 1st position
plt.plot(x,y,'r')
plt.subplot(1,2,2) # 1X2 me second position
plt.plot(y,x,'b')
Out[18]:
In [19]:
plt.subplot(1,2,2)
plt.plot(x,y,'r')
plt.subplot(1,2,1)
plt.plot(y,x,'b')
Out[19]:
In [ ]:
#object oriented method
In [1]:
import matplotlib.pyplot as plt
In [2]:
# %matplotlib inline
In [11]:
import numpy as np
x=np.linspace(0,5,11)
y=x**2
In [38]:
fig=plt.figure('my graph',figsize=(10,5)) # figure object created
In [53]:
axes=fig.add_axes([0.1,0.1,0.8,0.8]) #[left,bottom,width,height]
line = axes.plot(x,y)
plt.show()
In [57]:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5,11)
y=x**2
print('x is : ',x)
print('y is : ',y)
# fig=plt.figure('my graph',figsize=(10,4)) # figsize=(horizontal_length,vertical_length)
fig=plt.figure()
axes=fig.add_axes([0.1,0.1,0.8,0.8]) #[left,bottom,width,height]
line=axes.plot(x,y)
plt.show()
In [63]:
fig=plt.figure()
axes=fig.add_axes([0.1,0.1,0.8,0.8]) #[left,bottom,width,height]
line=axes.plot(x,y)
axes.set_xlabel('X Label')
axes.set_ylabel('Y Label')
axes.set_title('set title')
plt.show()
In [69]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.2,0.5,0.4,0.3])
In [70]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.4,0.5,0.4,0.3])
In [71]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.8,0.5,0.4,0.3])
In [80]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.4,0.5,0.45,0.8]) #here left is 0.4,bootom=0.5 , width=0.45,height=0.8
In [83]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([1,0.5,0.4,0.3]) # left 100%
In [85]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([1.5,0.5,0.4,0.3]) #left 150%
In [87]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.2,0.5,0.4,0.3])
axes1.plot(x,y)
axes2.plot(x,y)
Out[87]:
In [88]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.2,0.5,0.4,0.3])
axes1.plot(x,y)
axes2.plot(y,x)
Out[88]:
In [89]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.2,0.5,0.4,0.3])
axes1.plot(y,x)
axes2.plot(x,y)
Out[89]:
In [91]:
fig=plt.figure()
axes1=fig.add_axes([0.1,0.1,0.8,0.8])
axes2=fig.add_axes([0.2,0.5,0.4,0.3])
axes1.plot(x,y)
axes1.set_title('LARGER PLOT')
axes2.plot(y,x)
axes2.set_title('SMALLER PLOT')
Out[91]:
In [1]:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5,11)
y=x**2
In [2]:
fig,axes=plt.subplots(nrows=1,ncols=2)
In [3]:
fig,axes=plt.subplots(nrows=3,ncols=3)
In [5]:
fig,axes=plt.subplots(nrows=4,ncols=2)
In [7]:
fig,axes=plt.subplots(nrows=1,ncols=2)
for current_ax in axes:
current_ax.plot(x,y)
In [8]:
fig,axes=plt.subplots(nrows=1,ncols=2)
# axes list of fig ka collection ho gya samajhne ke liye
#axes[0],axes[1]
axes[0].plot(x,y)
axes[1].plot(y,x)
Out[8]:
In [23]:
fig,axes=plt.subplots(nrows=1,ncols=2)
# axes list of fig ka collection ho gya samajhne ke liye
#axes[0],axes[1]
axes[0].plot(x,y)
axes[0].set_title("First Plot")
axes[1].plot(y,x)
axes[1].set_title("Second Plot")
plt.tight_layout() # this will take care of any overlapping plots
Figure Size and DPI¶
In [24]:
fig=plt.figure(figsize=(8,2))
ax=fig.add_axes([0,0,1,1])
In [25]:
fig=plt.figure(figsize=(8,2))
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y)
Out[25]:
In [26]:
fig,axes=plt.subplots(nrows=2,ncols=1,figsize=(8,2))
In [27]:
fig,axes=plt.subplots(nrows=2,ncols=1,figsize=(8,2))
axes[0].plot(x,y)
axes[1].plot(y,x)
Out[27]:
In [28]:
fig,axes=plt.subplots(nrows=2,ncols=1,figsize=(8,2))
axes[0].plot(x,y)
axes[1].plot(y,x)
plt.tight_layout()
In [29]:
fig=plt.figure()
In [31]:
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y)
ax.set_title('Title')
ax.set_xlabel('X')
ax.set_ylabel("Y")
Out[31]:
In [41]:
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y)
ax.plot(y,x)
Out[41]:
In [36]:
fig.savefig('temp.jpg',dpi=200)
In [42]:
# legends
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,x**2,label='X Squared') # legend ke liye label yahi dena padega
ax.plot(x,x**3,label='Y Cubed')
ax.legend() # for location refer notes or documentation
Out[42]:
In [43]:
# legends
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,x**2,label='X Squared') # legend ke liye label yahi dena padega
ax.plot(x,x**3,label='Y Cubed')
ax.legend(loc=10) # for center
Out[43]:
Plot Appearance¶
In [45]:
#color
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='green')
Out[45]:
In [52]:
#color , linewidth=> i.e thickness
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',linewidth=8)
Out[52]:
In [53]:
#color , linewidth=> i.e thickness , alpha=> transparency
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',linewidth=8,alpha=0.5)
Out[53]:
In [58]:
# linewidth shortcut is lw
# linestyle
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=8,linestyle="--") # for dotted(dashed)
line style use "--"
Out[58]:
In [67]:
#linestyle shorcut is ls
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=8,ls=":")
Out[67]:
In [65]:
#drawstyle
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=8,drawstyle="steps")
Out[65]:
In [72]:
# marker
#marker define by symblo as name explained
#written in quotes
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=1,marker="1")
Out[72]:
In [75]:
# marker and markersize(not wriiten in quotes since value)
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=1,marker="1",markersize=20)
Out[75]:
In [76]:
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=1,marker="+",markersize=20)
Out[76]:
In [81]:
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=1,marker="*",markersize=20)
Out[81]:
In [84]:
#markerfacecolor , markeredgewidth and markeredgecolor
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=1,marker="o",markersize=20, markerfacecolor="green",markeredgewidth=3,markeredgecolor="red")
# also refer notes
Out[84]:
In [91]:
# lower bound and upperbound
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=2,ls="--")
ax.set_xlim([0,1])
Out[91]:
In [92]:
# lower bound and upperbound
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='#FF8C00',lw=2,ls="--")
ax.set_xlim([0,1])
ax.set_ylim([0,2])
Out[92]:
In [ ]:
Comments
Post a Comment