2
2
3
3
{{ header }}
4
4
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 ::
6
12
7
- import pandas as pd
8
- import matplotlib.pyplot as plt
13
+ In [1]: import pandas as pd
14
+ ...: import matplotlib.pyplot as plt
9
15
10
16
.. raw :: html
11
17
20
26
21
27
.. include :: includes/air_quality_no2.rst
22
28
23
- .. ipython :: python
29
+ .. ipython ::
24
30
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()
27
33
28
34
.. note ::
29
35
The usage of the ``index_col `` and ``parse_dates `` parameters of the ``read_csv `` function to define the first (0th) column as
35
41
</ul >
36
42
</div >
37
43
38
- How to create plots in pandas?
39
- ------------------------------
40
-
41
- .. image :: ../../_static/schemas/04_plot_overview.svg
42
- :align: center
43
-
44
44
.. raw :: html
45
45
46
46
<ul class =" task-bullet" >
47
47
<li >
48
48
49
49
I want a quick visual check of the data.
50
50
51
- .. ipython :: python
51
+ .. ipython ::
52
52
53
53
@savefig 04_airqual_quick.png
54
- air_quality.plot()
54
+ In [1]: air_quality.plot()
55
+ ...: plt.show()
55
56
56
57
With a ``DataFrame ``, pandas creates by default one line plot for each of
57
58
the columns with numeric data.
@@ -69,9 +70,18 @@ the columns with numeric data.
69
70
I want to plot only the columns of the data table with the data from Paris.
70
71
71
72
.. ipython :: python
73
+ :suppress:
74
+
75
+ # We need to clear the figure here as, within doc generation, the plot
76
+ # accumulates data on each plot(). This is not needed when running
77
+ # in a notebook, so is suppressed from output.
78
+ plt.clf()
79
+
80
+ .. ipython ::
72
81
73
82
@savefig 04_airqual_paris.png
74
- air_quality[" station_paris" ].plot()
83
+ In [1]: air_quality["station_paris"].plot()
84
+ ...: plt.show()
75
85
76
86
To plot a specific column, use the selection method of the
77
87
:ref: `subset data tutorial <10min_tut_03_subset >` in combination with the :meth: `~DataFrame.plot `
@@ -90,11 +100,11 @@ method. Hence, the :meth:`~DataFrame.plot` method works on both ``Series`` and
90
100
91
101
I want to visually compare the :math: `NO_2 ` values measured in London versus Paris.
92
102
93
- .. ipython :: python
103
+ .. ipython ::
94
104
95
105
@savefig 04_airqual_scatter.png
96
- air_quality.plot.scatter(x = " station_london" , y = " station_paris" , alpha = 0.5 )
97
-
106
+ In [1]: air_quality.plot.scatter(x="station_london", y="station_paris", alpha=0.5)
107
+ ...: plt.show()
98
108
.. raw :: html
99
109
100
110
</li >
@@ -121,10 +131,11 @@ One of the options is :meth:`DataFrame.plot.box`, which refers to a
121
131
`boxplot <https://en.wikipedia.org/wiki/Box_plot >`__. The ``box ``
122
132
method is applicable on the air quality example data:
123
133
124
- .. ipython :: python
134
+ .. ipython ::
125
135
126
136
@savefig 04_airqual_boxplot.png
127
- air_quality.plot.box()
137
+ In [1]: air_quality.plot.box()
138
+ ...: plt.show()
128
139
129
140
.. raw :: html
130
141
@@ -144,10 +155,11 @@ For an introduction to plots other than the default line plot, see the user guid
144
155
145
156
I want each of the columns in a separate subplot.
146
157
147
- .. ipython :: python
158
+ .. ipython ::
148
159
149
160
@savefig 04_airqual_area_subplot.png
150
- axs = air_quality.plot.area(figsize = (12 , 4 ), subplots = True )
161
+ In [1]: axs = air_quality.plot.area(figsize=(12, 4), subplots=True)
162
+ ...: plt.show()
151
163
152
164
Separate subplots for each of the data columns are supported by the ``subplots `` argument
153
165
of the ``plot `` functions. The builtin options available in each of the pandas plot
@@ -176,13 +188,14 @@ Some more formatting options are explained in the user guide section on :ref:`pl
176
188
177
189
I want to further customize, extend or save the resulting plot.
178
190
179
- .. ipython :: python
191
+ .. ipython ::
180
192
181
- fig, axs = plt.subplots(figsize = (12 , 4 ))
182
- air_quality.plot.area(ax = axs)
183
193
@savefig 04_airqual_customized.png
184
- axs.set_ylabel(" NO$_2$ concentration" )
185
- fig.savefig(" no2_concentrations.png" )
194
+ In [1]: fig, axs = plt.subplots(figsize=(12, 4))
195
+ ...: air_quality.plot.area(ax=axs)
196
+ ...: axs.set_ylabel("NO$_2$ concentration")
197
+ ...: fig.savefig("no2_concentrations.png")
198
+ ...: plt.show()
186
199
187
200
.. ipython :: python
188
201
:suppress:
0 commit comments