Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change main plot legend label text

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

And this is what I have so far: enter image description here 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?

like image 624
N.K. Avatar asked Apr 12 '14 23:04

N.K.


People also ask

How do I change the text in a legend in Powerpoint?

On the Design tab, in the Data group, click Select Data. In the Select Data Source dialog box, in the Legend Entries (Series) box, select the legend entry that you want to change. Click Edit. Tip: To add a new legend entry, click Add, or to remove a legend entry, click Remove.

How do I change labels in legend?

Click on the legend name you want to change in the Select Data Source dialog box, and click Edit. Note: You can update Legend Entries and Axis Label names from this view, and multiple Edit options might be available.

How do I change my legend label in Seaborn?

To change the position of a legend in a seaborn plot, you can use the plt. legend() command. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.


2 Answers

Another way:

ax.legend(labels=mylabels) 
like image 165
Kamil Sindi Avatar answered Oct 05 '22 01:10

Kamil Sindi


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

enter image description here

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.

like image 33
CT Zhu Avatar answered Oct 05 '22 02:10

CT Zhu