The plot data frame will add vertical lines; How to get custom legend text for everyone?

I can build a data frame (2 "Y") and add vertical lines (2) to the graph, and I can specify the text of the conditional legend for the values ​​of Y or vertical lines, but not simultaneously in both cases.

import pandas as pd
import matplotlib.pyplot as plt

d = {'x' : [1., 2., 3., 4.], 'y1' : [8., 6., 4., 2.], 'y2' : [-4., 13., 2.2, -1.1]}
df = pd.DataFrame(d)
ax = df.plot(x='x', y=['y1'], linestyle='-', color='b')
df.plot(x='x', y=['y2'], linestyle='--', color='y', ax=ax)
ax.legend(labels=['y1custom', 'y2custom'])
plt.axvline(x=1.5, color='r', linestyle='--', label='vline1.5custom')
plt.axvline(x=3.5, color='k', linestyle='--', label='vline3.5custom')
plt.legend()        # <---- comment out....or not....for different effects
plt.show()

The key line in the code is "plt.legend ()". With it, in the code, I get this (the conditional inscription has dataframe column labels "y1" and "y2" instead of my desired custom labels):

with a call to plt.legend ()

With the "plt.legend ()" deleted, I get this (the legend has my own shortcuts only for dataframe values, the legend for vertical lines doesn't even appear!):

without calling plt.legend ()

How can I get the best of both worlds, in particular the following (in any order) for my legend ?:

y1custom
y2custom
vline1.5custom
vline3.5custom

, dataframe, ... ! .

+4
3

legend() . .

, ax.get_legend_handles_labels() , , - . legend().

import pandas as pd
import matplotlib.pyplot as plt

d = {'x' : [1., 2., 3., 4.], 'y1' : [8., 6., 4., 2.], 'y2' : [-4., 13., 2.2, -1.1]}
df = pd.DataFrame(d)
ax = df.plot(x='x', y=['y1'], linestyle='-', color='b')
df.plot(x='x', y=['y2'], linestyle='--', color='y', ax=ax)

ax.axvline(x=1.5, color='r', linestyle='--', label='vline1.5custom')
ax.axvline(x=3.5, color='k', linestyle='--', label='vline3.5custom')

h,labels = ax.get_legend_handles_labels()
labels[:2] = ['y1custom','y2custom']
ax.legend(labels=labels)

plt.show()

enter image description here

+3

:

d = {'x' : [1., 2., 3., 4.], 'y1' : [8., 6., 4., 2.], 'y2' : [-4., 13., 2.2, -1.1]}
df = pd.DataFrame(d)
ax = df.plot(x='x', y=['y1'], linestyle='-', color='b')
df.plot(x='x', y=['y2'], linestyle='--', color='y', ax=ax)
l1 = plt.axvline(x=1.5, color='r', linestyle='--', label='vline1.5custom')
l2 = plt.axvline(x=3.5, color='k', linestyle='--', label='vline3.5custom')
#move ax.legend after axvlines and get_label
ax.legend(labels=['y1custom', 'y2custom',l1.get_label(),l2.get_label()])
plt.show()

:

enter image description here

0

labelcan pass in plot()while you specify the constructed column:

import pandas as pd
import matplotlib.pyplot as plt

d = {'x' : [1., 2., 3., 4.], 'y1' : [8., 6., 4., 2.], 'y2' : [-4., 13., 2.2, -1.1]}
df = pd.DataFrame(d)

ax = df['y1'].plot(x='x', linestyle='-', color='b', label='y1custom')
df['y2'].plot(x='x', linestyle='--', color='y', ax=ax, label='y2custom')
plt.axvline(x=1.5, color='r', linestyle='--', label='vline1.5custom')
plt.axvline(x=3.5, color='k', linestyle='--', label='vline3.5custom')
plt.legend()
plt.show()

This approach avoids the need to mess with the legend:

matplotlib figure + custom legend

0
source

Source: https://habr.com/ru/post/1694920/


All Articles