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

Add a gallery example showing how to use patterns via the "fill" parameter (or similar parameters) #2329

Merged
merged 45 commits into from
Mar 14, 2023
Merged
Changes from 34 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
54abdee
Add code for gallery example 'patterns.py'
yvonnefroehlich Jan 17, 2023
d88691a
Fix typos in comments
yvonnefroehlich Jan 17, 2023
e0ad0ad
Set 644 permission
yvonnefroehlich Jan 17, 2023
08c8ce3
Use UNIX line breaks
yvonnefroehlich Jan 17, 2023
1082c13
Show various patterns
yvonnefroehlich Jan 18, 2023
bd46172
Remove general pattern comment above loop
yvonnefroehlich Jan 18, 2023
867de72
Adjust size of figure
yvonnefroehlich Jan 18, 2023
5e4f4e1
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Jan 22, 2023
e9de513
Fix typo in comment
yvonnefroehlich Jan 25, 2023
50e3eb4
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Jan 25, 2023
01b7358
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Jan 29, 2023
cc7119f
Add link to the pattern overview in the GMT documentation
yvonnefroehlich Jan 29, 2023
1a061da
Add documentation
yvonnefroehlich Jan 29, 2023
6a75bb4
Fix line length
yvonnefroehlich Jan 29, 2023
eb31926
Add documentation
yvonnefroehlich Jan 29, 2023
c85043a
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Jan 31, 2023
25c3777
Update 'E and 'G' of 'coast' to 'extensionfill' and 'compressionfill'
yvonnefroehlich Feb 2, 2023
a5fd42f
Add 'pygmt.Figure.velo' to list
yvonnefroehlich Feb 2, 2023
c2c0015
Add 'pygmt.Figure.solar' to list
yvonnefroehlich Feb 2, 2023
fd0386d
Sort items in list alphabetically
yvonnefroehlich Feb 2, 2023
92caaf3
Improve formulation
yvonnefroehlich Feb 2, 2023
567b555
Sort items in list alphabetically
yvonnefroehlich Feb 2, 2023
b011799
Restructure list
yvonnefroehlich Feb 2, 2023
7ef4aae
Sort items in list alphabetically
yvonnefroehlich Feb 2, 2023
b8187d1
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 4, 2023
1b04163
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 7, 2023
ea07668
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 9, 2023
33477bb
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 11, 2023
f147e2a
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 19, 2023
4bb71b9
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Feb 26, 2023
77816a9
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 3, 2023
7f6d413
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 5, 2023
fc58d96
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 8, 2023
73c79e7
Add basic docs
yvonnefroehlich Mar 10, 2023
e560c58
Fix typo
yvonnefroehlich Mar 10, 2023
d6452ba
Improve formulation (code review)
yvonnefroehlich Mar 10, 2023
c34b850
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 11, 2023
18cf6be
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 11, 2023
8ffbb74
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 12, 2023
bc6c7ba
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 12, 2023
ba22af4
Fix general syntax for pattern (code review)
yvonnefroehlich Mar 13, 2023
4434c07
Move interation to end of loop (code review)
yvonnefroehlich Mar 13, 2023
7b8cffb
Merge branch 'main' into add-gallery-pattern
yvonnefroehlich Mar 13, 2023
e2b520e
Split syntax for pattern in two lines to fit line length
yvonnefroehlich Mar 13, 2023
0518f67
Shorten syntax for pattern to fit line length (code review)
yvonnefroehlich Mar 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/gallery/symbols/patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
r"""
Bit and Hachure Patterns
------------------------

PyGMT allows using bit or hachure patterns via the ``fill`` parameter
or similar parameters:

- :meth:`pygmt.Figure.coast`: Land and water masses via ``land`` and
``water``, respectively
- :meth:`pygmt.Figure.histogram`: Histogram bars via ``fill``
- :meth:`pygmt.Figure.meca`: Focal mechanisms via ``compressionfill``
and ``extensionfill``
- :meth:`pygmt.Figure.plot`: Symbols and polygons via ``fill``
- :meth:`pygmt.Figure.rose`: Histogram sectors via ``fill``
- :meth:`pygmt.Figure.solar`: Day-light terminators via ``fill``
- :meth:`pygmt.Figure.ternary`: Symbols via ``fill``
- :meth:`pygmt.Figure.velo`: Uncertainty wedges and velocity error
ellipses via ``uncertaintyfill``
- :meth:`pygmt.Figure.wiggle`: Anomalies via ``fillpositive``
and ``fillnegative``

The required argument has the following form:

**P**\ |**p**\ *pattern*\ [**+b**\ *color*\ ][**+f**\ *color*\ ][**+r**\ *dpi*]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should define a class for GMT patterns, e.g.,

class GMTPattern:
    def __init__(self, id, reversed=False, bgcolor="white", fgcolor="black", dpi=1200):
        ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional to the list at the beginning of this example, there is also the possibility to add a pattern via the dcw and frame parameters. The syntax seems to be difficult, as it seems to be something like +g p|P resolution / pattern. The functionality seems to be limited, as it seems that no foreground and background colors could be given (please correct me if I am wrong, so far I did not figure out how to set colors here).

Example code

import pygmt

fig = pygmt.Figure()

with fig.subplot(nrows=1, ncols=2, figsize=("10c", "5c")):

    with fig.set_panel(panel=0):
        fig.basemap(
            region=[-12, 50, 30, 70],
            projection="M?",
            frame="afg",
        )
        fig.coast(
            land="lightbrown",
            water="lightblue",
            shorelines="1/0.5p,black",
            borders="1/0.25p,red",
            dcw="=EU+gp200/8",  # here resolution/pattern 
        )        

    with fig.set_panel(panel=1):
        fig.basemap(
            region=[0, 10, 0, 10],
            projection="X?",
            frame=["afg", "+gp400/8"],  # here resolution/pattern
        )

fig.show()
# fig.savefig(fname="pattern_dcw_frame.png")

Output figure
pattern_dcw_frame

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax seems to be difficult, as it seems to be something like +g p|P resolution / pattern.

This is the old syntax of patterns for GMT 4. The full old syntax is -Gpdpi/pattern[:Bcolor[Fcolor]]. I think the old syntax is still supported in GMT5 and GMT6 (backward compatibility), but it's no longer documented.

The functionality seems to be limited, as it seems that no foreground and background colors could be given.

As I understand it, it's impossible to specify foreground/background colors and dpi using the new syntax in coast -E.

Ping @PaulWessel for comments on this.

Copy link
Member Author

@yvonnefroehlich yvonnefroehlich Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should define a class for GMT patterns, e.g.,

class GMTPattern:
    def __init__(self, id, reversed=False, bgcolor="white", fgcolor="black", dpi=1200):
        ...

Maybe we should we open a separate issue for this?
Edit 2023/03/17: Please see issue #2438.

yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved

*pattern* can either be a number in the range 1–90 or the name of a
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
1-, 8-, or 24-bit image raster file. The former will result in one of the 90
predefined 64 x 64 bit-patterns provided with GMT; an overview can by found at
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
https://docs.generic-mapping-tools.org/latest/cookbook/predefined-patterns.html.
The latter allows the user to create customized, repeating images using image
raster files.
By specifying upper case **P** instead of **p** the image will be
bit-reversed, i.e., white and black areas will be interchanged (only applies
to 1-bit images or predefined bit-image patterns).
For these patterns and other 1-bit images one may specify alternative
**b**\ ackground and **f**\ oreground colors (by appending **+b**\ *color*
and/or **+f**\ *color*) that will replace the default white and black pixels,
respectively. Excluding *color* from a fore- or background specification yields
a transparent image where only the back- or foreground pixels will be painted.
The **+r**\ *dpi* modifier sets the resolution in dpi.
"""

