Customizing Your Plot

Customizing Your Plot#

import pandas as pd, matplotlib.pyplot as plt # import statement
df = pd.read_csv('https://raw.githubusercontent.com/kwaldenphd/elements-of-computing/main/book/data/ch10/air_quality_no2.csv', index_col=0, parse_dates=True) # load data

We can customize the plot using parameters that work directly within the Pandas plotting function.

A few examples that set axis labels, plot title, etc.

# add axis labels and plot title
df['station_paris'].plot(xlabel = 'Paris Station', ylabel = 'No2 Level', title = 'Air Quality Level Readings')
# previous example, with grid set to True and color set to green
df['station_paris'].plot(xlabel = 'Paris Station', ylabel = 'No2 Level', title = 'Air Quality Level Readings', grid=True, color='green')
# sample example with adjusted font size
df['station_paris'].plot(xlabel = 'Paris Station', ylabel = 'No2 Level', title = 'Air Quality Level Readings', grid=True, color='green', fontsize=12)

Additional Resources#

For more parameters that can be passed to .plot(): pandas.DataFrame.plot

Parameter

Explanation

ax

matplotlib axes object; axes for the current figure

subplots

Default is False; set to True to enable multiple plots in the same figure

layout

Follows (rows, columns) syntax to set subplot layout

figsize

Follows (width, height) syntax; Sets figure object width and height in inches

use_Index

Default is True; uses dataframe index as ticks for X axis

title

Title to use for the plot; can take a list with titles for corresponding subplots

grid

Default is False; set to True to show axis grid lines

legend

Places legend on axis subplot

xticks

Values to use for X axis ticks

yticks

Values to use for Y axis ticks

xlim

Follows (lower limit, upper limit) syntax; Sets X axis limits

ylim

Follows (lower limit, upper limit) syntax; Sets Y axis limits

xlabel

Label for X axis; default uses index column name

ylabel

Label for Y axis; default uses Y column name

fontsize

Sets font size for tickmarks

colormap

Sets matplotlib colormap to select colors from

table

Default is False; set to True to draw a table from data in the DataFrame

stacked

Default is False in line and bar plots, True in area plot; if True, creates stacked plot