-
Notifications
You must be signed in to change notification settings - Fork 228
Add tutorial for cartesian histograms #2445
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
Merged
Merged
Changes from all 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 e68a814
[format-command] fixes
actions-bot 87ad648
Fix line length
yvonnefroehlich 38e13cd
Expand content list and shorten formulations
8138610
Fix word order
5511845
Improve comment for pattern
7b38ea4
Fix typo
4ae16fc
Add docs for overlaid bars
a2f4dab
Add docs for stacked bars
d35c232
Improve formulation of content list (code review)
yvonnefroehlich d0beaf4
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 5b049d8
Fix typo
yvonnefroehlich 98d593a
Expand limitations of grouped bars
yvonnefroehlich 4f03c0c
Add section 'Counts and frequency percent'
yvonnefroehlich 59c336c
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 3bee863
Fix format for section heading
9c0c8dd
Fix word order
a5c9bf6
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich bc723b9
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich a6a6acb
Follow documentation for 'region' parameter
yvonnefroehlich 3684225
Adjust y-axis limit
yvonnefroehlich f4bf927
Add missing word
yvonnefroehlich fbc0a41
Fix typo
yvonnefroehlich 0248f6a
Adjust y-axis limit
yvonnefroehlich 7d5c8d0
Remove un-needed word
yvonnefroehlich 62035b0
Improve formulation
yvonnefroehlich 90d2482
Improve formulation for legend entry
yvonnefroehlich 4ac02d8
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 30fcb38
Resort and restructure content in sections
yvonnefroehlich b6e57e5
Remove subplots
4a1f417
Add comments
2887355
Fix mixed up comments
yvonnefroehlich cd17f56
Add y axis label
yvonnefroehlich cba7a86
Fix frame
yvonnefroehlich 9c734d8
Adjust code for outline color of bars
yvonnefroehlich 04b391a
Adjust thumbnail_number
yvonnefroehlich 720b385
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 0dca603
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 5369e12
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 187047b
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich cbf49de
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich bc84f46
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 4c17ad9
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 9af1a5a
Replace 'subplot' by 'origin_shift'
yvonnefroehlich 50193d5
Replace 'tabs' by four spaces
yvonnefroehlich 0b4541b
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich a509296
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich d060285
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich 7fbd7f5
Improve documentation
yvonnefroehlich e314775
Fix 'fig.shift' to 'fig.shift_origin'
yvonnefroehlich 2bdf27f
Add comma (code review)
yvonnefroehlich 84c4070
Add ':'
yvonnefroehlich 4a3776d
Add "%" suffix at y-axis
yvonnefroehlich 6920d47
Improve formulation 'anchor point' -> 'plotting origin' (code review)
yvonnefroehlich 757a67c
Improve formulation 'anchor point' -> 'plotting origin' (code review)
yvonnefroehlich b8733b9
Remove un-needed 'region' and 'projection' parameters (code review)
yvonnefroehlich c5938cf
Improve comments
yvonnefroehlich 2302448
Improve y range setting in 'region' parameter
yvonnefroehlich 7013669
Add comma (code review)
yvonnefroehlich 2174bfb
Fix line length
yvonnefroehlich 6acddcd
Improve and clarify comment (code review)
yvonnefroehlich 4e8610a
Reverte change of comment regarding y range
yvonnefroehlich 6378261
Use 'numpy.random.normal' to generate random data from normal distrib…
yvonnefroehlich 44e8d80
Fix coding style - add white spaces
yvonnefroehlich 734296c
Use upper-case letters
yvonnefroehlich bb1ad60
Adjust building string for 'barwidth' argument
yvonnefroehlich ef3b151
Adjust building string for projection argument
yvonnefroehlich 786273c
Remove 'r' at the beginning of the docstrings (code review)
yvonnefroehlich 42922b4
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich ac6c3e0
Combined plots for vertical andh horizontal bars into one figure
yvonnefroehlich 1cbb466
Add section for 'stair-steps'
yvonnefroehlich 9ef9ac6
Adjust code and comments
yvonnefroehlich 5dca5d4
Add missing code
yvonnefroehlich 6f1eee9
Fix color order
yvonnefroehlich 437fa16
Adjust frame parameter
yvonnefroehlich 2b25d4a
Switch pen style for better visibility
yvonnefroehlich 74ad392
Explain 'pen' parameter only once
yvonnefroehlich 4e69442
Improve comments for stacked bars
yvonnefroehlich effd707
Merge branch 'main' into add-tutorial-histogram
yvonnefroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,379 @@ | ||
""" | ||
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 | ||
- Using stair-steps | ||
- 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 width, color, and style of the outlines. 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 with vertical bars | ||
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=["WStr", "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 1-point thick solid outline in "darkgray" around the bars | ||
pen="1p,darkgray,solid", | ||
# Choose counts via the "histtype" parameter | ||
histtype=0, | ||
) | ||
|
||
# Shift plot origin 12 centimeters to the right | ||
fig.shift_origin(xshift="12c") | ||
|
||
# Create histogram for data01 with horizontal bars | ||
fig.histogram( | ||
region=[0, 200, 0, 0], | ||
projection="X10c", | ||
frame=["WStr", "xaf10", "ya1f1+lCounts"], | ||
data=data01, | ||
series=10, | ||
fill="red3", | ||
pen="1p,darkgray,solid", | ||
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() | ||
|
||
|
||
############################################################################### | ||
# Stair-steps | ||
# ----------- | ||
# A stair-step diagram can be created by setting ``stairs=True``. Then only | ||
# the outer outlines of the bars are drawn, and no internal bars are visible. | ||
|
||
# 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, | ||
# Draw a 1-point thick dotted outline in "red3" | ||
pen="1p,red3,dotted", | ||
histtype=0, | ||
# Draw stair-steps in stead of bars | ||
stairs=True, | ||
) | ||
|
||
# Shift plot origin 12 centimeters to the right | ||
fig.shift_origin(xshift="12c") | ||
|
||
# Create histogram for data02 | ||
fig.histogram( | ||
region=[0, 200, 0, 0], | ||
projection="X10c", | ||
frame=["WSne", "xaf10", "ya1f1+lCounts"], | ||
data=data02, | ||
series=10, | ||
# Draw a 1.5-point thick dashed outline in "orange" | ||
pen="1.5p,orange,dashed", | ||
histtype=0, | ||
stairs=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,solid", | ||
# 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,solid", | ||
# 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,solid", | ||
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,solid", | ||
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) | ||
|
||
# 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,solid", | ||
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,solid", | ||
histtype=0, | ||
label="data02", | ||
) | ||
|
||
# Add legend | ||
fig.legend() | ||
|
||
fig.show() | ||
|
||
|
||
############################################################################### | ||
# Stacked bars | ||
# ------------ | ||
# Histograms with stacked bars are not directly supported by PyGMT. Thus, | ||
# before plotting, combined data sets have to be created from the single data | ||
# sets. Then, stacked bars can be achieved similar to overlaid bars via | ||
# plotting two or several histograms on top of each other. | ||
# | ||
# 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,solid", | ||
histtype=0, | ||
# The combined data set appears in the final histogram visually | ||
# as data set data02 | ||
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,solid", | ||
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,solid", | ||
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=f"{binwidth/2}+o-{binwidth/4}", | ||
label="data01", | ||
) | ||
|
||
# Create histogram for data02 | ||
fig.histogram( | ||
data=data02, | ||
series=binwidth, | ||
fill="orange", | ||
pen="1p,darkgray,solid", | ||
histtype=0, | ||
barwidth=f"{binwidth/2}+o{binwidth/4}", | ||
label="data02", | ||
) | ||
|
||
# Add legend | ||
fig.legend() | ||
|
||
fig.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.