import pygmt

y = 13

yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
fig = pygmt.Figure()
fig.basemap(
region=[0, 10, 0, 12],
projection="X10c",
frame="rlbt+glightgray+tBit and Hachure Patterns",
)

# To use a pattern as fill append "p" and the number of the desired
# pattern. By default, the pattern is plotted in black and white
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
# with a resolution of 300 dpi
for pattern in [
# Plot a hachted pattern via pattern number 8
"p8",
# Plot a dotted pattern via pattern number 19
"p19",
# Set the background color ("+b") to "red3"
# and the foreground color ("+f") to "lightgray"
"p19+bred3+flightbrown",
# Invert the pattern by using a capitalized "P"
"P19+bred3+flightbrown",
# Change the resolution ("+r") to 100 dpi
"p19+bred3+flightbrown+r100",
# Make the background transparent by not giving a color after "+b";
# works analogous for the foreground
"p19+b+flightbrown+r100",
]:
y -= 2
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
# Plot a square with the pattern as fill
fig.plot(
x=2,
y=y,
style="s2c", # square with a width of 2 centimeters
pen="1p,black", # 1 point thick, black outline
fill=pattern,
)
# Add a description of the pattern
fig.text(
x=4,
y=y,
text=pattern,
font="Courier-Bold",
justify="ML", # justification of the text is Middle Left
)
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved

fig.show()