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 tutorial for cartesian histograms #2445

Merged
merged 79 commits into from
May 5, 2023
Merged
Changes from 65 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
ba09b90
Add basic for tutorial 'cartesian_histograms.py'
yvonnefroehlich Mar 20, 2023
e68a814
[format-command] fixes
actions-bot Mar 20, 2023
87ad648
Fix line length
yvonnefroehlich Mar 20, 2023
38e13cd
Expand content list and shorten formulations
Mar 21, 2023
8138610
Fix word order
Mar 21, 2023
5511845
Improve comment for pattern
Mar 21, 2023
7b38ea4
Fix typo
Mar 21, 2023
4ae16fc
Add docs for overlaid bars
Mar 21, 2023
a2f4dab
Add docs for stacked bars
Mar 21, 2023
d35c232
Improve formulation of content list (code review)
yvonnefroehlich Mar 21, 2023
d0beaf4
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 21, 2023
5b049d8
Fix typo
yvonnefroehlich Mar 21, 2023
98d593a
Expand limitations of grouped bars
yvonnefroehlich Mar 23, 2023
4f03c0c
Add section 'Counts and frequency percent'
yvonnefroehlich Mar 23, 2023
59c336c
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 23, 2023
3bee863
Fix format for section heading
Mar 23, 2023
9c0c8dd
Fix word order
Mar 23, 2023
a5c9bf6
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 25, 2023
bc723b9
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 27, 2023
a6a6acb
Follow documentation for 'region' parameter
yvonnefroehlich Mar 28, 2023
3684225
Adjust y-axis limit
yvonnefroehlich Mar 28, 2023
f4bf927
Add missing word
yvonnefroehlich Mar 28, 2023
fbc0a41
Fix typo
yvonnefroehlich Mar 28, 2023
0248f6a
Adjust y-axis limit
yvonnefroehlich Mar 28, 2023
7d5c8d0
Remove un-needed word
yvonnefroehlich Mar 28, 2023
62035b0
Improve formulation
yvonnefroehlich Mar 28, 2023
90d2482
Improve formulation for legend entry
yvonnefroehlich Mar 28, 2023
4ac02d8
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 28, 2023
30fcb38
Resort and restructure content in sections
yvonnefroehlich Mar 29, 2023
b6e57e5
Remove subplots
Mar 29, 2023
4a1f417
Add comments
Mar 29, 2023
2887355
Fix mixed up comments
yvonnefroehlich Mar 29, 2023
cd17f56
Add y axis label
yvonnefroehlich Mar 29, 2023
cba7a86
Fix frame
yvonnefroehlich Mar 29, 2023
9c734d8
Adjust code for outline color of bars
yvonnefroehlich Mar 29, 2023
04b391a
Adjust thumbnail_number
yvonnefroehlich Mar 30, 2023
720b385
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Mar 30, 2023
0dca603
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 1, 2023
5369e12
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 4, 2023
187047b
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 6, 2023
cbf49de
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 9, 2023
bc84f46
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 10, 2023
4c17ad9
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 12, 2023
9af1a5a
Replace 'subplot' by 'origin_shift'
yvonnefroehlich Apr 15, 2023
50193d5
Replace 'tabs' by four spaces
yvonnefroehlich Apr 15, 2023
0b4541b
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 15, 2023
a509296
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 19, 2023
d060285
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 22, 2023
7fbd7f5
Improve documentation
yvonnefroehlich Apr 22, 2023
e314775
Fix 'fig.shift' to 'fig.shift_origin'
yvonnefroehlich Apr 22, 2023
2bdf27f
Add comma (code review)
yvonnefroehlich Apr 24, 2023
84c4070
Add ':'
yvonnefroehlich Apr 24, 2023
4a3776d
Add "%" suffix at y-axis
yvonnefroehlich Apr 24, 2023
6920d47
Improve formulation 'anchor point' -> 'plotting origin' (code review)
yvonnefroehlich Apr 24, 2023
757a67c
Improve formulation 'anchor point' -> 'plotting origin' (code review)
yvonnefroehlich Apr 24, 2023
b8733b9
Remove un-needed 'region' and 'projection' parameters (code review)
yvonnefroehlich Apr 24, 2023
c5938cf
Improve comments
yvonnefroehlich Apr 24, 2023
2302448
Improve y range setting in 'region' parameter
yvonnefroehlich Apr 27, 2023
7013669
Add comma (code review)
yvonnefroehlich Apr 28, 2023
2174bfb
Fix line length
yvonnefroehlich Apr 28, 2023
6acddcd
Improve and clarify comment (code review)
yvonnefroehlich Apr 28, 2023
4e8610a
Reverte change of comment regarding y range
yvonnefroehlich Apr 28, 2023
6378261
Use 'numpy.random.normal' to generate random data from normal distrib…
yvonnefroehlich Apr 28, 2023
44e8d80
Fix coding style - add white spaces
yvonnefroehlich Apr 28, 2023
734296c
Use upper-case letters
yvonnefroehlich Apr 28, 2023
bb1ad60
Adjust building string for 'barwidth' argument
yvonnefroehlich Apr 29, 2023
ef3b151
Adjust building string for projection argument
yvonnefroehlich Apr 29, 2023
786273c
Remove 'r' at the beginning of the docstrings (code review)
yvonnefroehlich Apr 29, 2023
42922b4
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich Apr 29, 2023
ac6c3e0
Combined plots for vertical andh horizontal bars into one figure
yvonnefroehlich Apr 30, 2023
1cbb466
Add section for 'stair-steps'
yvonnefroehlich Apr 30, 2023
9ef9ac6
Adjust code and comments
yvonnefroehlich Apr 30, 2023
5dca5d4
Add missing code
yvonnefroehlich Apr 30, 2023
6f1eee9
Fix color order
yvonnefroehlich Apr 30, 2023
437fa16
Adjust frame parameter
yvonnefroehlich Apr 30, 2023
2b25d4a
Switch pen style for better visibility
yvonnefroehlich Apr 30, 2023
74ad392
Explain 'pen' parameter only once
yvonnefroehlich Apr 30, 2023
4e69442
Improve comments for stacked bars
yvonnefroehlich Apr 30, 2023
effd707
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich May 2, 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
338 changes: 338 additions & 0 deletions examples/tutorials/advanced/cartesian_histograms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
r"""
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
Cartesian histograms
====================

Cartesian histograms can be generated using the :meth:`pygmt.Figure.histogram`
method. In this tutorial, different histogram related aspects are addressed:

- Using vertical and horizontal bars
- Showing counts and frequency percent
- Adding annotations to the bars
- Showing cumulative values
- Using color and pattern as fill for the bars
- Using overlaid, stacked, and grouped bars
"""

