0
0
Pandasdata~5 mins

Plot customization (title, labels, figsize) in Pandas

Choose your learning style9 modes available
Introduction

We add titles, labels, and size to plots to make them easier to understand and look better.

When you want to explain what the plot shows with a clear title.
When you want to name the x-axis and y-axis so people know what data they represent.
When the default plot size is too small or too big for your presentation or report.
When you want your plot to fit nicely in a document or slide.
When you want to make your plot look professional and clear.
Syntax
Pandas
df.plot(figsize=(width, height), title='Your Title', xlabel='X-axis Label', ylabel='Y-axis Label')

figsize takes a tuple with width and height in inches.

title, xlabel, and ylabel are strings to name parts of the plot.

Examples
Adds a title to the plot.
Pandas
df.plot(title='Sales Over Time')
Adds labels to the x-axis and y-axis.
Pandas
df.plot(xlabel='Month', ylabel='Sales')
Changes the plot size to 10 inches wide and 5 inches tall.
Pandas
df.plot(figsize=(10, 5))
Combines size, title, and axis labels in one plot.
Pandas
df.plot(figsize=(8, 4), title='Monthly Sales', xlabel='Month', ylabel='Sales')
Sample Program

This code creates a simple sales data table, then plots it with a title, axis labels, and a custom size.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr'], 'Sales': [250, 300, 400, 350]}
df = pd.DataFrame(data)
df.set_index('Month', inplace=True)

# Plot with customization
plot = df.plot(
    figsize=(8, 4),
    title='Monthly Sales',
    xlabel='Month',
    ylabel='Sales'
)
plt.show()
OutputSuccess
Important Notes

You need to import matplotlib.pyplot to show the plot when running outside notebooks.

Setting the index to the x-axis values (like 'Month') helps pandas plot the data correctly.

You can customize many other parts of the plot using matplotlib if needed.

Summary

Use title to add a clear heading to your plot.

Use xlabel and ylabel to name the axes.

Use figsize to control the size of your plot for better visibility.