How to plot a Pandas Dataframe with Matplotlib? - GeeksforGeeks

24 Jan, 2021
Prerequisites: 
Pandas
Matplotlib
Data visualization is the most important part of any analysis. Matplotlib is an amazing python library which can be used to plot pandas dataframe. There are various ways in which a plot can be generated depending upon the requirement. 
Bar Plot is one such example. To plot a bar graph using plot() function will be used.
Syntax:
matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
Example:
Python3
# importing pandas libraryimport pandas as pd# importing matplotlib libraryimport matplotlib.pyplot as plt  # creating dataframedf = pd.DataFrame({    'Name': ['John', 'Sammy', 'Joe'],    'Age': [45, 38, 90]})  # plotting a bar graphdf.plot(x="Name", y="Age", kind="bar")
Output:
 Histogram is an example of representing data as which is divided into closely related intervals. For this hist() function will be employed.
Syntax:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\*kwargs)
Example:
Python3
# importing pandas libraryimport pandas as pd# importing matplotlib libraryimport matplotlib.pyplot as plt  # creating dataframedf = pd.DataFrame({    'Age': [45, 38, 90, 60, 40, 50, 2, 32, 8, 15, 27, 69, 73, 55]})  # plotting a histogramplt.hist(df["Age"])plt.show()
Output:
Pie Chart is a great way of representing data which is a part of a whole. To plot a pie chart pie() function will be used.
Syntax:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, autopct=None, shadow=False)
Example:
Python3
# importing pandas libraryimport pandas as pd# importing matplotlib libraryimport matplotlib.pyplot as plt  # creating dataframedf = pd.DataFrame({    'Object': ['Bulb', 'Lamp', 'Table', 'Pen', 'Notebook'],    'Price': [45, 38, 90, 60, 40]})  # plotting a pie chartplt.pie(df["Price"], labels=df["Object"])plt.show()
Output:
In situations, where data is to be interpreted depending on dependent and non-dependent parameters, graphs like Line chart or Scatter plot, are used. To plot a line graph plot() function is sufficient but to visualize a scatter plot scatter() is used.
Syntax:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)
Example:
Python3
# importing pandas libraryimport pandas as pd# importing matplotlib libraryimport matplotlib.pyplot as plt  # creating dataframedf = pd.DataFrame({    'X': [1, 2, 3, 4, 5],    'Y': [2, 4, 6, 10, 15]})  # plotting a line graphprint("Line graph: ")plt.plot(df["X"], df["Y"])plt.show()  # plotting a scatter plotprint("Scatter Plot:  ")plt.scatter(df["X"], df["Y"])plt.show()
Output:
Picked
Python pandas-dataFrame
Python-matplotlib
Python-pandas
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Python | Get dictionary keys as a list
Bar Plot in Matplotlib
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
loops in python
Python - Call function from another file
Ways to filter Pandas DataFrame by column values
Python | Convert set into a list
Python program to find number of days between two given dates