Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align rows in matplotlib legend with 2 columns

Tags:

I have an issue where some mathtext formatting is making some labels take up more vertical space than others, which causes them to not line up when placed in two columns in the legend. This is particularly important because the rows are also used to indicate related data.

Here is an example:

import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.mathtext as mathtext mpl.rc("font", family="Times New Roman",weight='normal') plt.rcParams.update({'mathtext.default':  'regular' }) plt.plot(1,1, label='A') plt.plot(2,2, label='B') plt.plot(3,3, label='C') plt.plot(4,4,label='$A_{x}^{y}$') plt.plot(5,5,label='$B_{x}^{y}$') plt.plot(6,6,label='$C_{x}^{y}$') plt.legend(fontsize='xx-large', ncol=2) plt.show() 

This generates a figure like so: enter image description here

For a while, I was able to "fake it" a bit by adding some empty subscripts and superscripts, however this only works when the plot is exported to pdf. It does not appear to work when exporting to png. How can I spread out the first column of labels so that they line up with the second column?

like image 531
Samwise Avatar asked Feb 08 '17 01:02

Samwise


People also ask

How do I fix the legend position in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do you make a two column legend in Matlab?

MATLAB has introduced native support for multiple columns in legend from version 2018a. Just add 'NumColumns',desired_number at the end of the legend() command. Additionally, the orientation of the legend entries can be changed from top-to-bottom to left-to-right.

How do I change the order of items in Matplotlib legend?

Change order of items in the legend The above order of elements in the legend region can be changed by the gca method that uses another sub-method called get_legend_handles_labels method. These handles and labels lists are passed as parameters to legend method with order of indexes.


Video Answer


1 Answers

You could set the handleheight keyword argument to a number which is just large enough that the height of the handle is larger than the space taken by the font. This makes the text appear aligned. Doing so may require to set the labelspacing to a small number, in order not to make the legend appear too big.

plt.legend(fontsize='xx-large', ncol=2,handleheight=2.4, labelspacing=0.05) 

enter image description here

The drawback of this method, as can be seen in the picture, is that the lines shift upwards compared to the text's baseline. It would probably depent on the usage case if this is acceptable or not.

In case it is not, one needs to dig a little deeper. The following subclasses HandlerLine2D (which is the Handler for Lines) in order to set a slightly different position to the lines. Depending on the total legend size, font size etc. one would need to adapt the number xx in the SymHandler class.

from matplotlib.legend_handler import HandlerLine2D import matplotlib.lines  class SymHandler(HandlerLine2D):     def create_artists(self, legend, orig_handle,xdescent, ydescent, width, height, fontsize, trans):         xx= 0.6*height         return super(SymHandler, self).create_artists(legend, orig_handle,xdescent, xx, width, height, fontsize, trans)  leg = plt.legend(handler_map={matplotlib.lines.Line2D: SymHandler()},              fontsize='xx-large', ncol=2,handleheight=2.4, labelspacing=0.05)  

enter image description here

like image 154
ImportanceOfBeingErnest Avatar answered Sep 19 '22 21:09

ImportanceOfBeingErnest