We have different types of plots in matplotlib library which can help us to make a suitable graph as you needed. As per the given data, we can make a lot of graph and with the help of pandas, we can create a dataframe before doing plotting of data. Let’s discuss the different types of plot in matplotlib by using Pandas.
Use these commands to install matplotlib, pandas and numpy:
pip install matplotlib pip install pandas pip install numpy
Types of Plots:
- Basic plotting: In this basic plot we can use the randomly generated data to plot graph using series and matplotlib.
# import librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npts=pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))ts=ts.cumsum()ts.plot()plt.show()chevron_rightfilter_noneOutput:
- Plot of different data: Using more than one list of data in a plot.
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npts=pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))df=pd.DataFrame(np.random.randn(1000,4),index=ts.index, columns=list('ABCD'))df=df.cumsum()plt.figure()df.plot()plt.show()chevron_rightfilter_noneOutput:

- Plot on given axis: We can explicitly define the name of axis and plot the data on the basis of this axis.
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npts=pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))df=pd.DataFrame(np.random.randn(1000,4), index=ts.index,columns=list('ABCD'))df3=pd.DataFrame(np.random.randn(1000,2),columns=['B','C']).cumsum()df3['A']=pd.Series(list(range(len(df))))df3.plot(x='A', y='B')plt.show()chevron_rightfilter_noneOutput:
- Bar plot using matplotlib: Find different types of bar plot to clearly understand the behaviour of given data.
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npts=pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))df=pd.DataFrame(np.random.randn(1000,4), index=ts.index,columns=list('ABCD'))df3=pd.DataFrame(np.random.randn(1000,2),columns=['B','C']).cumsum()df3['A']=pd.Series(list(range(len(df))))df3.iloc[5].plot.bar()plt.axhline(0, color='k')plt.show()chevron_rightfilter_noneOutput:
- Histograms:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf4=pd.DataFrame({'a': np.random.randn(1000)+1,'b': np.random.randn(1000),'c': np.random.randn(1000)-1},columns=['a','b','c'])plt.figure()df4.plot.hist(alpha=0.5)plt.show()chevron_rightfilter_noneOutput:
- Box plot using Series and matplotlib: Use box to plot the data of dataframe.
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf=pd.DataFrame(np.random.rand(10,5),columns=['A','B','C','D','E'])df.plot.box()plt.show()chevron_rightfilter_noneOutput:
- Density plot:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf=pd.DataFrame(np.random.rand(10,5),columns=['A','B','C','D','E'])ser=pd.Series(np.random.randn(1000))ser.plot.kde()plt.show()chevron_rightfilter_noneOutput:
- Area plot using matplotlib:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf=pd.DataFrame(np.random.rand(10,5),columns=['A','B','C','D','E'])df.plot.area()plt.show()chevron_rightfilter_noneOutput:
- Scatter plot:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf=pd.DataFrame(np.random.rand(500,4),columns=['a','b','c','d'])df.plot.scatter(x='a', y='b')plt.show()chevron_rightfilter_noneOutput:
- Hexagonal Bin Plot:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npdf=pd.DataFrame(np.random.randn(1000,2), columns=['a','b'])df['a']=df['a']+np.arange(1000)df.plot.hexbin(x='a', y='b', gridsize=25)plt.show()chevron_rightfilter_noneOutput:
- Pie plot:
# importing librariesimportmatplotlib.pyplot as pltimportpandas as pdimportnumpy as npseries=pd.Series(3*np.random.rand(4),index=['a','b','c','d'], name='series')series.plot.pie(figsize=(4,4))plt.show()chevron_rightfilter_noneOutput:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

