Skip to content

Commit

Permalink
Add histogram gallery example (GenericMappingTools#1272)
Browse files Browse the repository at this point in the history
Gallery example of a histogram with red bars on a gray background.
Elevation counts are binned at 5m intervals, and the data is
generated from a normal distribution with random noise added.

Co-authored-by: Dongdong Tian <[email protected]>
Co-authored-by: Wei Ji <[email protected]>
Co-authored-by: Will Schlitzer <[email protected]>
  • Loading branch information
4 people authored and Josh Sixsmith committed Dec 21, 2022
1 parent 31dd042 commit 3266e4c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/gallery/histograms/histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Histogram
---------
The :meth:`pygmt.Figure.histogram` method can plot regular histograms.
Using the ``series`` parameter allows to set the interval for the width of
each bar. The type of histogram (frequency count or percentage) can be
selected via the ``histtype`` parameter.
"""

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()

fig.histogram(
table=data,
# define the frame, add title and set background color to
# lightgray, add annotations for x and y axis
frame=['WSne+t"Histogram"+glightgray', 'x+l"Elevation (m)"', 'y+l"Counts"'],
# 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 1p to draw the outlines
pen="1p",
# choose histogram type 0 = counts [default]
histtype=0,
)

fig.show()

0 comments on commit 3266e4c

Please sign in to comment.