python - Custom marker edge style in manual legend -
i want create legend plot manually not directly refer data in plot, such as:
import matplotlib.pyplot plt matplotlib.lines import line2d plt.plot([0, 1], [3, 2]) line = line2d([], [], label='abc', color='red', linewidth=1.5, marker='o', markerfacecolor='yellow', markeredgewidth=1.5, markersize=16) plt.legend(handles=[line], numpoints=1) plt.show()
this works great except because cannot find way of setting marker edge style (i.e. solid, dashed, dotted, etc.) well. there no markeredgestyle property or similar line2d, , setting linestyle instead not seem affect marker edge style. is there workaround this?
a few approaches come mind not sure of how feasible are:
- use entirely different line2d. need showable in legend, have same formatting options line2d, plus marker edge style. not sure if such class exists
- create custom class derived line2d , implement marker edge style myself
- create data in plot itself, remove there while keeping in legend somehow. not sure if class allows me in actual chart exists. note has contain both marker , line. closest thing can think of using scatter markers , plot lines, show 2 legend entries (unless there way combine single one)
ideally, line style , marker edge style different, if there way change marker edge style involves overwriting line style take it.
i using matplotlib 1.5.3.
you may use special marker symbol, has dotted edge, e.g. marker=ur'$\u25cc$'
(complete stix symbol table).
import matplotlib.pyplot plt matplotlib.lines import line2d plt.plot([0, 1], [3, 2]) line = line2d([], [], label='abc', color='red', linewidth=1.5, marker=ur'$\u25cc$', markeredgecolor='indigo', markeredgewidth=0.5, markersize=16) plt.legend(handles=[line], numpoints=1) plt.show()
this cannot filled.
on other hand, scatter
plot not have connecting lines, such linestyle
of scatter
affects indeed marker edge. may hence combine line2d
, scatter
, line has no marker , constitutes background line , scatter responsible marker.
import matplotlib.pyplot plt matplotlib.lines import line2d plt.plot([0, 1], [3, 2]) line = line2d([], [], label='abc', color='red', ls="-", linewidth=1.5) sc1 = plt.scatter([],[],s=14**2,facecolors='yellow', edgecolors='blue', linestyle='--') sc2 = plt.scatter([],[],s=14**2,facecolors='gold', edgecolors='indigo', linestyle=':', linewidth=1.5) plt.legend([(line,sc1), (line,sc2)], ["abc", "def"], numpoints=1) plt.show()
Comments
Post a Comment