I have the following code to generate the plots with a shared legend
from matplotlib.legend_handler import HandlerLine2D, HandlerTuple
import matplotlib.pyplot as pt
fig = pt.figure(figsize = (12,4))
gd = fig.add_gridspec(1,2)
p1 = fig.add_subplot(gd[0])
p2 = fig.add_subplot(gd[1])
redLine, = p1.plot([1,2,3], [4,2,5], 'r-')
greenLine, = p1.plot([1,2,3], [8,9,1], 'g--')
redDot, = p2.plot([1,2,3], [4,2,5], 'ro')
greenDot, = p2.plot([1,2,3], [8,9,1], 'gs')
leg = p2.legend([(redLine, redDot), (greenLine, greenDot)], ['Red', 'Green'], handler_map = {tuple: HandlerTuple(ndivide=None)})
Doing this however makes the legend lines a bit too short to clearly differentiate between solid line and dashed, so I'm trying to figure out how to make them longer without making the entire legend bigger.
From the documentation here https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D, it seems I should be able to do this by setting the sketch_params property. I have the following
legLines = leg.get_lines()
pt.setp(legLines, sketch_params = (1,2,3))
but this tells me it must be real number, not tuple -- contrary to what the documentation suggests. Also note the numbers in this example are arbitrary since I was just trying to understand how to use this.
I've tried a bunch of different stuff to get this shared legend to happen, and this is by far the closest I've gotten. So I was just hoping someone could help explain how I'm misusing the sketch_params
attribute, since it sounds like I should be able to specify the length with that.
EDIT: It was mentioned in the comments that to get sketch_params to work, I can simply do
for line in legLines:
line.set_sketch_params(1,2,3)
But it turns out that doesn't actually let me change the length of the lines like I wanted to. So I changed the question for more general help on how to achieve that.