Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
weiji14 opened this issue Jan 13, 2023 · 5 comments · Fixed by #2329
Open

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

weiji14 opened this issue Jan 13, 2023 · 5 comments · Fixed by #2329
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@weiji14
Copy link
Member

weiji14 commented Jan 13, 2023

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

@weiji14 weiji14 added good first issue Good for newcomers documentation Improvements or additions to documentation help wanted Helping hands are appreciated labels Jan 13, 2023
@yvonnefroehlich
Copy link
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

@seisman
Copy link
Member

seisman commented Jan 15, 2023

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.

@yvonnefroehlich
Copy link
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.

@jhtong33
Copy link
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

@weiji14
Copy link
Member Author

weiji14 commented Mar 14, 2023

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.

@weiji14 weiji14 reopened this Mar 14, 2023
@weiji14 weiji14 added this to the 0.10.0 milestone Mar 14, 2023
@yvonnefroehlich yvonnefroehlich removed the help wanted Helping hands are appreciated label Mar 23, 2023
@weiji14 weiji14 modified the milestones: 0.10.0, 0.11.0 Aug 24, 2023
@seisman seisman modified the milestones: 0.11.0, 0.12.0 Dec 11, 2023
@seisman seisman removed this from the 0.12.0 milestone Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
4 participants