-
Notifications
You must be signed in to change notification settings - Fork 901
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
revamp using style sheets #88
Changes from 5 commits
aec0df4
b20eb40
5ea8b48
aeeeb3b
e4c6b67
c44d32b
cfa709d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,62 +2,82 @@ | |
# Matplotlib cheat sheet | ||
# Released under the BSD License | ||
# ----------------------------------------------------------------------------- | ||
import pathlib | ||
|
||
import numpy as np | ||
import matplotlib as mpl | ||
import matplotlib.pyplot as plt | ||
import matplotlib.patches as mpatches | ||
from matplotlib.collections import PatchCollection | ||
|
||
|
||
fig = plt.figure(figsize=(4.25, 4.25 * 95/115)) | ||
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1, | ||
xlim=(0-5, 100+10), ylim=(-10, 80+5), xticks=[], yticks=[]) | ||
mpl.style.use([ | ||
pathlib.Path(__file__).parent/'../styles/base.mplstyle', | ||
]) | ||
mpl.rc('font', size=4) | ||
mpl.rc('lines', linewidth=0.5) | ||
mpl.rc('patch', linewidth=0.5) | ||
|
||
|
||
subplots_kw = dict( | ||
figsize=(5.7/2.54, 5.7/2.54 * 95/115), | ||
subplot_kw=dict( | ||
frameon=False, | ||
aspect=1, | ||
xlim=(0-5, 100+10), | ||
ylim=(-10, 80+5), | ||
xticks=[], | ||
yticks=[], | ||
), | ||
) | ||
|
||
(fig, ax) = plt.subplots(**subplots_kw) | ||
|
||
|
||
box = mpatches.FancyBboxPatch( | ||
(0, 0), 100, 83, mpatches.BoxStyle("Round", pad=0, rounding_size=2), | ||
linewidth=1., facecolor="0.9", edgecolor="black") | ||
facecolor="0.9", edgecolor="black") | ||
ax.add_artist(box) | ||
|
||
box = mpatches.FancyBboxPatch( | ||
(0, 0), 100, 75, mpatches.BoxStyle("Round", pad=0, rounding_size=0), | ||
linewidth=1., facecolor="white", edgecolor="black") | ||
facecolor="white", edgecolor="black") | ||
ax.add_artist(box) | ||
|
||
|
||
box = mpatches.Rectangle( | ||
(5, 5), 45, 30, zorder=10, | ||
linewidth=1.0, facecolor="white", edgecolor="black") | ||
facecolor="white", edgecolor="black") | ||
ax.add_artist(box) | ||
|
||
box = mpatches.Rectangle( | ||
(5, 40), 45, 30, zorder=10, | ||
linewidth=1.0, facecolor="white", edgecolor="black") | ||
facecolor="white", edgecolor="black") | ||
ax.add_artist(box) | ||
|
||
box = mpatches.Rectangle( | ||
(55, 5), 40, 65, zorder=10, | ||
linewidth=1.0, facecolor="white", edgecolor="black") | ||
facecolor="white", edgecolor="black") | ||
ax.add_artist(box) | ||
|
||
# Window button | ||
X, Y = [5, 10, 15], [79, 79, 79] | ||
plt.scatter(X, Y, s=75, zorder=10, | ||
edgecolor="black", facecolor="white", linewidth=1) | ||
plt.scatter(X, Y, s=20, zorder=10, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change the size? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The size of the figures were reduced to match |
||
edgecolor="black", facecolor="white") | ||
|
||
|
||
# Window size extension | ||
X, Y = [0, 0], [0, -8] | ||
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) | ||
plt.plot(X, Y, color="black", linestyle=":", clip_on=False) | ||
|
||
X, Y = [100, 100], [0, -8] | ||
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) | ||
plt.plot(X, Y, color="black", linestyle=":", clip_on=False) | ||
|
||
X, Y = [100, 108], [0, 0] | ||
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) | ||
plt.plot(X, Y, color="black", linestyle=":", clip_on=False) | ||
|
||
X, Y = [100, 108], [75, 75] | ||
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) | ||
plt.plot(X, Y, color="black", linestyle=":", clip_on=False) | ||
|
||
|
||
def ext_arrow(p0, p1, p2, p3): | ||
|
@@ -69,7 +89,7 @@ def ext_arrow(p0, p1, p2, p3): | |
ax.arrow(*p3, *(p2-p3), zorder=20, linewidth=0, | ||
length_includes_head=True, width=.4, | ||
head_width=2, head_length=2, color="black") | ||
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.9, color="black") | ||
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.5, color="black") | ||
|
||
|
||
def int_arrow(p0, p1): | ||
|
@@ -85,45 +105,44 @@ def int_arrow(p0, p1): | |
x = 0 | ||
y = 10 | ||
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) | ||
ax.text(x+9.5, y, "left", ha="left", va="center", size="x-small", zorder=20) | ||
ax.text(x+9.5, y, "left", ha="left", va="center", zorder=20) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove the text size? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The font size is configured with mpl.rc. |
||
|
||
x += 50 | ||
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) | ||
ax.text(x-4.5, y, "wspace", ha="right", va="center", size="x-small", zorder=20) | ||
ax.text(x-4.5, y, "wspace", ha="right", va="center", zorder=20) | ||
|
||
x += 45 | ||
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) | ||
ax.text(x-4.5, y, "right", ha="right", va="center", size="x-small", zorder=20) | ||
ax.text(x-4.5, y, "right", ha="right", va="center", zorder=20) | ||
|
||
y = 0 | ||
x = 25 | ||
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) | ||
ax.text(x, y+9.5, "bottom", ha="center", va="bottom", size="x-small", zorder=20) | ||
ax.text(x, y+9.5, "bottom", ha="center", va="bottom", zorder=20) | ||
|
||
y += 35 | ||
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) | ||
ax.text(x, y-4.5, "hspace", ha="center", va="top", size="x-small", zorder=20) | ||
ax.text(x, y-4.5, "hspace", ha="center", va="top", zorder=20) | ||
|
||
y += 35 | ||
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) | ||
ax.text(x, y-4.5, "top", ha="center", va="top", size="x-small", zorder=20) | ||
ax.text(x, y-4.5, "top", ha="center", va="top", zorder=20) | ||
|
||
int_arrow((0, -5), (100, -5)) | ||
ax.text(50, -5, "figure width", backgroundcolor="white", zorder=30, | ||
ha="center", va="center", size="x-small") | ||
ha="center", va="center") | ||
|
||
int_arrow((105, 0), (105, 75)) | ||
ax.text(105, 75/2, "figure height", backgroundcolor="white", zorder=30, | ||
rotation="vertical", ha="center", va="center", size="x-small") | ||
rotation="vertical", ha="center", va="center") | ||
|
||
int_arrow((55, 62.5), (95, 62.5)) | ||
ax.text(75, 62.5, "axes width", backgroundcolor="white", zorder=30, | ||
ha="center", va="center", size="x-small") | ||
ha="center", va="center") | ||
|
||
int_arrow((62.5, 5), (62.5, 70)) | ||
ax.text(62.5, 35, "axes height", backgroundcolor="white", zorder=30, | ||
rotation="vertical", ha="center", va="center", size="x-small") | ||
rotation="vertical", ha="center", va="center") | ||
|
||
|
||
plt.savefig("../figures/adjustments.pdf") | ||
# plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,47 +3,51 @@ | |
# Released under the BSD License | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Scripts to generate all the basic plots | ||
# Script to generate all the advanced plots | ||
import pathlib | ||
|
||
import numpy as np | ||
import matplotlib as mpl | ||
import matplotlib.pyplot as plt | ||
|
||
fig = plt.figure(figsize=(0.4, 0.4)) | ||
mpl.rcParams['axes.linewidth'] = 0.5 | ||
mpl.rcParams['xtick.major.size'] = 0.0 | ||
mpl.rcParams['ytick.major.size'] = 0.0 | ||
d = 0.01 | ||
ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ax is to make sure we have enough space for the frame lines. If set exactly to 0,0,1,1, then the outer lines are clipped and are not anti-aliased. Not sure the replacement code take care of that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the |
||
|
||
mpl.style.use([ | ||
pathlib.Path(__file__).parent/'../styles/base.mplstyle', | ||
jimustafa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', | ||
]) | ||
|
||
|
||
subplot_kw = dict( | ||
xlim=(0, 8), xticks=np.arange(1, 8), | ||
ylim=(0, 8), yticks=np.arange(1, 8), | ||
) | ||
|
||
# Step plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
X = np.linspace(0, 10, 16) | ||
Y = 4 + 2*np.sin(2*X) | ||
ax.step(X, Y, color="C1", linewidth=0.75) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this set somewhere else ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is set in the |
||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-step.pdf") | ||
ax.clear() | ||
ax.step(X, Y, color="C1") | ||
ax.grid() | ||
fig.savefig("../figures/advanced-step.pdf") | ||
|
||
# Violin plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(10) | ||
D = np.random.normal((3, 5, 4), (0.75, 1.00, 0.75), (200, 3)) | ||
VP = ax.violinplot(D, [2, 4, 6], widths=1.5, | ||
showmeans=False, showmedians=False, showextrema=False) | ||
for body in VP['bodies']: | ||
body.set_facecolor('C1') | ||
body.set_alpha(1) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-violin.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-violin.pdf") | ||
|
||
# Boxplot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(10) | ||
D = np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3)) | ||
VP = ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True, | ||
|
@@ -57,85 +61,74 @@ | |
"linewidth": 0.75}, | ||
capprops={"color": "C1", | ||
"linewidth": 0.75}) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-boxplot.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-boxplot.pdf") | ||
|
||
# Barbs plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(1) | ||
X = [[2, 4, 6]] | ||
Y = [[1.5, 3, 2]] | ||
U = -np.ones((1, 3)) * 0 | ||
V = -np.ones((1, 3)) * np.linspace(50, 100, 3) | ||
ax.barbs(X, Y, U, V, barbcolor="C1", flagcolor="C1", length=5, linewidth=0.5) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-barbs.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-barbs.pdf") | ||
|
||
# Event plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(1) | ||
X = [2, 4, 6] | ||
D = np.random.gamma(4, size=(3, 50)) | ||
ax.eventplot(D, colors="C1", orientation="vertical", lineoffsets=X, | ||
linewidth=0.25) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-event.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-event.pdf") | ||
|
||
# Errorbar plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(1) | ||
X = [2, 4, 6] | ||
Y = [4, 5, 4] | ||
E = np.random.uniform(0.5, 1.5, 3) | ||
ax.errorbar(X, Y, E, color="C1", linewidth=0.75, capsize=1) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-errorbar.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-errorbar.pdf") | ||
|
||
# Hexbin plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(1) | ||
X = np.random.uniform(1.5, 6.5, 100) | ||
Y = np.random.uniform(1.5, 6.5, 100) | ||
C = np.random.uniform(0, 1, 10000) | ||
ax.hexbin(X, Y, C, gridsize=4, linewidth=0.25, edgecolor="white", | ||
cmap=plt.get_cmap("Wistia"), alpha=1.0) | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-hexbin.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-hexbin.pdf") | ||
|
||
# Hist plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(1) | ||
X = 4 + np.random.normal(0, 1.5, 200) | ||
ax.hist(X, bins=8, facecolor="C1", linewidth=0.25, edgecolor="white") | ||
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) | ||
ax.set_ylim(0, 80), ax.set_yticks(np.arange(1, 80, 10)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-hist.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-hist.pdf") | ||
|
||
# Xcorr plot | ||
# ----------------------------------------------------------------------------- | ||
(fig, ax) = plt.subplots(subplot_kw=subplot_kw) | ||
np.random.seed(3) | ||
Y = np.random.uniform(-4, 4, 250) | ||
X = np.random.uniform(-4, 4, 250) | ||
|
@@ -144,6 +137,5 @@ | |
ax.set_xlim(-8, 8), ax.set_xticks(np.arange(-8, 8, 2)) | ||
ax.set_ylim(-.25, .25), ax.set_yticks(np.linspace(-.25, .25, 9)) | ||
ax.set_axisbelow(True) | ||
ax.grid(linewidth=0.125) | ||
plt.savefig("../figures/advanced-xcorr.pdf") | ||
ax.clear() | ||
ax.grid() | ||
fig.savefig("../figures/advanced-xcorr.pdf") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove
linewidth=1
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linewidth is configured with mpl.rc.