Pandas Data Visualization
Data visualization is an important part of data analysis; it helps us better understand and explain patterns, trends, and relationships in the data.
Through graphics, charts, and other forms, data visualization turns complex numbers and statistical information into easy-to-understand images, making it easier to make decisions.
Pandas provides integration with Matplotlib and Seaborn and other visualization libraries, making data visualization simple and efficient.
In Pandas, data visualization is mainly implemented through the DataFrame.plot() and Series.plot() method; these methods are actually wrappers around the Matplotlib library, simplifying the chart-drawing process.
| Chart type | Description | Method |
|---|---|---|
| Line chart | Show trends of data over time or other continuous variables | df.plot(kind='line') |
| Bar chart | Compare data across different categories | df.plot(kind='bar') |
| Horizontal bar chart | Compare data across different categories, with horizontal bars | df.plot(kind='barh') |
| Histogram | Show the distribution of data | df.plot(kind='hist') |
| Scatter chart | Show the relationship between two numeric variables | df.plot(kind='scatter', x='col1', y='col2') |
| Boxplot | Show data distribution, including median, quartiles, and more | df.plot(kind='box') |
| Density plot | Show the density distribution of data | df.plot(kind='kde') |
| Pie chart | Show the proportion of each part within the whole | df.plot(kind='pie') |
| Area plot | Show cumulative values of data | df.plot(kind='area') |
The basic functions and methods of Pandas data visualization can meet most everyday visualization needs, but for more complex visualizations you can use it together with Matplotlib and Seaborn for finer chart customization.
1. Overview of Pandas Data Visualization
Pandas provides plot() methods that can easily draw different types of charts, including line plots, bar charts, histograms, scatter plots, and more.plot() The method has many parameters for customizing chart styles, colors, labels, and more.
1. Basic plot() Method
| Parameters | Description |
|---|---|
kind |
chart types, supporting 'line', 'bar', 'barh', 'hist', 'box', 'kde', 'density', 'area', 'pie' and other types |
x |
Set the data column for the x-axis |
y |
Set the data column for the y-axis |
title |
The chart's title |
xlabel |
Label for the x-axis |
ylabel |
Label for the y-axis |
color |
Set the chart color |
figsize |
Set the chart size (width, height) |
legend |
Whether to show the legend |
2. Common Chart Types
| Chart type | Description | Common usage |
|---|---|---|
| Line chart | Used to show data trends over time | df.plot(kind='line') |
| Bar chart | Used to show comparative data between categories | df.plot(kind='bar') |
| Horizontal bar chart | Similar to a bar chart, but with horizontal bars | df.plot(kind='barh') |
| Histogram | Used to show the distribution of data (frequency distribution) | df.plot(kind='hist') |
| Scatter chart | Used to show the relationship between two numeric variables | df.plot(kind='scatter', x='col1', y='col2') |
| Boxplot | Used to show data distribution, outliers, and quartiles | df.plot(kind='box') |
| Density plot | Used to show the density distribution of data | df.plot(kind='kde') |
| Pie chart | Used to show the proportion of each part of the whole | df.plot(kind='pie') |
| Area plot | Used to show cumulative values (similar to a line plot, but filled with color) | df.plot(kind='area') |
2. Data Visualization Examples
1. Line Plot
Line plots are commonly used to show how data changes over time.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Year'
:
[
2015
,
2016
,
2017
,
2018
,
2019
,
2020
]
,
'Sales'
:
[
100
,
150
,
200
,
250
,
300
,
350
]
}
df
=
pd.
DataFrame
(
data
)
Draw a line chart
df.
plot
(
kind
=
'line'
,
x
=
'Year'
,
y
=
'Sales'
,
title
=
'Sales Over Years'
,
xlabel
=
'Year'
,
ylabel
=
'Sales'
,
figsize
=
(
10
,
6
)
)
plt.
show
(
)
Output:

2. Bar Chart
Bar charts are used to show comparisons between different categories, especially for discrete data.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Category'
:
[
'A'
,
'B'
,
'C'
,
'D'
]
,
'Value'
:
[
10
,
15
,
7
,
12
]
}
df
=
pd.
DataFrame
(
data
)
Draw a bar chart
df.
plot
(
kind
=
'bar'
,
x
=
'Category'
,
y
=
'Value'
,
title
=
'Category Values'
,
xlabel
=
'Category'
,
ylabel
=
'Value'
,
figsize
=
(
8
,
5
)
)
plt.
show
(
)
Output:

