'Change marker in the legend in matplotlib
Suppose that you plot a set of data:
plt.plot(x,y, marker='.', label='something')
plt.legend()
On the display, you will obtain . something, but how do you do to change it to - something, so that the marker that appears in the legend is a line a not a dot?
Solution 1:[1]
To follow up with more info. If you want to make your marker in the legend more visible when using plt.scatter(), you can do the following to automate the marker change process.
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
handles, labels, plt.gca().get_legend_handles_labels()
updated_handles = []
for handle in handles:
updated_handles.append(mpatches.Patch(color=handle.get_facecolor(), label=handle.get_label()))
by_label = dict(sorted(dict(zip(all_labels, updated_handles)).items()))
plt.figlegend(by_label.values(), by_label.keys(), ...)
If you would like to use a specific axis, change plt.gca() with yours. Also, on line 8 I sorted the legend, which is something you can choose not to do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
