python - Matplotlib plot legend shows markers twice -
the legend in plot shows marker icon twice in legend
the code produced plot given below
import pandas pd import random import matplotlib.pyplot plt import matplotlib.cm cm import numpy np n = 15 colors = cm.rainbow(np.linspace(0, 1, n)) df = [] in range(n): s = 'name %d' % df.append(dict(x=random.random(), y=random.random(), name=s)) df = pd.dataframe(df) c = 0 labels = [] fig, ax = plt.subplots(figsize=(12,12)) name, group in df.groupby('name'): x = group['x'].values[0] y = group['y'].values[0] color = colors[c] c += 1 ax.plot(x, y, color=color, marker='o', linestyle='', label=name) labels.append(name) handels, _ = ax.get_legend_handles_labels() ax.legend(handels, labels)
why happening?
my actual df
has multiple entries each name that's why groupby
. there i'm missing here?
you can either set plt.legend(loc=...,numpoints =1)
directly or create style sheet , set legend.numpoints : 1
if use linux system: place stylesheets in ~/.config/matplotlib/stylelib/
can use them plt.style.use([your_style_sheet])
. additionally, can e.g. make 1 sheet colors etc. , 1 size: plt.style.use([my_colors,half_column_latex])
Comments
Post a Comment