# sphinx_gallery_thumbnail_number = 4


# Import the required packages
import numpy as np
import pygmt

###############################################################################
# Generate random data from a normal distribution:

np.random.seed(100)

# Mean of distribution
mean = 100
# Standard deviation of distribution
stddev = 20

# Create two data sets
data01 = np.random.normal(mean, stddev, 42)
data02 = np.random.normal(mean, stddev * 2, 42)


###############################################################################
# Vertical and horizontal bars
# ----------------------------
# To define the width of the bins, the ``series`` parameter has to be
# specified. The bars can be filled via the ``fill`` parameter with either a
# color or a pattern (see later in this tutorial). Use the ``pen`` parameter
# to adjust the outline of the bars.

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data01
fig.histogram(
# Define the plot range as a list of xmin, xmax, ymin, ymax
# Let ymin and ymax determined automatically by setting both to the same
# value
region=[0, 200, 0, 0],
projection="X10c", # Cartesian projection with a width of 10 centimeters
# Add frame, annotations (a), ticks (f), and y-axis label (+l) "Counts"
# The numbers give the steps of annotations and ticks
frame=["WSne", "xaf10", "ya1f1+lCounts"],
data=data01,
# Set the bin width via the "series" parameter
series=10,
# Fill the bars with color "red3"
fill="red3",
# Draw a darkgray 1-point thick outline around the bars via the "pen"
# parameter
pen="1p,darkgray",
# Choose counts via the "histtype" parameter
histtype=0,
)

fig.show()

###############################################################################
# By default, a histogram with vertical bars is created. Horizontal bars can
# be achieved via ``horizontal=True``.

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data01
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
frame=["WSne", "xaf10", "ya1f1+lCounts"],
data=data01,
series=10,
fill="red3",
pen="1p,darkgray",
histtype=0,
# Use horizontal bars
# Please note the flip of the x and y axes regarding annotations, ticks,
# gridlines, and axis labels
horizontal=True,
)

fig.show()


###############################################################################
# Counts and frequency percent
# ----------------------------
# By default, a histogram showing the counts in each bin is created
# (``histtype=0``). To show the frequency percent set the ``histtpye``
# parameter to ``1``. For further options please have a look at the
# documentation of :meth:`pygmt.Figure.histogram`.

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data02 showing counts
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
frame=["WSnr", "xaf10", "ya1f1+lCounts"],
data=data02,
series=10,
fill="orange",
pen="1p,darkgray",
# Choose counts via the "histtype" parameter
histtype=0,
)

# Shift plot origin 11 centimeters to the right
fig.shift_origin(xshift="11c")

