Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control the number of rows within a legend

I am currently trying to plot a large amount of data on a single plot. I have structured my representation using repeated colors and symbols. However, when plotting the final results, the legend appears slightly off because I cannot control the number of rows within it. Thus, instead of getting 5 repeated green, then 5 repeated red, 5 repeated blue then 2 other, I get 5 -4 -4 -4 (where I would have prefered 5 - 5 - 5 - 2)

You can clearly see this in attached image.

Plot with colored legend in wrong format

Right now I use these options for the legend:

axp.legend(loc="lower right",ncol=4)
like image 777
BlaB Avatar asked Dec 10 '15 21:12

BlaB


People also ask

What are handles in legend?

legend handle. The original object which is used to generate an appropriate entry in the legend.

How do you resize a legend?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

What is a legend on a scatter plot?

The legend on an Excel scatter plot is simply a list of the names for each of the series on the chart. The legend is color coordinated, so you can quickly determine which data points belong to which name.


1 Answers

I also had this problem a couple of times and use this workaround by adding dummy items to the legend to fill the last column, if there are more elegant methods available 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

like image 104
Bart Avatar answered Oct 09 '22 14:10

Bart