I am trying to plot some data with confidence bands. I am doing this with two plots 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 confidence region) with a darker, solid line passing through the center. So far I have been able to use patches to create the rectangle legend key, but I don't know how to achieve the centerline. I tried using hatch, but there is no control over the placement, thickness, or color.
My original idea was to try and combine two patches (Patch and 2DLine); however, it hasn't worked yet. Is there a better approach? My MWE and current figure 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',))
The solution is borrowed from the comment by CrazyArm, found here: Matplotlib, legend with multiple different markers with one label. Apparently you can make a list of handles and assign only one label and it magically combines the two handles/artists.
I am having 'similar' issues. I was able to achieve the following thanks to this question.
However, my two (venus and mars) markers overlap in the legend. I tried playing with handlelength, but that doesn't seem to help. Any suggestions or comments would be helpful.
starting with your code,
this is the closest i can get you. there may be a way to create the patch in a way you want but i am a bit new to this as well and all i can do is build the proper legend:
From what i can tell you would have to create ans "artist" object that fits the design you are looking for which i cannot find a method of doing. some examples of something similar can be found in this thread: Custom Legend Thread
Hope that helps good luck, I am interested if there are more in depth ways as well.