PANDAS绘制多个Y轴


73

我知道熊猫支持次要Y轴,但我很好奇是否有人知道将三次Y轴放置在地块上的方法...目前,我正在使用numpy + pyplot来实现这一点...但是对于大数据集来说它的速度很慢。

这是为了在同一张图上绘制具有不同单位的不同测量值以便于比较(例如,相对湿度/温度/和电导率)

所以真的很好奇是否有人在pandas没有太多工作的情况下就能做到这一点。

[编辑]我怀疑是否有办法做到这一点(没有太多的开销),但是我希望被证明是错误的,这可能是matplotlib的局限性...

Answers:


87

我认为这可能有效:

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))

输出:

输出量


这很棒。但是我们需要ax2吗?
goofd

使用这种方法,我遇到了图例重叠的问题。这对我有帮助: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

@ DexD.Hunterax3.set_ylabel('ylabelC3')ax.right_ax.set_ylabel('ylabelB2')
tommy.carstensen

1
怎么ax3.set_frame_on(True)办?您可以在代码中添加一些注释行吗?谢谢!
tommy.carstensen

1
像@ tommy.carstensen一样,我将不胜感激(更多)评论(最好是每行一个)。
Qaswed

36

没有以下情况的更简单的解决方案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)

....

使用函数来实现这一点:

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

    from pandas import plotting

    # 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 = getattr(getattr(plotting, '_matplotlib').style, '_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

例:

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))

输出:

多个Y轴


3
我正在AttributeError: module 'pandas.plotting' has no attribute '_matplotlib'了解最新的大熊猫
-wordforthewise,

1
试试: colors = plotting._style._get_standard_colors(num_colors=len(cols))
RGD2 '19
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.