子图的 matplotlib get_color [英] matplotlib get_color for subplot
问题描述
我正在关注这里的教程: https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
但是,使用的示例仅用于单个绘图,而我目前正在处理子图.我的版本如下:
I am following the tutorial from here:
https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
However, the example used is for a single plot and I'm currently working with subplots.
My version is the following:
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())
当我尝试执行pX.get_color()时,我得到:
When I try to do pX.get_color() I get:
AttributeError: 'AxesSubplot' 对象没有属性 'get_color'
AttributeError: 'AxesSubplot' object has no attribute 'get_color'
我的问题是:我应该使用哪种方法来恢复子图的颜色?
我知道我可以手动设置颜色以匹配,因为这是少量的说明,但我只是想知道是否还有其他方法.
I'm aware that I could manually set the color to match since it's a small number of instructions, but I'm just wondering if there is another way.
谢谢
推荐答案
这与子图无关.您使用的是熊猫绘图功能,而不是示例中的matplotlib plot
函数.
This has nothing to do with subplots. You are using the pandas plotting function instead of the matplotlib plot
function as in the example.
轴确实没有颜色.如果您对轴内的线条颜色感兴趣,可以使用例如
An axes indeed has no color. If you are interested in the color of the lines inside the axes you may use e.g.
ax.get_lines()[0].get_color()
获取轴 ax
中第一行的颜色.
to get the color of the first line in the axes ax
.
这篇关于子图的 matplotlib get_color的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!