How to plot a Pandas Dataframe with Matplotlib?
Last Updated :
09 Apr, 2025
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 Plot2. 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:
Histogram3. 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 Chart4. 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 Plot5. 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 ChartIn 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.
Similar Reads
How to Plot a Dataframe using Pandas Pandas plotting is an interface to Matplotlib, that allows to generate high-quality plots directly from a DataFrame or Series. The .plot() method is the core function for plotting data in Pandas. Depending on the kind of plot we want to create, we can specify various parameters such as plot type (ki
8 min read
How to Create a Swarm Plot with Matplotlib Swarm plots, also known as beeswarm plots, are a type of categorical scatter plot used to visualize the distribution of data points in a dataset. Unlike traditional scatter plots, swarm plots arrange data points so that they do not overlap, providing a clear view of the distribution and density of d
5 min read
How to Create a Table with Matplotlib? In this article, we will discuss how to create a table with Matplotlib in Python. Method 1: Create a Table using matplotlib.plyplot.table() function In this example, we create a database of average scores of subjects for 5 consecutive years. We import packages and plotline plots for each consecutive
3 min read
How to Plot Multiple Series from a Pandas DataFrame? In this article, we will discuss how to plot multiple series from a dataframe in pandas. Series is the range of the data  that include integer points we cab plot in pandas dataframe by using plot() function Syntax: matplotlib.pyplot(dataframe['column_name']) We can place n number of series and we ha
2 min read
How to Create Subplots in Matplotlib with Python? Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to plot data from a text file using Matplotlib? Perquisites: Matplotlib, NumPy In this article, we will see how to load data files for Matplotlib. Matplotlib is a 2D Python library used for Date Visualization. We can plot different types of graphs using the same data like: Bar GraphLine GraphScatter GraphHistogram Graph and many. In this article,
3 min read