Pandas Plotting

Pandas Plotting

Pandas provides built-in plotting functions that make data visualization simple. These functions internally use Matplotlib, so you get quick plots with minimal code.

Pandas Plotting


1. Basic Plotting Syntax

df.plot()

or for a specific column:

df["ColumnName"].plot()

2. Common Plot Types in Pandas

Plot Type Use
line Trends over time
bar Category comparison
barh Horizontal bars
hist Distribution
scatter Relationship between two variables
box Outliers & spread
area Cumulative values
pie Proportion

3. Line Plot Example (Most Common)

import pandas as pd
import matplotlib.pyplot as plt

data = {
“Day”: [1, 2, 3, 4, 5],
“Sales”: [200, 300, 250, 400, 500]
}

df = pd.DataFrame(data)

df.plot(x=“Day”, y=“Sales”, kind=“line”)
plt.show()

Use case: Sales trend, stock prices, temperature over time


4. Bar Plot Example

data = {
"Product": ["A", "B", "C"],
"Revenue": [5000, 7000, 6000]
}

df = pd.DataFrame(data)

df.plot(x=“Product”, y=“Revenue”, kind=“bar”)
plt.show()

Use case: Comparing categories


5. Histogram Example

data = {
"Marks": [45, 55, 60, 70, 75, 80, 85, 90]
}

df = pd.DataFrame(data)

df[“Marks”].plot(kind=“hist”, bins=5)
plt.show()

Use case: Data distribution


6. Scatter Plot Example

data = {
"StudyHours": [1, 2, 3, 4, 5],
"Marks": [40, 50, 60, 70, 85]
}

df = pd.DataFrame(data)

df.plot(x=“StudyHours”, y=“Marks”, kind=“scatter”)
plt.show()

Use case: Relationship between variables


7. Box Plot Example

df = pd.DataFrame({
"Marks": [50, 55, 60, 65, 70, 75, 90]
})

df.plot(kind=“box”)
plt.show()

Use case: Detecting outliers


8. Multiple Columns Plot

data = {
"Year": [2021, 2022, 2023],
"Sales": [500, 700, 900],
"Profit": [200, 300, 400]
}

df = pd.DataFrame(data)

df.plot(x=“Year”)
plt.show()


9. Customizing Plots

df.plot(title="Sales Report", xlabel="Day", ylabel="Sales")
plt.show()

10. Real-World Example

df = pd.read_csv("sales.csv")
df.groupby("Month")["Revenue"].sum().plot(kind="bar")
plt.show()

Key Points

✔ Pandas plotting is fast and beginner-friendly
✔ Uses Matplotlib internally
✔ Best for quick analysis & exploration


Conclusion

Pandas plotting allows you to visualize data with very little code, making it ideal for exploratory data analysis before advanced visualization.

You may also like...