96

I know pandas supports a secondary Y axis, but I'm curious if anyone knows a way to put a tertiary Y axis on plots. Currently I am achieving this with numpy+pyplot, but it is slow with large data sets.

This is to plot different measurements with distinct units on the same graph for easy comparison (eg: Relative Humidity/Temperature/ and Electrical Conductivity).

So really just curious if anyone knows if this is possible in pandas without too much work.

[Edit] I doubt that there is a way to do this(without too much overhead) however I hope to be proven wrong, as this may be a limitation of matplotlib.

3 Answers 3

115

I think this might work:

import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame
df = DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C'])

fig, ax = plt.subplots()
ax3 = ax.twinx()
rspine = ax3.spines['right']
rspine.set_position(('axes', 1.15))
ax3.set_frame_on(True)
ax3.patch.set_visible(False)
fig.subplots_adjust(right=0.7)

df.A.plot(ax=ax, style='b-')
# same ax as above since it's automatically added on the right
df.B.plot(ax=ax, style='r-', secondary_y=True)
df.C.plot(ax=ax3, style='g-')

# add legend --> take advantage of pandas providing us access
# to the line associated with the right part of the axis
ax3.legend([ax.get_lines()[0], ax.right_ax.get_lines()[0], ax3.get_lines()[0]],\
           ['A','B','C'], bbox_to_anchor=(1.5, 0.5))

Output:

Output

6
  • this is great. but do we need the ax2 ?
    – goofd
    Commented Feb 28, 2014 at 0:27
  • 2
    Using this approach I had problem with legend which was overlapping. This helped me: ax3.legend([ax.get_lines()[0], ax2.get_lines()[0], ax3.get_lines()[0]], [label1', 'label2', 'label3'], bbox_to_anchor=(1.15, 0.5))
    – tworec
    Commented Apr 12, 2017 at 11:05
  • @DexD.Hunter ax3.set_ylabel('ylabelC3') and ax.right_ax.set_ylabel('ylabelB2') Commented Aug 24, 2018 at 1:06
  • 3
    What does ax3.set_frame_on(True) do? Can you add a few comment lines to the code? Thanks! Commented Aug 24, 2018 at 1:33
  • 2
    Like @tommy.carstensen I would appreciate (much) more comments (ideally one per line).
    – Qaswed
    Commented Apr 25, 2019 at 11:08
61

A simpler solution without plt:

ax1 = df1.plot()

ax2 = ax1.twinx()
ax2.spines['right'].set_position(('axes', 1.0))
df2.plot(ax=ax2)

ax3 = ax1.twinx()
ax3.spines['right'].set_position(('axes', 1.1))
df3.plot(ax=ax3)

....

Using function to achieve this:

def plot_multi(data, cols=None, spacing=.1, **kwargs):

    from pandas.plotting._matplotlib.style import get_standard_colors

    # Get default color style from pandas - can be changed to any other color list
    if cols is None: cols = data.columns
    if len(cols) == 0: return
    colors = get_standard_colors(num_colors=len(cols))

    # First axis
    ax = data.loc[:, cols[0]].plot(label=cols[0], color=colors[0], **kwargs)
    ax.set_ylabel(ylabel=cols[0])
    lines, labels = ax.get_legend_handles_labels()

    for n in range(1, len(cols)):
        # Multiple y-axes
        ax_new = ax.twinx()
        ax_new.spines['right'].set_position(('axes', 1 + spacing * (n - 1)))
        data.loc[:, cols[n]].plot(ax=ax_new, label=cols[n], color=colors[n % len(colors)], **kwargs)
        ax_new.set_ylabel(ylabel=cols[n])
        
        # Proper legend position
        line, label = ax_new.get_legend_handles_labels()
        lines += line
        labels += label

    ax.legend(lines, labels, loc=0)
    return ax

Example:

from random import randrange

data = pd.DataFrame(dict(
    s1=[randrange(-1000, 1000) for _ in range(100)],
    s2=[randrange(-100, 100) for _ in range(100)],
    s3=[randrange(-10, 10) for _ in range(100)],
))

plot_multi(data.cumsum(), figsize=(10, 5))

Output:

Multiple Y-Axes

9
  • 3
    I'm getting AttributeError: module 'pandas.plotting' has no attribute '_matplotlib' with latest pandas Commented Sep 12, 2019 at 18:39
  • 1
    try: colors = plotting._style._get_standard_colors(num_colors=len(cols))
    – RGD2
    Commented Oct 8, 2019 at 0:27
  • 1
    What if the x axis is also data from a column?
    – Ken S
    Commented Feb 4, 2021 at 15:30
  • 1
    It is not useful in pandas 1.2.0, got AttributeError: module 'pandas.plotting' has no attribute '_style'
    – somewheve
    Commented Feb 17, 2021 at 16:34
  • 1
    @KenS To provide custom X axis you must replace the plotting code above with data.plot(x='x_column', y='y_column', ...). It's easier to read by the way.
    – offchan
    Commented Oct 30, 2021 at 23:39
2

I modified the above answer a bit to make it accept custom x column, well-documented, and more flexible.

You can copy this snippet and use it as a function:

from typing import List, Union

import matplotlib.axes
import pandas as pd

def plot_multi(
    data: pd.DataFrame,
    x: Union[str, None] = None,
    y: Union[List[str], None] = None,
    spacing: float = 0.1,
    **kwargs
) -> matplotlib.axes.Axes:
    """Plot multiple Y axes on the same chart with same x axis.

    Args:
        data: dataframe which contains x and y columns
        x: column to use as x axis. If None, use index.
        y: list of columns to use as Y axes. If None, all columns are used
            except x column.
        spacing: spacing between the plots
        **kwargs: keyword arguments to pass to data.plot()

    Returns:
        a matplotlib.axes.Axes object returned from data.plot()

    Example:
    >>> plot_multi(df, figsize=(22, 10))
    >>> plot_multi(df, x='time', figsize=(22, 10))
    >>> plot_multi(df, y='price qty value'.split(), figsize=(22, 10))
    >>> plot_multi(df, x='time', y='price qty value'.split(), figsize=(22, 10))
    >>> plot_multi(df[['time price qty'.split()]], x='time', figsize=(22, 10))

    See Also:
        This code is mentioned in https://stackoverflow.com/q/11640243/2593810
    """
    from pandas.plotting._matplotlib.style import get_standard_colors

    # Get default color style from pandas - can be changed to any other color list
    if y is None:
        y = data.columns

    # remove x_col from y_cols
    if x:
        y = [col for col in y if col != x]

    if len(y) == 0:
        return
    colors = get_standard_colors(num_colors=len(y))

    if "legend" not in kwargs:
        kwargs["legend"] = False  # prevent multiple legends

    # First axis
    ax = data.plot(x=x, y=y[0], color=colors[0], **kwargs)
    ax.set_ylabel(ylabel=y[0])
    lines, labels = ax.get_legend_handles_labels()

    for i in range(1, len(y)):
        # Multiple y-axes
        ax_new = ax.twinx()
        ax_new.spines["right"].set_position(("axes", 1 + spacing * (i - 1)))
        data.plot(
            ax=ax_new, x=x, y=y[i], color=colors[i % len(colors)], **kwargs
        )
        ax_new.set_ylabel(ylabel=y[i])

        # Proper legend position
        line, label = ax_new.get_legend_handles_labels()
        lines += line
        labels += label

    ax.legend(lines, labels, loc=0)
    return ax

Here's one way to use it:

plot_multi(df, x='time', y='price qty value'.split(), figsize=(22, 10))

Not the answer you're looking for? Browse other questions tagged or ask your own question.