Skip to content

Suggestion for improvement of gallery examples regarding categorical colormaps #1933

Closed

Description

@yvonnefroehlich

In two gallery examples categorical data (without order) is displayed by using color-coding for the categories:

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.

Activity

  1. added
    documentation
    on May 25, 2022
  2. seisman commented on May 27, 2022

    @seisman
    Member

    @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:

    df.species = df.species.astyp(dtype="category")
    pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+cAdelie,Chinstrap,Gentoo")
    

    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:

    1. Users must know the labels Adelie,Chinstrap,Gentoo
    2. The labels Adelie,Chinstrap,Gentoo must be given in alphabetical order in this case
    3. 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:

    df.species = df.species.astyp(dtype="category")
    
    labels = list(df.species.cat.categories)
    pygmt.makecpt(cmap="inferno", series=(df.species.cat.codes.min(), df.species.cat.codes.min(), 1), color_model="+c" + ",".join(labels))
    
  3. added this to the 0.7.0 milestone on May 27, 2022
  4. yvonnefroehlich commented on May 27, 2022

    @yvonnefroehlich
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions