更改主图图例标签文本 [英] Change main plot legend label text

查看:92
本文介绍了更改主图图例标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经能够将子图贴上标签,但是主图上存在问题.

So far I have been able to label the subplots just fine but I'm having an issue with the main one.

这是我代码的相关部分:

Here's the relevant part of my code:

data_BS_P = data[channels[0]]
data_BS_R = data[channels[1]]
data_BS_Y = data[channels[2]]
plot_BS_P = data_BS_P.plot() #data_BS_P is a pandas dataframe
axBS = plot_BS_P.gca()
axBS.plot(data_BS_R, label='Roll')
axBS.plot(data_BS_Y, label='Yaw')
axBS.set_ylabel('Amplitude (urad)')
axBS.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3,
            fancybox=True, shadow=True)
ml1 = MultipleLocator(10)
ml2 = MultipleLocator(3600)
axBS.yaxis.set_minor_locator(ml1)
axBS.xaxis.set_minor_locator(ml2)
plot_BS_P.save('L1-SUS-BS_M1_DAMP_PRY_INMON.jpg')

这是我到目前为止的内容: 请注意,蓝线的标签很长.我希望将其标记为"Pitch"而不是文件名.我可以在哪一行?

And this is what I have so far: Notice the lengthy label for the blue line. I'd like that to be labeled as "Pitch" instead of the file name. In which line can I do that?

推荐答案

您需要访问legend()对象并使用set_text()更改文本值,这是一个简单的示例:

You need to gain access of the legend() object and use set_text() to change the text values, a simple example:

plt.plot(range(10), label='Some very long label')
plt.plot(range(1,11), label='Short label')
L=plt.legend()
L.get_texts()[0].set_text('make it short')
plt.savefig('temp.png')

对于您而言,您正在更改图例中的第一项,我很确定L.get_texts()[0]中的0索引也适用于您的问题.

In your case, you are changing the first item in the legend, I am quite sure the 0 index in L.get_texts()[0] applies to your problem too.

这篇关于更改主图图例标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