forked from catppuccin/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_plots.py
More file actions
142 lines (105 loc) Β· 3.77 KB
/
Copy pathexample_plots.py
File metadata and controls
142 lines (105 loc) Β· 3.77 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Functions to plot some images using matplotlib.
The generated plots are in the `assets` directory.
"""
from dataclasses import asdict
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
from catppuccin import PALETTE
from catppuccin.models import Flavor
SEED = 0
POINTS = 50
def plot_palette(palette: Flavor) -> plt.Figure: # type: ignore [name-defined]
"""Plot a palette with color names and hex values."""
colors = asdict(palette.colors)
# Create figure and adjust figure height to number of colormaps
nrows = len(colors)
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
fig, axs = plt.subplots(nrows=nrows, figsize=(6.4, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh, left=0.2, right=0.99)
axs[0].set_title(palette.name, fontsize=14)
for ax, color_name in zip(axs, colors, strict=True):
ax.hlines(0, 0, 1, colors=colors[color_name]["hex"], linewidth=15)
ax.text(
-0.01,
0.5,
f"{color_name} {colors[color_name]['hex']}",
va="center",
ha="right",
fontsize=10,
transform=ax.transAxes,
)
# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
fig.tight_layout()
return fig
def example_plot() -> plt.Figure: # type: ignore [name-defined]
"""Generate a plot with multiple sin functions with phase shifts."""
x = np.linspace(0.0, 1.0, num=101)
phases = np.linspace(0.0, -0.8, num=5)
np.sin(2 * np.pi * x)
fig = plt.figure()
for idx, phase in enumerate(phases):
plt.plot(x, np.sin(2 * np.pi * x + phase), label=f"Color {idx + 1}")
plt.grid()
plt.legend()
return fig
def example_scatter() -> plt.Figure: # type: ignore [name-defined]
"""Generate a scatter plot with two sets of random data."""
rng = np.random.default_rng(SEED)
x = rng.random(POINTS)
fig = plt.figure()
plt.scatter(x, rng.random(POINTS))
plt.scatter(x, rng.random(POINTS))
return fig
def example_boxplot() -> plt.Figure: # type: ignore [name-defined]
"""Generate a boxplot with random data."""
rng = np.random.default_rng(SEED)
bars = 4
nominal_values = rng.random(bars)
distributions = rng.random((bars, POINTS))
fig = plt.figure()
plt.boxplot(nominal_values + distributions.T, patch_artist=True)
return fig
def example_bar() -> plt.Figure: # type: ignore [name-defined]
"""Generate a bar plot with random data."""
rng = np.random.default_rng(SEED)
bars = 10
x = np.arange(bars).astype(np.float64) + 0.5
y = rng.random(bars)
fig = plt.figure()
plt.bar(x, y)
return fig
def example_patches() -> plt.Figure: # type: ignore [name-defined]
"""Generate a plot with two arrows."""
fig, ax = plt.subplots()
arrow_1 = mpatches.FancyArrowPatch((0, 1), (1, 0), mutation_scale=100)
arrow_2 = mpatches.FancyArrowPatch((0, 0), (1, 1), mutation_scale=100)
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
ax.add_patch(arrow_1)
ax.add_patch(arrow_2)
return fig
def example_imshow() -> plt.Figure: # type: ignore [name-defined]
"""Generate an image plot with random data."""
rng = np.random.default_rng(SEED)
data = rng.random((30, 30))
fig, ax = plt.subplots()
im = ax.imshow(data)
ax.tick_params(
left=False, right=False, labelleft=False, labelbottom=False, bottom=False
)
fig.colorbar(im, ax=ax, ticks=[])
return fig
if __name__ == "__main__":
flavor = PALETTE.mocha
plt.style.use(flavor.matplotlib_style)
plot_palette(flavor)
example_plot()
example_scatter()
example_boxplot()
example_bar()
example_patches()
example_imshow()
plt.show()