2
2
3
3
{{ header }}
4
4
5
+ How do I create plots in pandas?
6
+ ----------------------------------
7
+
8
+ .. image :: ../../_static/schemas/04_plot_overview.svg
9
+ :align: center
10
+
5
11
.. ipython :: python
6
12
7
13
import pandas as pd
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" >
@@ -52,6 +52,7 @@ I want a quick visual check of the data.
52
52
53
53
@savefig 04_airqual_quick.png
54
54
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.
@@ -68,10 +69,19 @@ the columns with numeric data.
68
69
69
70
I want to plot only the columns of the data table with the data from Paris.
70
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
+
71
80
.. ipython :: python
72
81
73
82
@savefig 04_airqual_paris.png
74
83
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 `
@@ -94,6 +104,7 @@ I want to visually compare the :math:`NO_2` values measured in London versus Par
94
104
95
105
@savefig 04_airqual_scatter.png
96
106
air_quality.plot.scatter(x = " station_london" , y = " station_paris" , alpha = 0.5 )
107
+ plt.show()
97
108
98
109
.. raw :: html
99
110
@@ -125,6 +136,7 @@ method is applicable on the air quality example data:
125
136
126
137
@savefig 04_airqual_boxplot.png
127
138
air_quality.plot.box()
139
+ plt.show()
128
140
129
141
.. raw :: html
130
142
@@ -148,6 +160,7 @@ I want each of the columns in a separate subplot.
148
160
149
161
@savefig 04_airqual_area_subplot.png
150
162
axs = air_quality.plot.area(figsize = (12 , 4 ), subplots = True )
163
+ plt.show()
151
164
152
165
Separate subplots for each of the data columns are supported by the ``subplots `` argument
153
166
of the ``plot `` functions. The builtin options available in each of the pandas plot
@@ -180,9 +193,10 @@ I want to further customize, extend or save the resulting plot.
180
193
181
194
fig, axs = plt.subplots(figsize = (12 , 4 ))
182
195
air_quality.plot.area(ax = axs)
183
- @savefig 04_airqual_customized.png
184
196
axs.set_ylabel(" NO$_2$ concentration" )
197
+ @savefig 04_airqual_customized.png
185
198
fig.savefig(" no2_concentrations.png" )
199
+ plt.show()
186
200
187
201
.. ipython :: python
188
202
:suppress:
@@ -208,6 +222,7 @@ This strategy is applied in the previous example:
208
222
air_quality.plot.area(ax=axs) # Use pandas to put the area plot on the prepared Figure/Axes
209
223
axs.set_ylabel("NO$_2$ concentration") # Do any Matplotlib customization you like
210
224
fig.savefig("no2_concentrations.png") # Save the Figure/Axes using the existing Matplotlib method.
225
+ plt.show() # Display the plot
211
226
212
227
.. raw :: html
213
228
0 commit comments