In [1]:
%matplotlib inline
import betterplotlib as bpl
import numpy as np
import matplotlib.pyplot as plt
In [2]:
xs1 = np.random.normal(0, 1, 500)
ys1 = np.random.normal(0, 1, 500)
xs2 = np.random.normal(1, 1, 500)
ys2 = np.random.normal(1, 1, 500)
In [3]:
fig, [ax0, ax1] = plt.subplots(figsize=[7, 10], nrows=2, tight_layout=True)
ax0.hist(xs1)
ax0.set_xlabel("Data Values")
ax0.set_ylabel("Number")
ax0.set_xlim(-4, 4)
ax1.scatter(xs1, ys1, label="Data 1")
ax1.scatter(xs2, ys2, label="Data 2")
ax1.set_xlabel("X Values")
ax1.set_ylabel("Y Values")
ax1.legend(loc=2)
ax1.set_xlim(-4, 6)
ax1.set_ylim(-4, 6)
ax0.set_title("matplotlib defaults", fontsize=24)
fig.savefig("plt.png", dpi=300)
In [4]:
bpl.presentation_style()
In [5]:
fig, [ax0, ax1] = bpl.subplots(figsize=[7, 10], nrows=2, tight_layout=True)
ax0.hist(xs1, bin_size=0.5)
ax0.set_xlabel("Data Values")
ax0.set_ylabel("Number")
ax0.set_xlim(-4, 4)
ax1.scatter(xs1, ys1, label="Data 1")
ax1.scatter(xs2, ys2, label="Data 2")
ax1.set_xlabel("X Values")
ax1.set_ylabel("Y Values")
ax1.legend(loc=2)
ax1.set_xlim(-4, 6)
ax1.set_ylim(-4, 6)
ax0.set_title("betterplotlib defaults", fontsize=24)
fig.savefig("bpl.png", dpi=300)
In [12]:
x1 = np.random.normal(-1, 2, 5000)
x2 = np.random.normal(1, 2, 5000)
contour_xs = np.concatenate([np.random.normal(0, 1, 10000),
np.random.normal(3, 1, 10000),
np.random.normal(0, 1, 10000)])
contour_ys = np.concatenate([np.random.normal(0, 1, 10000),
np.random.normal(3, 1, 10000),
np.random.normal(3, 1, 10000)])
In [33]:
x3 = np.random.normal(0, 1, 500)
y3 = np.random.normal(0, 1, 500)
x4 = np.random.normal(2, 1, 500)
y4 = np.random.normal(0, 1, 500)
x5 = np.random.normal(2, 1, 500)
y5 = np.random.normal(2, 1, 500)
In [23]:
sc_x1 = np.random.uniform(-2, 3, 200)
sc_y1 = sc_x1 + np.random.normal(0, 0.5, 200)
sc_x2 = np.random.normal(1, 1, 200)
sc_y2 = np.random.normal(1, 1, 200)
In [39]:
fig, [ax1, ax2] = bpl.subplots(ncols=2, nrows=1, figsize=[15, 7])
# [ax1, ax2], [ax3, ax4] = axs
ax1.hist(x1, rel_freq=True, histtype="step", bin_size=0.5, lw=3, hatch="\\", label="Data 1")
ax1.hist(x2, rel_freq=True, histtype="step", bin_size=0.5, lw=3, hatch= "/", label="Data 2")
ax1.remove_spines(["top", "right"])
ax1.add_labels("X Value", "Relative Frequency")
ax1.set_limits(-10, 10, 0, 0.12)
ax1.legend()
ax2.contour_scatter(contour_xs, contour_ys, bin_size=0.3, scatter_kwargs={"label":"Outliers"})
ax2.equal_scale()
ax2.make_ax_dark()
ax2.set_limits(-4, 8, -4, 8)
ax2.legend("light")
ax2.add_labels("X Value", "Y Value")
# ax3.scatter(x3, y3)
# ax3.scatter(x4, y4)
# ax3.scatter(x5, y5)
# ax3.add_labels("X Value", "Y Value")
# ax4.scatter(sc_x1, sc_y1, label="Data 1")
# ax4.scatter(sc_x2, sc_y2, label="Data 2")
# ax4.remove_spines(["top", "right"])
# ax4.legend(loc=2)
# ax4.add_labels("X Value", "Y Value")
fig.savefig("bpl_demo.png")
In [84]:
fig, ax = bpl.subplots()
xs = [0, 1]
ax.fill_between(xs, 0, 1, color=bpl.almost_black)
ax.fill_between([0.75, 1.0], 0, 1, color="k")
ax.fill_between(xs, 1, 2, color=bpl.light_gray)
ax.fill_between(xs, 2, 3, color=bpl.steel_blue)
ax.fill_between(xs, 3, 4, color=bpl.parks_canada_heritage_green)
for idx, col in enumerate(bpl.color_cycle[::-1]):
ax.fill_between(xs, 4 + 2 * idx / len(bpl.color_cycle), 4 + 2 * (idx + 1) / len(bpl.color_cycle), color=col)
ax.remove_spines(["all"])
ax.remove_labels("both")
ax.add_text(0.5, 0.5, "almost_black", ha="center", va="center", color="w")
ax.add_text((1.0 + 0.75) / 2.0, 0.5, "black", ha="center", va="center", color="w")
ax.add_text(0.5, 1.5, "light_gray", ha="center", va="center", color=bpl.almost_black)
ax.add_text(0.5, 2.5, "steel_blue", ha="center", va="center", color="w")
ax.add_text(0.5, 3.5, "parks_canada_heritage_green", ha="center", va="center", color="w")
ax.text(0.25, 5.0, "Color Cycle", rotation=45, ha="center", va="center", color="w", fontsize=28)
ax.add_text(0.75, 4 + 1.0 / len(bpl.color_cycle), "9", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 3.0 / len(bpl.color_cycle), "8", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 5.0 / len(bpl.color_cycle), "7", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 7.0 / len(bpl.color_cycle), "6", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 9.0 / len(bpl.color_cycle), "5", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 11.0 / len(bpl.color_cycle), "4", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 13.0 / len(bpl.color_cycle), "3", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 15.0 / len(bpl.color_cycle), "2", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 17.0 / len(bpl.color_cycle), "1", ha="center", va="center", color="w", fontsize=14)
ax.add_text(0.75, 4 + 19.0 / len(bpl.color_cycle), "0", ha="center", va="center", color="w", fontsize=14)
fig.savefig("colors.png")
In [ ]: