Skip to content

Commit de23d8e

Browse files
authored
Merge pull request #209 from QuantEcon/style-sheet
Chapter 10: Adding a Section on Matplotlib Style Sheets
2 parents 3272f26 + d69d735 commit de23d8e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

lectures/matplotlib.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,92 @@ The custom `subplots` function
268268
1. makes the desired customizations to `ax`, and
269269
1. passes the `fig, ax` pair back to the calling code.
270270

271+
### Style Sheets
272+
273+
Another useful feature in Matplotlib is [style sheets](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html).
274+
275+
We can use style sheets to create plots with uniform styles.
276+
277+
We can find a list of available styles by printing the attribute `plt.style.available`
278+
279+
280+
```{code-cell} python3
281+
print(plt.style.available)
282+
```
283+
284+
We can now use the `plt.style.use()` method to set the style sheet.
285+
286+
Let's write a function that takes the name of a style sheet and draws different plots with the style
287+
288+
```{code-cell} python3
289+
290+
def draw_graphs(style='default'):
291+
292+
# Setting a style sheet
293+
plt.style.use(style)
294+
295+
fig, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))
296+
x = np.linspace(-13, 13, 150)
297+
298+
# Set seed values to replicate results of random draws
299+
np.random.seed(9)
300+
301+
for i in range(3):
302+
303+
# Draw mean and standard deviation from uniform distributions
304+
m, s = np.random.uniform(-8, 8), np.random.uniform(2, 2.5)
305+
306+
# Generate a normal density plot
307+
y = norm.pdf(x, loc=m, scale=s)
308+
axes[0].plot(x, y, linewidth=3, alpha=0.7)
309+
310+
# Create a scatter plot with random X and Y values
311+
# from normal distributions
312+
rnormX = norm.rvs(loc=m, scale=s, size=150)
313+
rnormY = norm.rvs(loc=m, scale=s, size=150)
314+
axes[1].plot(rnormX, rnormY, ls='none', marker='o', alpha=0.7)
315+
316+
# Create a histogram with random X values
317+
axes[2].hist(rnormX, alpha=0.7)
318+
319+
# and a line graph with random Y values
320+
axes[3].plot(x, rnormY, linewidth=2, alpha=0.7)
321+
322+
plt.suptitle(f'Style: {style}', fontsize=13)
323+
plt.show()
324+
325+
```
326+
327+
Let's see what some of the styles look like.
328+
329+
First, we draw graphs with the style sheet `seaborn`
330+
331+
```{code-cell} python3
332+
draw_graphs(style='seaborn')
333+
```
334+
335+
We can use `grayscale` to remove colors in plots
336+
337+
```{code-cell} python3
338+
draw_graphs(style='grayscale')
339+
```
340+
341+
Here is what `ggplot` looks like
342+
343+
```{code-cell} python3
344+
draw_graphs(style='ggplot')
345+
```
346+
347+
We can also use the style `dark_background`
348+
349+
```{code-cell} python3
350+
draw_graphs(style='dark_background')
351+
```
352+
353+
You can use the function to experiment with other styles in the list.
354+
355+
If you are interested, you can even create [your own style sheets](https://matplotlib.org/stable/tutorials/introductory/customizing.html#defining-your-own-style).
356+
271357
## Further Reading
272358

273359
* The [Matplotlib gallery](http://matplotlib.org/gallery.html) provides many examples.

0 commit comments

Comments
 (0)