3. Scatter Plot
Scatter plots are used to show the relationship between two numeric variables.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Height'
:
[
150
,
160
,
170
,
180
,
190
]
,
'Weight'
:
[
50
,
60
,
70
,
80
,
90
]
}
df
=
pd.
DataFrame
(
data
)
Draw a scatter plot
df.
plot
(
kind
=
'scatter'
,
x
=
'Height'
,
y
=
'Weight'
,
title
=
'Height vs Weight'
,
xlabel
=
'Height (cm)'
,
ylabel
=
'Weight (kg)'
,
figsize
=
(
8
,
5
)
)
plt.
show
(
)
Output:

4. Histogram
Histograms are used to show the distribution of data, especially its frequency distribution.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Scores'
:
[
55
,
70
,
85
,
90
,
60
,
75
,
80
,
95
,
100
,
65
]
}
df
=
pd.
DataFrame
(
data
)
Draw a histogram
df.
plot
(
kind
=
'hist'
,
y
=
'Scores'
,
bins
=
5
,
title
=
'Scores Distribution'
,
xlabel
=
'Scores'
,
figsize
=
(
8
,
5
)
)
plt.
show
(
)
Output:

5. Box Plot
Box plots are used to show data distribution, including the median, quartiles, and outliers.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Scores'
:
[
55
,
70
,
85
,
90
,
60
,
75
,
80
,
95
,
100
,
65
]
}
df
=
pd.
DataFrame
(
data
)
Draw box plot
df.
plot
(
kind
=
'box'
,
title
=
'Scores Boxplot'
,
ylabel
=
'Scores'
,
figsize
=
(
8
,
5
)
)
plt.
show
(
)
Output:

6. Pie Chart
Pie charts are used to show the proportion of each part of the whole.
Example
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Category'
:
[
'A'
,
'B'
,
'C'
,
'D'
]
,
'Value'
:
[
10
,
15
,
7
,
12
]
}
df
=
pd.
DataFrame
(
data
)
Draw the pie chart
df.
plot
(
kind
=
'pie'
,
y
=
'Value'
,
labels
=
df
[
'Category'
]
,
autopct
=
'%1.1f%%'
,
title
=
'Category Proportions'
,
figsize
=
(
8
,
5
)
)
plt.
show
(
)
Output:

3. Seaborn Visualization
Seaborn is a high-level data visualization library built on Matplotlib, offering prettier and easier-to-use charts and a richer variety of statistical chart types.
In Pandas, it can be used directly together with Seaborn.
Heatmap:
Example
import
pandas
as
pd
import
seaborn
as
sns
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'A'
:
[
1
,
2
,
3
]
,
'B'
:
[
4
,
5
,
6
]
,
'C'
:
[
7
,
8
,
9
]
}
df
=
pd.
DataFrame
(
data
)
Draw a heatmap
sns.
heatmap
(
df.
corr
(
)
,
annot
=
True
,
cmap
=
'coolwarm'
)
plt.
show
(
)
Output:

Scatter plot matrix of all numeric features in the dataset:
Example
import
pandas
as
pd
import
seaborn
as
sns
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'A'
:
[
1
,
2
,
3
]
,
'B'
:
[
4
,
5
,
6
]
,
'C'
:
[
7
,
8
,
9
]
}
df
=
pd.
DataFrame
(
data
)
sns.
pairplot
(
df
)
plt.
show
(
)
Output:

4. Advanced Matplotlib Customization
In addition to using the plot() methods provided by Pandas, Matplotlib can offer more flexible customization, such as adding titles, labels, setting chart styles, adjusting axes, and more.
Example
import
pandas
as
pd
import
seaborn
as
sns
import
matplotlib.
pyplot
as
plt
Sample data
data
=
{
'Year'
:
[
2015
,
2016
,
2017
,
2018
,
2019
]
,
'Sales'
:
[
100
,
150
,
200
,
250
,
300
]
}
df
=
pd.
DataFrame
(
data
)
Draw a line chart
plt.
plot
(
df
[
'Year'
]
,
df
[
'Sales'
]
,
color
=
'blue'
,
marker
=
'o'
)
Customization
plt.
title
(
'Sales Over Years'
)
plt.
xlabel
(
'Year'
)
plt.
ylabel
(
'Sales'
)
plt.
grid
(
True
)
Display
plt.
show
(
)
Output:
