Control the number of lines in the legend

I am currently trying to build a large amount of data on one site. I structured my presentation using repeating colors and symbols. However, when constructing the final results, the legend deviates slightly, because I cannot control the number of lines inside it. Thus, instead of 5 repeated greens, then 5 repeated red, 5 repeated blue, and then 2 others, I get 5 -4 -4 -4 (where I would prefer 5 - 5 - 5 - 2)

You can clearly see this in the attached image.

The plot with a color legend in the wrong format

Now I use these options for the legend:

axp.legend(loc="lower right",ncol=4) 
+6
source share
1 answer

I also had this problem several times and use this workaround, adding dummy elements to the legend to fill the last column, if there are more elegant methods, I would also be very interested to hear about them.

 import numpy as np import matplotlib.pylab as pl pl.figure() pl.plot(np.arange(10), np.random.random([10,5]), color='r', label='red') pl.plot(np.arange(10), np.random.random([10,5]), color='g', label='green') pl.plot(np.arange(10), np.random.random([10,5]), color='b', label='blue') pl.plot(np.arange(10), np.random.random([10,2]), color='k', label='black') # Add empty dummy legend items pl.plot(np.zeros(1), np.zeros([1,3]), color='w', alpha=0, label=' ') pl.legend(ncol=4) 

enter image description here

+6
source

All Articles