matplotlib starting concept

matplotlib-udemy-Copy1
In [1]:
import matplotlib.pyplot as plt
Matplotlib is building the font cache; this may take a moment.
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]:
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
In [7]:
y
Out[7]:
array([ 0.  ,  0.25,  1.  ,  2.25,  4.  ,  6.25,  9.  , 12.25, 16.  ,
       20.25, 25.  ])
In [8]:
#functional
plt.plot(x,y)
Out[8]:
[<matplotlib.lines.Line2D at 0x468f730>]
In [9]:
plt.plot(x,y)
plt.show()    # it is used (if you not using jupyter notebook) 
In [10]:
'string'
Out[10]:
'string'
In [11]:
print('string')
string
In [14]:
plt.plot(x,y,'r-')    #change color as red
Out[14]:
[<matplotlib.lines.Line2D at 0x4792b98>]
In [15]:
plt.plot(x,y)
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Title')
Out[15]:
Text(0.5, 1.0, 'Title')
In [16]:
plt.subplot()
Out[16]:
<AxesSubplot:>
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]:
[<matplotlib.lines.Line2D at 0xe978b20>]
In [19]:
plt.subplot(1,2,2)  
plt.plot(x,y,'r')
plt.subplot(1,2,1)
plt.plot(y,x,'b')
Out[19]:
[<matplotlib.lines.Line2D at 0xe90cbf8>]
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
<Figure size 720x360 with 0 Axes>
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()
 
<ipython-input-53-ebc58007db9a>:1: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  axes=fig.add_axes([0.1,0.1,0.8,0.8])   #[left,bottom,width,height]
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()
x is :  [0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
y is :  [ 0.    0.25  1.    2.25  4.    6.25  9.   12.25 16.   20.25 25.  ]
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]:
[<matplotlib.lines.Line2D at 0x3839fd0>]
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]:
[<matplotlib.lines.Line2D at 0xd6b2580>]
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]:
[<matplotlib.lines.Line2D at 0x5953610>]
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]:
Text(0.5, 1.0, 'SMALLER PLOT')
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]:
[<matplotlib.lines.Line2D at 0xc624e08>]
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]:
[<matplotlib.lines.Line2D at 0x5605820>]
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]:
[<matplotlib.lines.Line2D at 0x56a29a0>]
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()
<Figure size 432x288 with 0 Axes>
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]:
Text(0, 0.5, 'Y')
In [41]:
fig=plt.figure()

ax=fig.add_axes([0,0,1,1])
ax.plot(x,y)
ax.plot(y,x)
Out[41]:
[<matplotlib.lines.Line2D at 0xb3afa90>]
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]:
<matplotlib.legend.Legend at 0x572d628>
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]:
<matplotlib.legend.Legend at 0x561b658>

Plot Appearance

In [45]:
#color
fig=plt.figure()

ax=fig.add_axes([0,0,1,1])
ax.plot(x,y,color='green')
Out[45]:
[<matplotlib.lines.Line2D at 0x56bbb20>]
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]:
[<matplotlib.lines.Line2D at 0xd56d0a0>]
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]:
[<matplotlib.lines.Line2D at 0xd59c370>]
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]:
[<matplotlib.lines.Line2D at 0x5a2e0a0>]
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]:
[<matplotlib.lines.Line2D at 0xdeea868>]
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]:
[<matplotlib.lines.Line2D at 0xdd4d2e0>]
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]:
[<matplotlib.lines.Line2D at 0xe31f778>]
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]:
[<matplotlib.lines.Line2D at 0xdd364d8>]
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]:
[<matplotlib.lines.Line2D at 0xe3060d0>]
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]:
[<matplotlib.lines.Line2D at 0xe916e80>]
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]:
[<matplotlib.lines.Line2D at 0xeba5838>]
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]:
(0.0, 1.0)
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]:
(0.0, 2.0)
In [ ]:
 

Comments

Popular posts from this blog

bookmarks

problem from contest

numpy(Python for data science)

Rural Development