I am attempting to add a 2nd legend to display the max values (via markers).
However, only the marker type in mrkr
is given in the 2nd legend and not the desired symbol corresponding to mrkr
.
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
np.random.seed(0)
df0 = pd.DataFrame(np.random.randint(0,1000,size=(18, 4)),
columns=list('ABCD'))
ax = df0.plot()
mrkr = ['o', '.', 'v', 'x']
mrkr_df = pd.DataFrame(mrkr)
idx_mx_vals = df0.abs().idxmax().to_list()
mx_vals = df0.to_numpy()[idx_mx_vals,
np.arange(len(idx_mx_vals))].tolist()
for x, y, m, line in zip(idx_mx_vals, mx_vals, mrkr, ax.lines):
line2 = ax.plot(x, y, marker=m, c=line.get_color(), markersize=20)
leg1 = ax.legend(loc='upper right')
leg2 = ax.legend(mrkr, loc='lower right')
# manually return leg1
ax.add_artist(leg1)
Desired Output:
2nd legend showing the markers with its symbol.
CodePudding user response:
legend_elements = []
for x, y, m, line in zip(idx_mx_vals, mx_vals, mrkr, ax.lines):
line2 = ax.plot(x, y, marker=m, c=line.get_color(), markersize=20)
legend_elements.append(Line2D([0], [0], c=line.get_color(), marker=m, label=m))
leg1 = ax.legend(loc='upper right')
leg2 = ax.legend(handles=legend_elements, loc='lower right')