Skip to content

Commit ae7672e

Browse files
committed
DOC: Added plt.show() at the end of each necessary block (pandas-dev#45772)
Reworded and moved title to the top of the page
1 parent 9303302 commit ae7672e

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

doc/source/getting_started/intro_tutorials/04_plotting.rst

+40-29
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
{{ header }}
44

5-
.. ipython:: python
5+
How do I create plots in pandas?
6+
----------------------------------
7+
8+
.. image:: ../../_static/schemas/04_plot_overview.svg
9+
:align: center
10+
11+
.. ipython::
612

7-
import pandas as pd
8-
import matplotlib.pyplot as plt
13+
In [1]: import pandas as pd
14+
...: import matplotlib.pyplot as plt
915

1016
.. raw:: html
1117

@@ -20,10 +26,10 @@
2026

2127
.. include:: includes/air_quality_no2.rst
2228

23-
.. ipython:: python
29+
.. ipython::
2430

25-
air_quality = pd.read_csv("data/air_quality_no2.csv", index_col=0, parse_dates=True)
26-
air_quality.head()
31+
In [1]: air_quality = pd.read_csv("data/air_quality_no2.csv", index_col=0, parse_dates=True)
32+
...: air_quality.head()
2733

2834
.. note::
2935
The usage of the ``index_col`` and ``parse_dates`` parameters of the ``read_csv`` function to define the first (0th) column as
@@ -35,23 +41,17 @@
3541
</ul>
3642
</div>
3743

38-
How to create plots in pandas?
39-
------------------------------
40-
41-
.. image:: ../../_static/schemas/04_plot_overview.svg
42-
:align: center
43-
4444
.. raw:: html
4545

4646
<ul class="task-bullet">
4747
<li>
4848

4949
I want a quick visual check of the data.
5050

51-
.. ipython:: python
51+
.. ipython::
5252

5353
@savefig 04_airqual_quick.png
54-
air_quality.plot()
54+
In [1]: air_quality.plot();
5555

5656
With a ``DataFrame``, pandas creates by default one line plot for each of
5757
the columns with numeric data.
@@ -69,9 +69,18 @@ the columns with numeric data.
6969
I want to plot only the columns of the data table with the data from Paris.
7070

7171
.. ipython:: python
72+
:suppress:
73+
74+
# We need to clear the figure here as, within doc generation, the plot
75+
# accumulates data on each plot(). This is not needed when running
76+
# in a notebook, so is suppressed from output.
77+
plt.clf()
78+
79+
.. ipython::
7280

7381
@savefig 04_airqual_paris.png
74-
air_quality["station_paris"].plot()
82+
In [1]: air_quality["station_paris"].plot()
83+
...: plt.show()
7584

7685
To plot a specific column, use the selection method of the
7786
:ref:`subset data tutorial <10min_tut_03_subset>` in combination with the :meth:`~DataFrame.plot`
@@ -90,11 +99,11 @@ method. Hence, the :meth:`~DataFrame.plot` method works on both ``Series`` and
9099

91100
I want to visually compare the :math:`N0_2` values measured in London versus Paris.
92101

93-
.. ipython:: python
102+
.. ipython::
94103

95104
@savefig 04_airqual_scatter.png
96-
air_quality.plot.scatter(x="station_london", y="station_paris", alpha=0.5)
97-
105+
In [1]: air_quality.plot.scatter(x="station_london", y="station_paris", alpha=0.5)
106+
...: plt.show()
98107
.. raw:: html
99108

100109
</li>
@@ -121,10 +130,11 @@ One of the options is :meth:`DataFrame.plot.box`, which refers to a
121130
`boxplot <https://en.wikipedia.org/wiki/Box_plot>`__. The ``box``
122131
method is applicable on the air quality example data:
123132

124-
.. ipython:: python
133+
.. ipython::
125134

126135
@savefig 04_airqual_boxplot.png
127-
air_quality.plot.box()
136+
In [1]: air_quality.plot.box()
137+
...: plt.show()
128138

129139
.. raw:: html
130140

@@ -144,10 +154,11 @@ For an introduction to plots other than the default line plot, see the user guid
144154

145155
I want each of the columns in a separate subplot.
146156

147-
.. ipython:: python
157+
.. ipython::
148158

149159
@savefig 04_airqual_area_subplot.png
150-
axs = air_quality.plot.area(figsize=(12, 4), subplots=True)
160+
In [1]: axs = air_quality.plot.area(figsize=(12, 4), subplots=True)
161+
...: plt.show()
151162

152163
Separate subplots for each of the data columns are supported by the ``subplots`` argument
153164
of the ``plot`` functions. The builtin options available in each of the pandas plot
@@ -176,13 +187,13 @@ Some more formatting options are explained in the user guide section on :ref:`pl
176187

177188
I want to further customize, extend or save the resulting plot.
178189

179-
.. ipython:: python
180-
181-
fig, axs = plt.subplots(figsize=(12, 4))
182-
air_quality.plot.area(ax=axs)
183-
@savefig 04_airqual_customized.png
184-
axs.set_ylabel("NO$_2$ concentration")
185-
fig.savefig("no2_concentrations.png")
190+
.. ipython::
191+
@04_airqual_customized.png
192+
In [1]: fig, axs = plt.subplots(figsize=(12, 4))
193+
...: air_quality.plot.area(ax=axs)
194+
...: axs.set_ylabel("NO$_2$ concentration")
195+
...: fig.savefig("no2_concentrations.png")
196+
...: plt.show()
186197

187198
.. ipython:: python
188199
:suppress:

0 commit comments

Comments
 (0)