python - Matplotlib Legend Guide basic examples -
i trying understand how use legend() better, how use proxy artists. find legend guide severely lacking. post similar this one.
i using python 2.7.5 on windows 7, matplotlib version 1.2.1. wrote code combination of examples in legend guide:
import matplotlib.patches mpatches import matplotlib.lines mlines import matplotlib.pyplot plt line_up, = plt.plot([1,2,3], label='line 2') line_down, = plt.plot([3,2,1], label='line 1') blue_line = mlines.line2d([], [], color='blue', marker='*', markersize=15, label='blue stars') red_patch = mpatches.patch(color='red', label='the red data') plt.legend([red_patch,blue_line]) plt.show()
the legend label colors not assigned in handles, nor patch , line stars. have tried removing , adding lines, adding , removing commas after lines, etc. better references using legend()? other tips using proxy artists? don't understand why examples give me totally different results...
i'm still using matplotlib 1.2.1 i'll tell works me. find if pass line objects legend(), have pass labels separately. [this consistent matplotlib documentation on legened()]. i've modified example this, , seems work:
import matplotlib.patches mpatches import matplotlib.lines mlines import matplotlib.pyplot plt line_up, = plt.plot([1,2,3], label='line 2') line_down, = plt.plot([3,2,1], label='line 1') blue_line = mlines.line2d([], [], color='blue', marker='*', markersize=15, label='blue stars') red_patch = mpatches.patch(color='red', label='the red data') lines = [blue_line, red_patch] labels = [line.get_label() line in lines] plt.legend(lines, labels) plt.show()
Comments
Post a Comment