I have recently discovered matplotlib as a much better alternative to Matlab for plots. Unfortunately, my knowledge of python is close to zero.
Considering the following minimal working example: I would like the entries in the legend to appear in the order in which they are declared. I learned from this post that the entries are ordered first based on type and then based on order (in this case, the two line plots always come before the scatter plot).
I tried to apply the methods from the above post to my example, i.e., creating handles and passing them to plt.legend
, but it doesn't work. Unfortunately, I'm doing it quite blindly as I don't fully understand what I'm doing, so it might be the wrong approach or some syntax error. How would one adapt the simple code below to enforce the desired legend order?
import numpy as np
import scipy.io
from matplotlib import pyplot as plt
plt.figure(figsize=[3.3, 3.3])
plt.rcParams.update({'font.size': 8, 'text.usetex': True})
plt.scatter([1, 2, 3], [5, 10, 15], label='1st entry')
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label='2nd entry')
plt.plot([1, 2, 3, 4], [1, 2, 3, 4], label='3st entry')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='lower right', labelspacing=0.25, handlelength=0.4, handletextpad=0.7)
plt.savefig('filename.pdf', format='pdf')
plt.show()