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 gallery example for scatter plot with histograms on sides #2410

Merged
merged 21 commits into from
Mar 12, 2023
Merged
Changes from 8 commits
Commits
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
62 changes: 62 additions & 0 deletions examples/gallery/histograms/scatter_and_histograms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Scatter plot with histograms
----------------------------
To create a scatter plot with histograms at the sides of the plot one
can use :meth:`pygmt.Figure.plot` in combination with
:meth:`pygmt.Figure.histogram`.
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
"""

import numpy as np
import pygmt

np.random.seed(19680801)

# generate random data
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
x = np.random.randn(1000)
y = np.random.randn(1000)

# get axis limits
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))

fig = pygmt.Figure()
fig.basemap(
region=[-xymax - 0.5, xymax + 0.5, -xymax - 0.5, xymax + 0.5],
projection="X10c/10c",
frame=["WSrt", "a1"],
)

fillcol = "seagreen"

# plot data points as circles with a diameter of 0.25 centimeters
fig.plot(x=x, y=y, style="c0.25c", fill=fillcol, transparency=50)
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved

# add top margin histogram
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
fig.shift_origin(yshift="10.25c")

fig.histogram(
projection="X10c/2c",
frame=["Wsrt", "y+lCounts"],
# Since no y-range is specified, ymax is calculated automatically
region=[-xymax - 0.5, xymax + 0.5, 0, 0],
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
data=x,
fill=fillcol,
histtype=0,
series=0.1,
)

# add right margin histogram
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
fig.shift_origin(yshift="-10.25c", xshift="10.25c")

fig.histogram(
horizontal=True,
projection="X2c/10c",
# Note that x and y are flipped here due to horizontal=True
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
frame=["wSrt", "y+lCounts"],
region=[-xymax - 0.5, xymax + 0.5, 0, 0],
data=y,
fill=fillcol,
histtype=0,
series=0.1,
)

fig.show()