diff --git a/09-plotting.md b/09-plotting.md index e9b9b11ed..3fb77f222 100644 --- a/09-plotting.md +++ b/09-plotting.md @@ -347,14 +347,22 @@ matplotlib will make this variable refer to a new empty figure. Therefore, make sure you call `plt.savefig` before the plot is displayed to the screen, otherwise you may find a file with an empty plot. +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. + +```python +plt.tight_layout() +plt.savefig('my_figure.png') +``` + When using dataframes, data is often generated and plotted to screen in one line. In addition to using `plt.savefig`, we can save a reference to the current figure -in a local variable (with `plt.gcf`) and call the `savefig` class method from +in a local variable (with `plt.gcf`) and call the `tight_layout` and `savefig` class method from that variable to save the figure to file. ```python data.plot(kind='bar') fig = plt.gcf() # get current figure +fig.tight_layout() fig.savefig('my_figure.png') ``` diff --git a/md5sum.txt b/md5sum.txt index dea8d627c..a13579eba 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -12,7 +12,7 @@ "episodes/06-libraries.md" "96899c58843e51f10eb84a8ac20ebb90" "site/built/06-libraries.md" "2023-05-02" "episodes/07-reading-tabular.md" "b5b65e50037a583dfc5a3a879e4404b0" "site/built/07-reading-tabular.md" "2023-05-02" "episodes/08-data-frames.md" "af0057242e5f63f0c049f58ad66f1cbb" "site/built/08-data-frames.md" "2023-08-29" -"episodes/09-plotting.md" "d701a7c8d39329d1786b48b32063ffc8" "site/built/09-plotting.md" "2024-03-17" +"episodes/09-plotting.md" "d658f02ee6086122c2af0fdbc1c07609" "site/built/09-plotting.md" "2024-03-17" "episodes/10-lunch.md" "0624bfa89e628df443070e8c44271b33" "site/built/10-lunch.md" "2023-05-02" "episodes/11-lists.md" "1257daeb542377a3b04c6bec0d0ffee1" "site/built/11-lists.md" "2023-07-24" "episodes/12-for-loops.md" "1da6e4e57a25f8d4fd64802c2eb682c4" "site/built/12-for-loops.md" "2023-05-02"