Skip to content

Gallery example for histogram/plot showing bit and hachure patterns #2323

Description

@weiji14

Description of the desired feature

Some functions like histogram and plot have a -Gp or -GP option (in PyGMT, -G is aliased to fill after #1617) that sets bit and hachure patterns like so:

Predefined Bit and Hachure Patterns in GMT

@JingHuiTong suggested during the AGU 2022 Fall Meeting that there could be a gallery example for this, showing how to create e.g. histogram bars with these fill patterns, to be placed under https://www.pygmt.org/v0.8.0/gallery/index.html#histograms

Extends #1156.

References:

Are you willing to help implement and maintain this feature?

Maybe

Related PRs and issues

Activity

  1. added
    good first issue
    documentation
    help wanted
    on Jan 13, 2023
  2. yvonnefroehlich commented on Jan 14, 2023

    @yvonnefroehlich
    Member

    I agree, that this would be a good addition to the gallery! Maybe we can split this into two parts:

    1. A separate gallery example showing how to add a pattern as fill, as this (see the GMT documentation https://docs.generic-mapping-tools.org/6.4/cookbook/features.html#specifying-area-fill-attributes) is currently missing in the PyGMT documentation for fill (e.g., https://www.pygmt.org/dev/api/generated/pygmt.Figure.plot.html#pygmt.Figure.plot)
    2. Expand the existing gallery example for histogram (https://www.pygmt.org/dev/gallery/histograms/histogram.html)

    Here are two rough examples, which maybe can serve as an orientation or starting point:

    1. Gallery example for pattern as fill

    import pygmt
    
    y = 13
    
    fig = pygmt.Figure()
    
    fig.basemap(
        region=[0, 12, 0, 12],
        projection="X12c",
        frame="lrtb",
    )
      
    for pattern in [
        # pattern number 3
        "p3",
        # pattern number 19
        "p19",
        # pattern number 32
        "p32", 
        # pattern number 23
        "p23",
        # pattern number 23
        # with "red" foreground color (+f) and "yellow" background color (+b)
        "p23+fred+byellow",
        # pattern number 23
        # as before but foreground and backround colors inverted
        "P23+fred+byellow",
    ]:
        
        y -= 2
        
        # Plot square with pattern as fill
        fig.plot(
            x=2,
            y=y,
            style="s2c",
            pen="1p,black",
            fill=pattern,
        )
    
        # Add text description
        fig.text(
            x=4,
            y=y,
            text=pattern,
            font="Courier-Bold",
            justify="ML",
        )
    
    fig.show()
    # fig.savefig(fname="fill_pattern.png")

    Output figure:
    fill_pattern

    2) Expand exisiting gallery example for histogram

    # source: https://www.pygmt.org/dev/gallery/histograms/histogram.html
    # last access: 2023-01-14
    # modified: subplot with second panel using pattern as fill
    
    import numpy as np
    import pygmt
    
    np.random.seed(100)
    
    # Generate random elevation data from a normal distribution
    mean = 100  # mean of distribution
    stddev = 25  # standard deviation of distribution
    data = mean + stddev * np.random.randn(521)
    
    fig = pygmt.Figure()
    
    with fig.subplot(
        nrows=1, ncols=2, figsize=("13.5c", "5c"), title="Histogram",
    ):
            
        with fig.set_panel(panel=0):        
            fig.histogram(
                data=data,
                # Define the frame, add title and set background color to
                # "lightgray", add labels for x and y axes
                frame=["WSne+glightgray", "x+lElevation in m", "y+lCounts"],
                # Generate evenly spaced bins by increments of 5
                series=5,
                # Use "red3" as color fill for the bars
                fill="red3",
                # Use a pen size of 1 point to draw the outlines
                pen="1p",
                # Choose histogram type 0, i.e., counts [Default]
                histtype=0,
            )
            
        with fig.set_panel(panel=1):
            fig.histogram(
                data=data,
                frame=["wSne+glightgray", "x+lElevation in m"],
                series=5,
                # Use pattern number 8 as fill for the bars
                # with "lightblue" as background color (+b) and
                # "black" as foreground color (+f)
                fill="p8+blightblue+fblack",
                pen="1p",
                histtype=0,
            )
    
    fig.show()
    # fig.savefig(fname="histogram_pattern.png")

    Output figure:
    histogram_pattern

  3. seisman commented on Jan 15, 2023

    @seisman
    Member

    Some functions like histogram and plot have a -Gp or -GP option (in PyGMT, -G is aliased to fill after #1617) that sets bit and hachure patterns

    This statement is inaccurate. I believe any functions that accept fill as an argument (e.g., coast's -G and -S) can use bit and hachure patterns.

    Maybe we can split this into two parts:

    1. A separate gallery example showing how to add a pattern as fill, as this (see the GMT documentation docs.generic-mapping-tools.org/6.4/cookbook/features.html#specifying-area-fill-attributes) is currently missing in the PyGMT documentation for fill (e.g., pygmt.org/dev/api/generated/pygmt.Figure.plot.html#pygmt.Figure.plot)
    2. Expand the existing gallery example for histogram (pygmt.org/dev/gallery/histograms/histogram.html)

    I agree with part 1, but for part 2, I don't think it's necessary. Instead, we may need to have a tutorial to explain what fill GMT/PyGMT can accept, including color names, RGB/HSV/CMYK/gray values, and these patterns.

  4. yvonnefroehlich commented on Jan 15, 2023

    @yvonnefroehlich
    Member

    Some functions like histogram and plot have a -Gp or -GP option (in PyGMT, -G is aliased to fill after #1617) that sets bit and hachure patterns

    This statement is inaccurate. I believe any functions that accept fill as an argument (e.g., coast's -G and -S) can use bit and hachure patterns.

    Yes, several PyGMT methods (GMT modules) can use fill or related parameters with a pattern, e.g.:

    • pygmt.Figure.plot: fill
    • pygmt.Figure.histogram: fill
    • pygmt.Figure.coast: land, water
    • pygmt.Figure.wiggle: fillpositive , fillnegative
    • pygmt.Figure.rose: fill (does NOT work for +r in sector)
    • pygmt.Figure.meca: G, E

    fill_pattern_others

    Click to show code

    import numpy as np
    import pygmt
    
    
    fig = pygmt.Figure()
    
    with fig.subplot(
        nrows=2, ncols=3, figsize=("22.5c", "15c"), title="Pattern as fill",
    ):
    
        with fig.set_panel(panel=0):
            # after: https://www.pygmt.org/dev/gallery/histograms/histogram.html
            # last access: 2022/01/15
            np.random.seed(100)
            stddev = 25  # standard deviation of distribution
            mean = 100  # mean of distribution
            data = mean + stddev * np.random.randn(521)
    
            fig.histogram(
                data=data,
                frame="WSne+glightgray",
                series=10,
                fill="p8+blightblue+fblack",
                pen="1p",
                histtype=0,
            )
    
        with fig.set_panel(panel=1):
            fig.basemap(
                region="d",
                projection="W?",
                frame="afg",
            )
            fig.coast(
                land="p23+blightbrown",  # -G dry areas
                water="p31+blightblue",  # -S wet areas
                shorelines="1/0.5p,black",
            )
    
        with fig.set_panel(panel=2):
            # after: https://www.pygmt.org/dev/gallery/lines/wiggle.html
            # last access: 2023/01/15
            x = np.arange(-10, 8, 0.1)
            y = np.zeros(x.size)
            z = np.cos(0.5 * np.pi * x) * 50
    
            fig.basemap(
                region=[-8, 12, -1, 1],
                projection="X?",
                frame=["Snlr", "xa2f1"],
            )
            fig.wiggle(
                x=x,
                y=y,
                z=z,
                scale="20c",
                fillpositive="p19+bred3+flightgray",
                fillnegative="p19+bred3+flightgray+r100",
                pen="1.0p",
                track="1.0p,blue",
                position="jRM+w100",
            )
    
        with fig.set_panel(panel=3):
            # after: https://www.pygmt.org/dev/gallery/histograms/rose.html
            # last access: 2023/01/15
            data = pygmt.datasets.load_sample_data(name="fractures")
    
            fig.rose(
                length=data.length,
                azimuth=data.azimuth,
                region=[0, 1, 0, 360],
                diameter="?",
                sector="10",  # pattern does not work with +r
                norm=True,
                fill="p32+blightblue",
                frame=["x0.2g0.2", "y30g30"],# "+gp18"],
                pen="1p",
                labels="W,E,S,N",
            )
    
        # with fig.set_panel(panel=4):
        #     fig.basemap(
        #         region=[-12, 50, 30, 70],
        #         projection="M?",
        #         frame="afg",
        #     )
        #     # TODO: Currently not clear why shift of dcw ???
        #     fig.coast(
        #         land="lightgray",
        #         water="lightblue",
        #         shorelines="1/0.5p,black",
        #         borders="1/0.25p,red",
        #         dcw="=EU+gP15+p0.25p,darkred",
        #     )
    
        with fig.set_panel(panel=5):
            focal_mechanism = dict(strike=330, dip=30, rake=90, magnitude=5)
    
            fig.basemap(
                region=[0, 10, 0, 10],
                projection="X?",
                frame="rltb",
            )
            fig.meca(
                spec=focal_mechanism,
                scale="6c",
                longitude=5,
                latitude=5,
                depth=10.0,
                G="p8+r100",
                E="p31",
                L="1p,black",
            )
    
    fig.show()
    # fig.savefig(fname="fill_pattern_others.png")

    Maybe we can split this into two parts:

    1. A separate gallery example showing how to add a pattern as fill, as this (see the GMT documentation docs.generic-mapping-tools.org/6.4/cookbook/features.html#specifying-area-fill-attributes) is currently missing in the PyGMT documentation for fill (e.g., pygmt.org/dev/api/generated/pygmt.Figure.plot.html#pygmt.Figure.plot)
    2. Expand the existing gallery example for histogram (pygmt.org/dev/gallery/histograms/histogram.html)

    I agree with part 1, but for part 2, I don't think it's necessary. Instead, we may need to have a tutorial to explain what fill GMT/PyGMT can accept, including color names, RGB/HSV/CMYK/gray values, and these patterns.

    I can work on part 1.
    A tutorial is probably a more general way to explain the "functionality" of fill, which indirectly addresses all related methods. Maybe pygmt.Figure.histogram was only exemplary suggested, because bars of histograms are often filled with pattern.

  5. jhtong33 commented on Jan 16, 2023

    @jhtong33
    Contributor

    I think pygmt.Figure.histogram could be one of examples to show how to use fill.
    Because when I used 2 colors (red, blue)with transparency, it looks like 3 colors (red, blue, purple) in a figure.
    That's why I suggested the histogram bars with these fill patterns will be more clearly.
    Maybe it can insert into part1.
    Surly, the function still based on users how to apply.

    some examples I plotted before.
    image
    image

  6. weiji14 commented on Mar 14, 2023

    @weiji14
    MemberAuthor

    Reopening because the histogram example mentioned in #2323 (comment) hasn't been done yet, and we might want to add a full tutorial for bit/hachure patterns too.

  7. added this to the 0.10.0 milestone on Mar 14, 2023
  8. removed
    help wanted
    on Mar 23, 2023
  9. modified the milestones: 0.10.0, 0.11.0 on Aug 24, 2023
  10. modified the milestones: 0.11.0, 0.12.0 on Dec 11, 2023
  11. removed this from the 0.12.0 milestone on Feb 26, 2024
  12. seisman commented on Sep 24, 2025

    @seisman
    Member

    What's the status of this issue? Can it be closed after #4020 is merged?

  13. added this to the 0.17.0 milestone on Sep 24, 2025
  14. removed
    good first issue
    on Sep 25, 2025
  15. yvonnefroehlich commented on Sep 28, 2025

    @yvonnefroehlich
    Member

    What's the status of this issue? Can it be closed after #4020 is merged?

    I think there was the idea to have another gallery example to show the effect of plotting two histograms on top of each other when using semi-transparent colors or patterns (see comment #2323 (comment)). But I think such an example is not essentially required.

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