子图的 matplotlib get_color

Yuc*_*uca 6 python matplotlib

我正在关注这里的教程:https : //matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
但是,使用的示例是针对单个图的,我目前正在处理子图。我的版本如下:

    p1 = tr[names['equity']].plot(ax=ax3, linewidth = 0.75)
    axb = ax3.twinx()
    axb.spines["right"].set_position(("axes", 0.5))
    p2 = tr[names[local_rating]].plot(ax=axb, c= 'r', linewidth = 0.75)
    axb.grid(False)
    axb.margins(x=0)
    axc = ax3.twinx()    
    p3 = tr[names['vol']].plot(ax=axc, c = 'g', linewidth = 0.75)
    axc.grid(False)
    axc.margins(x=0)
    ax3.yaxis.label.set_color(p1.get_color())
    axb.yaxis.label.set_color(p2.get_color())
    axc.yaxis.label.set_color(p3.get_color())
Run Code Online (Sandbox Code Playgroud)

当我尝试执行 pX.get_color() 时,我得到:

AttributeError: 'AxesSubplot' 对象没有属性 'get_color'

我的问题是:我应该使用什么方法来恢复子图的颜色?

我知道我可以手动设置颜色来匹配,因为它是少量的指令,但我只是想知道是否有另一种方法。

谢谢

Imp*_*est 9

这与子图无关。您正在使用 pandas 绘图函数而不是plot示例中的 matplotlib函数。

轴确实没有颜色。如果您对轴内线条的颜色感兴趣,您可以使用例如

ax.get_lines()[0].get_color()
Run Code Online (Sandbox Code Playgroud)

获取轴中第一行的颜色ax