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 an example for scatter plots with auto legends #607

Merged
merged 7 commits into from
Sep 27, 2020
Merged
Changes from all 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
41 changes: 41 additions & 0 deletions examples/gallery/plot/scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Scatter plots with a legend
---------------------------

To create a scatter plot with a legend one may use a loop and create one scatter
plot per item to appear in the legend and set the label accordingly.

Modified from the matplotlib example:
https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html
"""

import numpy as np
import pygmt

np.random.seed(19680801)
n = 200 # number of random data points

fig = pygmt.Figure()
fig.basemap(
region=[-0.1, 1.1, -0.1, 1.1],
projection="X10c/10c",
frame=["xa0.2fg", "ya0.2fg", "WSrt"],
)
for color in ["blue", "orange", "green"]:
x, y = np.random.rand(2, n) # random X and Y data in [0,1]
sizes = np.random.rand(n) * 0.5 # random size [0,0.5], in cm
# plot data points as circles (style="c"), with different sizes
fig.plot(
x=x,
y=y,
style="c",
sizes=sizes,
color=color,
# Set the legend label,
# and set the circle size to be 0.25 cm (+S0.25c) in legend
label=f"{color}+S0.25c",
transparency=70, # set transparency level for all symbols
)

fig.legend(transparency=30) # set transparency level for legends
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is cool, I hadn't thought of using transparent legend boxes before!

fig.show()