Python Pandas Plotting
In this article, you will learn different plotting techniques using the Python Pandas plotting methods and matplotlib library. Data exploration is the initial step in data analysis. Data exploration can use a combination of manual methods and automated tools such as data visualizations, charts, and diagrams.
Python Matplotlib
A plot can present the data in continuous, discrete, surface, or volume form. The matplotlib library is used for data plotting and visualization. It is a very useful complement with Pandas.
pip install matplotlib
Pandas plot() Method
Pandas provides the plot() method to create different plots with a dataframe or series. The matplotlib module provides simple and basic-level tools for plotting. But, in many cases, we need to make a complete plot which is spread across many objects. The Pandas have a rich number of high-level plotting methods for creating standard plots.
Syntax of plot()DataFrame.plot(x, y, kind, ax, figsize, title, grid, style=None, logx, logy, xticks, yticks, fontsize, colormap, table, sort_columns)
x, y- level or position, none by default.
kind- kind of plotting to produce, like - line, bar, hist, box, density, pie.
ax- matplotlib axes object, none by default.
figsize- a tuple (width, height) in inches, none by default.
title- title to use for a plot.
grid- axes grid lines (bool), none by default.
style- matplotlib line style per column.
logx- use log scaling on x axes, default False.
logy- use log scaling on y axes, default False.
xsticks- values to use for the xticks.
ysticks- values to use for the yticks.
fontsize- font size for xticks and yticks.
colormap- colormap to select color from and if we provide a string, then load color from that name from matplotlib.
table- use to draw a table using the data in the dataframe.
sort_columns- sort column names to determine plot ordering.
Python Pandas Line Plots
We can plot graphs using the plot() method on both series and dataframe data. Here, we have plotted a simple line graph of the student's grade.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'students':[15,30,30,15,10],
'grade':['A','B','C','D','E']
})
df.plot(kind='line',x='grade',y='students',color='red')
plt.title('Number of students by grade')
plt.xlabel('Grade')
plt.ylabel('Students')
plt.show()
Similarly, we can plot multiple plot graph on a single window. Here, we have used ax property and assign matplotlib subplot object to plot on.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'sales_item1':[30,40,30,25,32,45],
'sales_item2':[25,60,55,40,35,70],
'months': ['Jan','Feb','Mar','Apr','May','Jun']
})
ax = plt.gca()
df.plot(kind='line',x='months',y='sales_item1',color='green',ax=ax)
df.plot(kind='line',x='months',y='sales_item2',color='red',ax=ax)
plt.show()
Python Pandas Bar Plot
Bar plot represents data in a rectangular bar with heights proportional to the values that they represent. There are several techniques to plot bar graph, but here we have used a very simple process to plot bar graph.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'sales_item1':[30,40,30,25,32,45],
'sales_item2':[25,60,55,40,35,70],
'sales_item3':[15,50,45,30,40,55],
'months': ['Jan','Feb','Mar','Apr','May','Jun']
})
df.plot.bar()
plt.xlabel('Months')
plt.ylabel('Sales')
plt.show()
Python Pandas Scatter Plot
In scatter plot, visualisation is represented by scattered data. Here, we have taken two variables. One of the two variables is scaled horizontally and the other is scaled vertically, and the graph is plotted on a Cartesian plane based on these variable positions.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([[450,'Jan'],[330,'Feb'],[530,'Mar'],
[545,'Apr'],[470,'May'],[510,'June'],
[495,'Jul'],[610,'Aug'],[520,'Sep'],
[480,'Oct'],[450,'Nov'],[395,'Dec']],
columns=['sales','months'])
df.plot.scatter(x='months',y='sales',c='DarkBlue')
plt.xlabel('Months')
plt.ylabel('Sales')
plt.show()
Python Pandas Pie Chart
The pie chart represents the values in slices in a circular graph.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'order':[30,40,30,25,32,45]},
index=['Sandwich', 'Pizza', 'Soup','Donut'
,'Burger','Noodles']
)
df.plot.pie(y='order',figsize=(5, 5))
plt.show()
Python Pandas Boxplot
Boxplot is a visual representation of data that shows the minimum, first quartile, median, third quartile and maximum in the graph. The median is a line that divides the box into two parts.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'sales_item1':[30,40,30,25,32,45],
'sales_item2':[25,60,55,40,35,70],
'sales_item3':[15,50,45,30,40,55],
'months': ['Jan','Feb','Mar','Apr','May','Jun']
})
df.boxplot(column=['sales_item1','sales_item2','sales_item3'])
plt.show()
Python Pandas Histograms
A histogram represents the distribution of numerical or categorical data. It is similar to the bar chart except a bar graph relates two variables, but a histogram relates only one variable. Here is a histogram plot that represents monthly item sales.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'sales_item1':[30,40,30,25,35,45],
'months': ['Jan','Feb','Mar','Apr','May','Jun']
})
df.hist()
plt.show()
Related Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib 3D Plot
How to save figure in matplotlib pyplot
Python Matplotlib Scatter Plot
Python Pandas Plotting
Pandas string to datetime
Write Python Pandas Dataframe to CSV
Fillna Pandas Example
Convert Excel to CSV Python Pandas
Read data from excel file using Python Pandas
Python Pandas CSV to Dataframe
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python Tkinter Geometry Managers
Python Tkinter Scale Widget
Python convert dict to xml