-
Notifications
You must be signed in to change notification settings - Fork 0
/
barbie_style_demo.py
115 lines (92 loc) · 3.84 KB
/
barbie_style_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import numpy as np
from numpy.random import RandomState
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
from matplotlib import rcParams
# Fixing random state for reproducibility
np.random.seed(19680801)
# Add Barbie font to the list of available fonts
font_manager.fontManager.addfont('Bartex.ttf')
rcParams['font.family'] = 'Bartex'
# Use hyphen instead of Unicode minus
rcParams['axes.unicode_minus'] = False
def plot_scatter(ax, prng, nb_samples=100):
"""Scatter plot."""
for mu, sigma, marker in [(-.5, 0.75, 'o'), (0.75, 1., 's')]:
x, y = prng.normal(loc=mu, scale=sigma, size=(2, nb_samples))
ax.plot(x, y, ls='none', marker=marker)
return ax
def plot_colored_lines(ax):
"""Plot lines with colors following the style color cycle."""
t = np.linspace(-10, 10, 100)
def sigmoid(t, t0):
return 1 / (1 + np.exp(-(t - t0)))
nb_colors = len(plt.rcParams['axes.prop_cycle'])
shifts = np.linspace(-5, 5, nb_colors)
amplitudes = np.linspace(1, 1.5, nb_colors)
for t0, a in zip(shifts, amplitudes):
ax.plot(t, a * sigmoid(t, t0), '-')
ax.set_xlim(-10, 10)
return ax
def plot_bar_graphs(ax, prng, min_value=5, max_value=25, nb_samples=5):
"""Plot two bar graphs side by side, with letters as x-tick labels."""
x = np.arange(nb_samples)
ya, yb = prng.randint(min_value, max_value, size=(2, nb_samples))
width = 0.25
ax.bar(x, ya, width)
ax.bar(x + width, yb, width, color='C2')
ax.set_xticks(x + width, labels=['a', 'b', 'c', 'd', 'e'])
return ax
def plot_colored_circles(ax, prng, nb_samples=15):
"""
Plot circle patches.
NB: draws a fixed amount of samples, rather than using the length of
the color cycle, because different styles may have different numbers
of colors.
"""
for sty_dict, j in zip(plt.rcParams['axes.prop_cycle'](),
range(nb_samples)):
ax.add_patch(plt.Circle(prng.normal(scale=3, size=2),
radius=1.0, color=sty_dict['color']))
ax.set_xlim([-4, 8])
ax.set_ylim([-5, 6])
ax.set_aspect('equal', adjustable='box') # to plot circles as circles
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_title('Title')
return ax
def plot_histograms(ax, prng, nb_samples=10000):
"""Plot 4 histograms and a text annotation."""
params = ((10, 10), (4, 12), (50, 12), (6, 55))
for a, b in params:
values = prng.beta(a, b, size=nb_samples)
ax.hist(values, histtype="stepfilled", bins=30,
alpha=0.8, density=True)
return ax
def plot_figure(style_label=""):
"""Setup and plot the demonstration figure with a given style."""
# Use a dedicated RandomState instance to draw the same "random" values
# across the different figures.
prng = RandomState(96917002)
fig, axs = plt.subplots(ncols=5, nrows=1, num=style_label,
figsize=(14.8, 2.8), layout='constrained')
fig.suptitle('Barbie-fied plots', x=0.01, ha='left', color='#d74ea2',
fontsize=14)
plot_scatter(axs[0], prng)
plot_bar_graphs(axs[1], prng)
plot_colored_lines(axs[2])
plot_histograms(axs[3], prng)
plot_colored_circles(axs[4], prng)
if __name__ == "__main__":
# To modify this style further, all the available styles can be viewed
# using the code below for inspiration:
# style_list = sorted(
# style for style in plt.style.available
# if style != 'classic' and not style.startswith('_'))
style_list = ['barbie.mplstyle']
# Plot a demonstration figure for every specified style sheet.
for style_label in style_list:
with plt.rc_context({"figure.max_open_warning": len(style_list)}):
with plt.style.context(style_label):
plot_figure(style_label=style_label)
plt.show()