You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PyGMT does not support displaying legends in multiple columns, which is usually used when the items to plot have a large number. For instance, the following script would show the legend in two columns.
I agree that it would be nice to be able to specify the number of columns directly via the Figure.legend method. As far as I know for auto-legends in GMT, the number of columns cannot be specified and it is always 1.
N 3
S 0.4c c 0.5 lightgreen 0.5p,black 1c Apples
S 0.4c - 0.5 gray 1.5p,gray 1c My Lines
S 0.4c t 0.5 orange 0.5p,black 1c Oranges
This file is passed to the spec parameter of the Figure.legend method. For legends with multiple columns, users have to state the width of the legend via the position parameter (+w). Code example:
I agree that it would be nice to be able to specify the number of columns directly via the Figure.legend method. As far as I know for auto-legends in GMT, the number of columns cannot be specified and it is always 1.
For auto legend, yes, only one column is supported I think.
There was a suggestion to allow fig.legend's spec parameter accept StringIO inputs (see #571) that would make it easier to generate these legend.txt files on the fly, and a working proof of concept at #576. We could potentially revive that if there's interest.
Activity
yvonnefroehlich commentedon Jul 12, 2023
I agree that it would be nice to be able to specify the number of columns directly via the
Figure.legendmethod. As far as I know for auto-legends in GMT, the number of columns cannot be specified and it is always 1.For a legend with multiple columns, users have to write a specific input file instead of using the
labelparameter of the single plotting methods. For details on the Legend Codes, please see the upstream GMT documentation at https://docs.generic-mapping-tools.org/6.4/legend.html#legend-codes. To get multiple columns, add N number_of_columns at the top of the file.Legend file: legend_horizontal.txt
This file is passed to the
specparameter of theFigure.legendmethod. For legends with multiple columns, users have to state the width of the legend via thepositionparameter (+w).Code example:
Output figure:

De-Guo commentedon Jul 13, 2023
Thanks,this works at present.
weiji14 commentedon Jul 13, 2023
For auto legend, yes, only one column is supported I think.
There was a suggestion to allow
fig.legend'sspecparameter accept StringIO inputs (see #571) that would make it easier to generate these legend.txt files on the fly, and a working proof of concept at #576. We could potentially revive that if there's interest.yvonnefroehlich commentedon Oct 20, 2023
I was wrong, it's possible to create multi-column legends for auto-legends. I though there should be a modifier for
legendto force a multi-column legend, but the specification has to be made together with the argument passed to thelabelparameter ofplot. (See also the GMT forum https://forum.generic-mapping-tools.org/t/horizontal-legend/4362/3?u=yvonnefroehlich.)Output figure:
