How to plot a Pandas Dataframe with Matplotlib?

We can plot various types of visualizations like Line Graphs, Pie Charts, and Histograms with a Pandas DataFrame using Matplotlib. For this, we need to import both Pandas and Matplotlib libraries.

Required Imports

import pandas as pd
import matplotlib.pyplot as plt

Line Graph

A line graph shows the relationship between two numerical variables. Here's how to plot registration prices against units sold ?

import pandas as pd
import matplotlib.pyplot as plt

# Creating a DataFrame with car data
dataFrame = pd.DataFrame(
    {
        "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
        "Reg_Price": [2000, 2500, 2800, 3000, 3200, 3500],
        "Units": [100, 120, 150, 170, 180, 200]
    }
)

# Plot a line graph
plt.figure(figsize=(8, 5))
plt.plot(dataFrame["Reg_Price"], dataFrame["Units"], marker='o')
plt.xlabel('Registration Price')
plt.ylabel('Units Sold')
plt.title('Units Sold vs Registration Price')
plt.grid(True)
plt.show()

The output shows a line graph with registration prices on the x-axis and units sold on the y-axis.

Pie Chart

A pie chart displays the proportion of each category in the dataset. Let's visualize the registration price distribution among different car brands ?

import pandas as pd
import matplotlib.pyplot as plt

# Creating dataframe with car brands and prices
dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
})

# Plot a pie chart
plt.figure(figsize=(8, 8))
plt.pie(dataFrame["Reg_Price"], labels=dataFrame["Car"], autopct='%1.1f%%')
plt.title('Registration Price Distribution by Car Brand')
plt.axis('equal')
plt.show()

The pie chart shows the percentage contribution of each car brand to the total registration price.

Histogram

A histogram displays the frequency distribution of a numerical variable. Here's how to create a histogram of registration prices ?

import pandas as pd
import matplotlib.pyplot as plt

# Creating dataframe
dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
})

# Plot a histogram
plt.figure(figsize=(8, 6))
plt.hist(dataFrame["Reg_Price"], bins=5, edgecolor='black', alpha=0.7)
plt.xlabel('Registration Price')
plt.ylabel('Frequency')
plt.title('Distribution of Registration Prices')
plt.grid(True, alpha=0.3)
plt.show()

The histogram shows how frequently different price ranges appear in the dataset.

Comparison of Plot Types

Plot Type Best For Data Requirements
Line Graph Showing relationships between two numerical variables Two numerical columns
Pie Chart Showing proportions of categories Categorical labels and numerical values
Histogram Showing distribution of a single variable One numerical column

Conclusion

Matplotlib provides powerful plotting capabilities for Pandas DataFrames. Use line graphs for relationships, pie charts for proportions, and histograms for distributions. Adding titles, labels, and formatting makes visualizations more informative and professional.

Updated on: 2026-03-26T13:39:05+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements