Skip to content

plot() Keywords

The pandas plot() method accepts many keyword arguments to customize visualizations. This document covers the most commonly used parameters.

ax - Specify Axes

The ax parameter allows plotting on an existing matplotlib axes object, enabling complex layouts.

import matplotlib.pyplot as plt
import pandas as pd
import yfinance as yf

ticker = 'WMT'
df = yf.Ticker(ticker).history(start='2020-01-01', end='2020-12-31')

# Create figure with multiple subplots
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 3))

# Plot on specific axes
df[['High', 'Low']].plot(ax=ax0, title='Price')
df['Volume'].plot(ax=ax1, title='Volume')

plt.tight_layout()
plt.show()

Why Use ax?

  • Combine multiple plots in one figure
  • Control layout precisely
  • Add annotations to specific subplots
  • Reuse axes from other libraries

title - Plot Title

# Single title
df['Close'].plot(title='Stock Price')

# With ax
fig, ax = plt.subplots()
df['Close'].plot(ax=ax, title='WMT Closing Price 2020')
plt.show()

figsize - Figure Size

Control the figure dimensions (width, height) in inches:

# Wider figure
df.plot(figsize=(12, 4))

# Square figure
df.plot(figsize=(6, 6))

# Tall figure
df.plot(figsize=(6, 10))

subplots - Separate Plots per Column

When subplots=True, each column gets its own subplot:

import matplotlib.pyplot as plt
import pandas as pd
import yfinance as yf

ticker = 'WMT'
df = yf.Ticker(ticker).history(start='2020-01-01', end='2020-12-31')

# Each column in separate subplot
fig, axes = plt.subplots(1, 2, figsize=(12, 3))
df[['High', 'Low']].plot(ax=axes, subplots=True)
plt.tight_layout()
plt.show()

With layout Parameter

# 2x2 grid of subplots
df[['Open', 'High', 'Low', 'Close']].plot(
    subplots=True, 
    layout=(2, 2),
    figsize=(10, 8)
)
plt.tight_layout()
plt.show()

x and y - Specify Columns

Use x and y to plot specific columns against each other:

import yfinance as yf

ticker = 'AAPL'
company = yf.Ticker(ticker)
maturity = company.options[0]
calls = company.option_chain(maturity).calls

fig, ax = plt.subplots(figsize=(10, 4))
calls.plot(
    x='strike',
    y='lastPrice',
    ax=ax
)
ax.set_title(f'{ticker} Call Options')
plt.show()

Multiple y Columns

df.plot(x='date', y=['open', 'close'])

label - Legend Label

Customize the legend label:

fig, ax = plt.subplots(figsize=(10, 4))
calls.plot(
    x='strike', 
    y='lastPrice', 
    label=f'{ticker} Call {maturity}',
    ax=ax
)
plt.show()

rot - Rotate Tick Labels

Rotate x-axis tick labels to prevent overlap:

import pandas as pd

url = 'https://raw.githubusercontent.com/theJollySin/scipy_con_2019/master/modern_time_series_analysis/ModernTimeSeriesAnalysis/StateSpaceModels/global_temps.csv'
df = pd.read_csv(url)
df = df.pivot(index='Date', columns='Source', values='Mean')

# Rotate labels 30 degrees
df['GCAG'].plot(rot=30)
plt.show()

Common Rotation Values

Value Use Case
0 Default horizontal
45 Moderate length labels
90 Long labels
30 Slight angle for dates

grid - Show Grid Lines

df.plot(grid=True)

legend - Control Legend

# Show legend (default)
df.plot(legend=True)

# Hide legend
df.plot(legend=False)

# Legend position
df.plot().legend(loc='upper left')

color / c - Line Colors

# Single color
df['A'].plot(color='red')

# Multiple colors for multiple columns
df.plot(color=['red', 'blue', 'green'])

# Using color codes
df['A'].plot(color='#FF5733')

style - Line Style

# Dashed line
df['A'].plot(style='--')

# With markers
df['A'].plot(style='o-')  # Line with circle markers

# Different styles per column
df.plot(style=['--', '-.', ':'])

Style Codes

Code Meaning
- Solid line
-- Dashed line
-. Dash-dot line
: Dotted line
o Circle marker
s Square marker
^ Triangle marker

alpha - Transparency

# 50% transparent
df.plot(alpha=0.5)

linewidth / lw - Line Width

df.plot(linewidth=2)

xlim and ylim - Axis Limits

df.plot(xlim=(0, 100), ylim=(0, 50))

Complete Example

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Create sample data
df = pd.DataFrame({
    'A': np.random.randn(100).cumsum(),
    'B': np.random.randn(100).cumsum()
}, index=pd.date_range('2024-01-01', periods=100))

# Plot with multiple customizations
fig, ax = plt.subplots(figsize=(12, 5))

df.plot(
    ax=ax,
    title='Cumulative Random Walk',
    color=['steelblue', 'coral'],
    linewidth=1.5,
    alpha=0.8,
    grid=True,
    rot=45
)

ax.set_xlabel('Date')
ax.set_ylabel('Value')
plt.tight_layout()
plt.show()

Summary Table

Keyword Purpose Example
ax Target axes ax=ax0
title Plot title title='My Plot'
figsize Figure size figsize=(12, 4)
subplots Separate subplots subplots=True
x, y Column selection x='col1', y='col2'
label Legend label label='Series A'
rot Tick rotation rot=45
grid Show grid grid=True
legend Show legend legend=False
color Line color color='red'
style Line style style='--'
alpha Transparency alpha=0.5
linewidth Line width linewidth=2
xlim, ylim Axis limits xlim=(0, 100)