Skip to content
51 changes: 36 additions & 15 deletions examples/gallery/histograms/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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 the histogram (frequency count or percentage) can be
selected via the ``histtype`` parameter.
selected via the ``histtype`` parameter. The ``fill`` of the
bars can be either a color or a pattern.
"""

import numpy as np
Expand All @@ -19,19 +20,39 @@

fig = pygmt.Figure()

fig.histogram(
data=data,
# Define the frame, add a title, and set the background color to
# "lightgray". Add labels to the x-axis and y-axis
frame=["WSne+tHistogram+glightgray", "x+lElevation (m)", "y+lCounts"],
# Generate evenly spaced bins by increments of 5
series=5,
# Use "red3" as color fill for the bars
fill="red3",
# Use the pen parameter to draw the outlines with a width of 1 point
pen="1p",
# Choose histogram type 0, i.e., counts [Default]
histtype=0,
)
with fig.subplot(
nrows=1,
ncols=2,
figsize=("13.5c", "5c"),
title="Histograms",
):
with fig.set_panel(panel=0):
fig.histogram(
data=data,
# Define the frame 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 thickness 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 (p) 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()