Skip to content

Commit 8b0ab09

Browse files
committed
differences for PR #650
1 parent df94160 commit 8b0ab09

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

09-plotting.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,22 @@ matplotlib will make this variable refer to a new empty figure.
347347
Therefore, make sure you call `plt.savefig` before the plot is displayed to
348348
the screen, otherwise you may find a file with an empty plot.
349349

350+
Sometimes, some elements of the figure may get truncated when saving the figure to a file. You can find an in-depth explanation why this happens [here](https://matplotlib.org/stable/users/explain/axes/tight_layout_guide.html). To avoid this, we can call `plt.tight_layout` before `plt.savefig` to make sure that everything fits into the figure area.
351+
352+
```python
353+
plt.tight_layout()
354+
plt.savefig('my_figure.png')
355+
```
356+
350357
When using dataframes, data is often generated and plotted to screen in one line.
351358
In addition to using `plt.savefig`, we can save a reference to the current figure
352-
in a local variable (with `plt.gcf`) and call the `savefig` class method from
359+
in a local variable (with `plt.gcf`) and call the `tight_layout` and `savefig` class method from
353360
that variable to save the figure to file.
354361

355362
```python
356363
data.plot(kind='bar')
357364
fig = plt.gcf() # get current figure
365+
fig.tight_layout()
358366
fig.savefig('my_figure.png')
359367
```
360368

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"episodes/06-libraries.md" "96899c58843e51f10eb84a8ac20ebb90" "site/built/06-libraries.md" "2023-05-02"
1313
"episodes/07-reading-tabular.md" "b5b65e50037a583dfc5a3a879e4404b0" "site/built/07-reading-tabular.md" "2023-05-02"
1414
"episodes/08-data-frames.md" "af0057242e5f63f0c049f58ad66f1cbb" "site/built/08-data-frames.md" "2023-08-29"
15-
"episodes/09-plotting.md" "d701a7c8d39329d1786b48b32063ffc8" "site/built/09-plotting.md" "2024-03-17"
15+
"episodes/09-plotting.md" "d658f02ee6086122c2af0fdbc1c07609" "site/built/09-plotting.md" "2024-03-17"
1616
"episodes/10-lunch.md" "0624bfa89e628df443070e8c44271b33" "site/built/10-lunch.md" "2023-05-02"
1717
"episodes/11-lists.md" "1257daeb542377a3b04c6bec0d0ffee1" "site/built/11-lists.md" "2023-07-24"
1818
"episodes/12-for-loops.md" "1da6e4e57a25f8d4fd64802c2eb682c4" "site/built/12-for-loops.md" "2023-05-02"

0 commit comments

Comments
 (0)