. Advertisement .
..3..
. Advertisement .
..4..
There can be quite a decent amount to learn when it comes to Pandas plot DataFrame. Worrying no more since with our article, you’ll know you will rack up the best only! Let’s check out for more.
Plot
The data may be interpreted using a variety of visualizations. Each graph serves a certain function. Bar graphs, scatter plots, histograms, and other types of plots are among them.
Today, we are going to grasp about processes to plot line charts, scatter diagrams, and bar charts.
Dataframe Example
Let’s first create a DataFrame example!
Running the code:
# importing required library
# In case pandas is not installed on your machine
# use the command 'pip install pandas'.
import pandas as pd
import matplotlib.pyplot as plt
# A dictionary which represents data
data_dict = { 'name':['p1','p2','p3','p4','p5','p6'],
'age':[20,20,21,20,21,20],
'math_marks':[100,90,91,98,92,95],
'physics_marks':[90,100,91,92,98,95],
'chem_marks' :[93,89,99,92,94,92]
}
# creating a data frame object
df = pd.DataFrame(data_dict)
# show the dataframe
# bydefault head() show
# first five rows from top
df.head()
Output:
name age math_marks physics_marks chem_marks
0 p1 20 100 90 93
1 p2 20 90 100 89
2 p3 21 91 91 99
3 p4 20 98 92 92
4 p5 21 92 98 94
How To Plot DataFrame In Pandas
A Line Chart Plotting
Trends throughout time are frequently displayed using line charts. Now let’s look at how to plot a line chart with Pandas.
Syntax: kind=’line’,x= ‘some_column’,y=’some_colum’,color=’somecolor’,ax=’someaxes’
Running the code:
#Get current axis
ax = plt.gca()
# line plot for math marks
df.plot(kind = 'line',
x = 'name',
y = 'math_marks',
color = 'green',ax = ax)
# line plot for physics marks
df.plot(kind = 'line',x = 'name',
y = 'physics_marks',
color = 'blue',ax = ax)
# line plot for chemistry marks
df.plot(kind = 'line',x = 'name',
y = 'chem_marks',
color = 'black',ax = ax)
# set the title
plt.title('LinePlots')
# show the plot
plt.show()
Output:
......... ADVERTISEMENT .........
..8..
Scatter Plotting
We only need to execute the plot()
function with a few arguments to obtain the scatterplot of a dataframe.
Syntax: kind='scatter',x= 'some_column',y='some_colum',color='somecolor'
Running the code:
# scatter plot
df.plot(kind = 'scatter',
x = 'math_marks',
y = 'physics_marks',
color = 'red')
# set the title
plt.title('ScatterPlot')
# show the plot
plt.show()
Output:
......... ADVERTISEMENT .........
..8..
Bar Chart Plotting
To obtain a bar plot, we also need to supply certain arguments for the plot()
function.
Syntax: kind='bar',x= 'some_column',y='some_colum',color='somecolor'
Running the code:
# bar plot
df.plot(kind = 'bar',
x = 'name',
y = 'physics_marks',
color = 'green')
# set the title
plt.title('BarPlot')
# show the plot
plt.show()
Output:
......... ADVERTISEMENT .........
..8..
The Bottom Line
You’ve just gone over a few instances of using Pandas to plot DataFrame. Hopefully, this article can be of great help to you then. Also, don’t forget to stay tuned with our upcoming updates for further gainful insight!
Leave a comment