Combine two Pyplot patches for a legend

I am trying to build some data with trust groups. I do this with two graphs for each data stream: plot and fill_between . I would like the legend to look similar to the plots, where each entry has a box (the color of the trust area) with a darker solid line through the center. So far, I could use patches to create a rectangle legend key, but I don't know how to reach the centerline. I tried using the hatch, but there is no control over the placement, thickness or color.

My initial idea was to try to combine the two patches (Patch and 2DLine); however, it has not yet worked. Is there a better approach? My MWE and current score are shown below.

 import matplotlib.patches as mpatches import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,11) y = np.linspace(0,1,11) plt.plot(x, y, c='r') plt.fill_between(x, y-0.2, y+0.2, color='r', alpha=0.5) p = mpatches.Patch(color='r', alpha=0.5, linewidth=0) plt.legend((p,), ('Entry',)) 

Figure

+4
python matplotlib data-visualization legend
source share
3 answers

The solution is borrowed from CrazyArm's comment found here: Matplotlib, a legend with several different markers with one label . Obviously, you can make a list of pens and assign only one tag, and it magically combines two pens / artists.

 import matplotlib.patches as mpatches import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,11) y = np.linspace(0,1,11) p1, = plt.plot(x, y, c='r') # notice the comma! plt.fill_between(x, y-0.2, y+0.2, color='r', alpha=0.5) p2 = mpatches.Patch(color='r', alpha=0.5, linewidth=0) plt.legend(((p1,p2),), ('Entry',)) 

Figure

+9
source share

starting with your code

This is the closest I can get. there may be a way to create the patch the way you want, but I'm a little new to this, and all I can do is build the right legend:

 import matplotlib.patches as mpatches import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,11) y = np.linspace(0,1,11) fig = plt.figure() ax = fig.add_subplot(111) plt.plot(x, y, c='r',label='Entry') plt.fill_between(x, y-0.2, y+0.2, color='r', alpha=0.5) p_handle = [mpatches.Patch(color='r', alpha=0.5, linewidth=0)] p_label = [u'Entry Confidence Interval'] handle, label = ax.get_legend_handles_labels() handles=handle+p_handle labels=label+p_label plt.legend(handles,labels,bbox_to_anchor=(0. ,1.02 ,1.,0.3),loc=8, ncol=5,mode='expand',borderaxespad=0,prop={'size':9},numpoints=1) plt.show() 

enter image description here

From what I can say, you will need to create an β€œartist” object that matches the design you are looking for that I cannot find. some examples of something similar can be found in this thread: Custom legend text

Hope this helps good luck, I wonder if there are deeper ways.

+1
source share

I'm having "similar" problems. Thanks to this question, I was able to achieve the following.

 fig = pylab.figure() figlegend = pylab.figure(figsize=(3,2)) ax = fig.add_subplot(111) point1 = ax.scatter(range(3), range(1,4), 250, marker=ur'$\u2640$', label = 'S', edgecolor = 'green') point2 = ax.scatter(range(3), range(2,5), 250, marker=ur'$\u2640$', label = 'I', edgecolor = 'red') point3 = ax.scatter(range(1,4), range(3), 250, marker=ur'$\u2642$', label = 'S', edgecolor = 'green') point4 = ax.scatter(range(2,5), range(3), 250, marker=ur'$\u2642$', label = 'I', edgecolor = 'red') figlegend.legend(((point1, point3), (point2, point4)), ('S','I'), 'center', scatterpoints = 1, handlelength = 1) figlegend.show() pylab.show() 

However, my two (Venus and Mars) markers overlap in legend. I tried playing with the handle length, but that doesn't seem to help. Any suggestions or comments would be helpful.

+1
source share

All Articles