Home > Back-end >  Set a different `numpoints` parameter for each line inside the same matplotlib legend
Set a different `numpoints` parameter for each line inside the same matplotlib legend

Time:07-14

I have following plot:

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0, 0.25*np.pi, 200)
plt.plot(x, np.sin(x),label="sin", marker="x", markevery=12, c='k')
plt.plot(x, np.cos(x),label="cos", marker=".",markersize=1, lw=0, c='k')
plt.plot(x, np.tan(x),label="tan", ls="-.", c='k')
plt.gca().legend(numpoints=1)

plot of sin(x), cos(x), and tan(x) with a legend

The cosine would be better represented in the legend using, e.g., numpoints=8, while the sine is best represented as it is in the above image. Is there a way (rather a workaround) to use numpoints=1 for all but the cosine? Maybe combining two legends in one?

CodePudding user response:

You can create a custom handler, and then pass this handler as an keyword to the legend function. This is shown in the enter image description here

  • Related