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
{{ message }}
Repository navigation
Suggestion for improvement of gallery examples regarding categorical colormaps #1933
The data (csv file) is loaded into a pandas dataframe and the column of interest is converted to categorical data type. A discrete colormap in categorical format is set up via the pygmt.makecpt() function (+c through color_model). For plotting the symbols with the corresponding fill color the categorical number code is passed through the color parameter of the pygmt.Figure.plot() method.
I recommend to clarify that by default the labels for the colorbar must be given in alphabetical order. If one wants labels in non-alphabetical order, e. g. increasing or decreasing order for displaying categorical data with order, the categorical number code has to be adjusted by the user. For instance, to get 'poor, fair, good' instead of 'fair, good, poor' (dataframe 'df' with column 'quality'): df.quality = df.quality.cat.set_categories(['poor','fair','good'], ordered = True)
The label order (comma-separated list) passed through color_model in the pygmt.makecpt() function has no effect on the plotting itself, but affects only the colorbar labels.
As pointed out by @yvonnefroehlich, the labels must be given in alphabetical order because when converting df.species to categorical dtype, the default order is alphabetical (e.g., 0-Adelie, 1-Chinstrap,2-Gentoo). So the examples have many limitations and are difficult to be re-used by users:
Users must know the labels Adelie,Chinstrap,Gentoo
The labels Adelie,Chinstrap,Gentoo must be given in alphabetical order in this case
Users must know the minimum and maximum values of categorical data (0 and 2 in this case).
So, instead of hard-coding the numbers and values in the makecpt call, we can rewrite it to:
I already started working on this issue in PR #1934. But until now I only added a remark or warning regarding the
alphabetical order. I can continue with this and change the code as suggested by @seisman.
Activity
seisman commentedon May 27, 2022
@yvonnefroehlich Nice catch!
I'm not familiar with pandas's category type, but looking at the two gallery examples, I think both examples should be improved.
I'll take Color points by categories as an example. Here are the two important lines in the example:
As pointed out by @yvonnefroehlich, the labels must be given in alphabetical order because when converting
df.speciesto categorical dtype, the default order is alphabetical (e.g., 0-Adelie, 1-Chinstrap,2-Gentoo). So the examples have many limitations and are difficult to be re-used by users:Adelie,Chinstrap,GentooAdelie,Chinstrap,Gentoomust be given in alphabetical order in this caseSo, instead of hard-coding the numbers and values in the
makecptcall, we can rewrite it to:yvonnefroehlich commentedon May 27, 2022
I already started working on this issue in PR #1934. But until now I only added a remark or warning regarding the
alphabetical order. I can continue with this and change the code as suggested by @seisman.