Open In App

How to plot a Pandas Dataframe with Matplotlib?

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We have a Pandas DataFrame and now we want to visualize it using Matplotlib for data visualization to understand trends, patterns and relationships in the data. In this article we will explore different ways to plot a Pandas DataFrame using Matplotlib’s various charts. Before we start, ensure you have the necessary libraries using:

pip install pandas matplotlib

Types of Data Visualizations in Matplotlib

Matplotlib offers a wide range of visualization techniques that can be used for different data types and analysis. Choosing the right type of plot is essential for effectively conveying data insights.

1. Bar Plot

Bar Plot is useful for comparing values across different categories and comparing Categorical Data. It displays rectangular bars where the height corresponds to the magnitude of the data.

Python
import pandas
import matplotlib

data = {'Category': ['A', 'B', 'C', 'D'], 'Values': [20, 34, 30, 35]}
df = pd.DataFrame(data)

plt.bar(df['Category'], df['Values'], color='skyblue')
plt.xlabel("Category")
plt.ylabel("Values")
plt.title("Bar Chart Example")
plt.show()

Output:

bar

Bar Plot

2. Histogram

A histogram groups numerical data into intervals (bins) to show frequency distributions. It helps in understanding data distribution and spotting trends and is widely used for continuous data.

Python
import pandas
import matplotlib

df = pd.DataFrame({'Values': [12, 23, 45, 36, 56, 78, 89, 95, 110, 130]})

plt.hist(df['Values'], bins=5, color='lightcoral', edgecolor='black')
plt.xlabel("Value Range")
plt.ylabel("Frequency")
plt.title("Histogram Example")
plt.show()

Output:

hist

Histogram

3. Pie Chart

A pie chart is used to display categorical data as proportions of a whole. Each slice represents a percentage of the dataset.

Python
import pandas
import matplotlib

df = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Values': [30, 50, 20]})

plt.pie(df['Values'], labels=df['Category'], autopct='%1.1f%%', colors=['gold', 'lightblue', 'pink'])
plt.title("Pie Chart Example")
plt.show()

Output:

pie

Pie Chart

4. Scatter Plot

A scatter plot visualizes the relationship between two numerical variables helping us to identify correlations and data patterns.

Python
import pandas
import matplotlib

df = pd.DataFrame({'X': [1, 2, 3, 4, 5], 'Y': [2, 4, 1, 8, 7]})

plt.scatter(df['X'], df['Y'], color='green', marker='o')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Plot Example")
plt.show()

Output:

scatter

Scatter Plot

5. Line Chart

A line chart is ideal for analyzing trends and patterns in time-series or sequential data by connecting data points with a continuous line.

Python
import pandas
import matplotlib

df = pd.DataFrame({'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'], 'Sales': [200, 250, 300, 280, 350]})

plt.plot(df['Month'], df['Sales'], marker='o', linestyle='-', color='blue')
plt.xlabel("Month")
plt.ylabel("Sales")
plt.title("Line Chart Example")
plt.grid()
plt.show()

Output:

line

Line Chart

In this article we explored various techniques to visualize data from a Pandas DataFrame using Matplotlib. From bar charts for categorical comparisons to histograms for distribution analysis and scatter plots for identifying relationships each visualization serves a unique purpose.



Next Article

Similar Reads