python - How to make two markers share the same label in the legend using matplotlib? -


what want this: enter image description here

what this: enter image description here

so how merge markers 1 label? lines, lines, of course, u can realize not assigning label second line while using same linetype, markers, can not, since of different shapes.

i think it's best use full legend - otherwise, how readers know difference between 2 models, or 2 datasets? way:

enter image description here

but, if want way, can use custom legend shown in guide. you'll need create own class, do, defines legend_artist method, adds squares , circles appropriate. here plot generated , code used generate it:

enter image description here

#!/usr/bin/env python import matplotlib.pyplot plt import matplotlib.patches mpatches import numpy np   # ================================== # define form of function # ================================== def model(x, a=190, k=1):     return * np.exp(-k*x/50)  # ================================== # how many data points generated # ================================== num_samples = 15  # ================================== # create data plots # ================================== x_model = np.linspace(0, 130, 200)  x_data1 = np.random.rand(num_samples) * 130 x_data1.sort()  x_data2 = np.random.rand(num_samples) * 130 x_data2.sort()  data1 = model(x_data1, k=1) * (1 + np.random.randn(num_samples) * 0.2) data2 = model(x_data2, k=2) * (1 + np.random.randn(num_samples) * 0.15)  model1 = model(x_model, k=1) model2 = model(x_model, k=2)  # ================================== # plot # ================================== fig = plt.figure() ax = fig.add_subplot('111') ax.plot(x_data1, data1, 'ok', markerfacecolor='none', label='data (k=1)') ax.plot(x_data2, data2, 'sk', markeredgecolor='0.5', markerfacecolor='0.5', label='data (k=2)') ax.plot(x_model, model1, '-k', label='model (k=1)') ax.plot(x_model, model2, '--k', label='model (k=2)')  # ================================== # format plot # ================================== ax.set_xlabel('distance heated face($10^{-2}$ m)') ax.set_ylabel('temperature ($^\circ$c)') ax.set_xlim((0, 130)) ax.set_title('normal way plot') ax.legend() fig.tight_layout()  plt.show()   # ================================== # ================================== # again, custom # legend # ================================== # ================================== class anyobject(object):     pass   class data_handler(object):     def legend_artist(self, legend, orig_handle, fontsize, handlebox):         scale = fontsize / 22         x0, y0 = handlebox.xdescent, handlebox.ydescent         width, height = handlebox.width, handlebox.height         patch_sq = mpatches.rectangle([x0, y0 + height/2 * (1 - scale) ], height * scale, height * scale, facecolor='0.5',                 edgecolor='0.5', transform=handlebox.get_transform())         patch_circ = mpatches.circle([x0 + width - height/2, y0 + height/2], height/2 * scale, facecolor='none',                 edgecolor='black', transform=handlebox.get_transform())          handlebox.add_artist(patch_sq)         handlebox.add_artist(patch_circ)         return patch_sq  # ================================== # plot # ================================== fig = plt.figure() ax = fig.add_subplot('111') d1 = ax.plot(x_data1, data1, 'ok', markerfacecolor='none', label='data (k=2)') d2 = ax.plot(x_data2, data2, 'sk', markeredgecolor='0.5', markerfacecolor='0.5', label='data (k=1)') m1 = ax.plot(x_model, model1, '-k', label='model (k=1)') m2 = ax.plot(x_model, model2, '-k', label='model (k=2)')  # ax.legend([d1], handler_map={ax.plot: data_handler()}) ax.legend([anyobject(), m1[0]], ['data', 'model'], handler_map={anyobject: data_handler()})  # ================================== # format plot # ================================== ax.set_xlabel('distance heated face($10^{-2}$ m)') ax.set_ylabel('temperature ($^\circ$c)') ax.set_xlim((0, 130)) ax.set_title('custom legend') fig.tight_layout()  plt.show() 

Comments