# Create histogram for data02 showing frequency percent
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
# Add suffix % (+u)
frame=["lSnE", "xaf10", "ya2f1+u%+lFrequency percent"],
data=data02,
series=10,
fill="orange",
pen="1p,darkgray",
# Choose frequency percent via the "histtype" parameter
histtype=1,
)

fig.show()


###############################################################################
# Cumulative values
# -----------------
# To create a histogram showing the cumulative values set ``cumulative=True``.
# Here, the bars of the cumulative histogram are filled with a pattern via
# the ``fill`` parameter. Annotate each bar with the counts it represents
# using the ``annotate`` parameter.

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data01 showing the counts per bin
fig.histogram(
region=[0, 200, 0, len(data01) + 1],
projection="X10c",
frame=["WSne", "xaf10", "ya5f1+lCounts"],
data=data01,
series=10,
fill="red3",
pen="1p,darkgray",
histtype=0,
# Annotate each bar with the counts it represents
annotate=True,
)

# Shift plot origin 11 centimeters to the right
fig.shift_origin(xshift="11c")

# Create histogram for data01 showing the cumulative counts
fig.histogram(
region=[0, 200, 0, len(data01) + 1],
projection="X10c",
frame=["wSnE", "xaf10", "ya5f1+lCumulative counts"],
data=data01,
series=10,
# Use pattern (p) number 8 as fill for the bars
# Set the background (+b) to white [Default]
# Set the foreground (+f) to black [Default]
fill="p8+bwhite+fblack",
pen="1p,darkgray",
histtype=0,
# Show cumulative counts
cumulative=True,
# Offest (+o) the label by 10 points in negative y-direction
annotate="+o-10p",
)

fig.show()


###############################################################################
# Overlaid bars
# -------------
# Overlaid or overlapping bars can be achieved by plotting two or serveral
# histograms, each for one data set, on top of each other. The legend entry
# can be specified via the ``label`` parameter.
#
# Limitations of histograms with overlaid bars are:
#
# - Mixing of colors or/and patterns
# - Visually more colors or/and patterns than data sets
# - Visually a "third histogram" (or more in case of more than two data sets)
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data01
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
frame=["WSne", "xaf10", "ya1f1+lCounts"],
data=data01,
series=10,
fill="red3",
pen="1p,darkgray",
histtype=0,
# Set legend entry
label="data01",
)

# Create histogram for data02
# It is plotted on top of the histogram for data01
fig.histogram(
data=data02,
series=10,
# Fill bars with color "orange", use a transparency of 50% ("@50")
fill="orange@50",
pen="1p,darkgray",
histtype=0,
label="data02",
)

# Add legend
fig.legend()

fig.show()


###############################################################################
# Stacked bars
# ------------
# Stacked bars can be achieved similar to overlaid bars via plotting two or
# several histograms on top of each other. However, before plotting, combined
# data sets have to be created from the singel data sets.
#
# Limitations of histograms with stacked bars are:
#
# - No common baseline
# - Partly not directly clear whether overlaid or stacked bars

# Combine the two data sets to one data set
data_merge = np.concatenate((data01, data02), axis=None)
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data02 by using the combined data set
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
frame=["WSne", "xaf10", "ya1f1+lCounts"],
data=data_merge,
series=10,
fill="orange",
pen="1p,darkgray",
histtype=0,
label="data02",
)

# Create histogram for data01
# It is plotted on top of the histogram for data02
fig.histogram(
data=data01,
series=10,
fill="red3",
pen="1p,darkgray",
histtype=0,
label="data01",
)

# Add legend
fig.legend()

fig.show()


###############################################################################
# Grouped bars
# ------------
# By setting the ``barwidth`` parameter in respect to the values passed to the
# ``series`` parameter histograms with grouped bars can be created.
#
# Limitations of histograms with grouped bars are:
#
# - Careful setting width and position of the bars in respect to the bin width
# - Difficult to see the variations of the single data sets

# Width used for binning the data
binwidth = 10

# Create new figure instance
fig = pygmt.Figure()

# Create histogram for data01
fig.histogram(
region=[0, 200, 0, 0],
projection="X10c",
frame=["WSne", "xaf10g10", "ya1f1+lCounts"],
data=data01,
series=binwidth,
fill="red3",
pen="1p,darkgray",
histtype=0,
# Calculate the bar width in respect to the bin width, here for two
# data sets half of the bin width
# Offset (+o) the bars to align each bar with the left limit of the
# corresponding bin
barwidth=str(binwidth / 2) + "+o-" + str(binwidth / 4),
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
label="data01",
)

# Create histogram for data02
fig.histogram(
data=data02,
series=binwidth,
fill="orange",
pen="1p,darkgray",
histtype=0,
barwidth=str(binwidth / 2) + "+o" + str(binwidth / 4),
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
label="data02",
)

# Add legend
fig.legend()

fig.show()