You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/data-visualisation.md
+18-22Lines changed: 18 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,46 +177,42 @@ The drop from 2012 through part of 2014 corresponds to the reconstruction period
177
177
::::::::::::::::::::::::::::::::::::::::::::::::
178
178
::::::::::::::::::::::::::::::::::::::::::::::::
179
179
180
-
## Use Matplotlib for more detailed charts
180
+
## Use Pandas for More Detailed Charts
181
181
182
-
What if we want to alter the axis labels and the title of the graph. In order to do that, we need to first import `matplotlib`, an extensive plotting package in Python that lets us alter all aspects of a graph.
183
-
184
-
- We can pass parameters to Matplotlib's `.plot()` function to assign a plot title, to declare a figsize - which accepts a width and height in inches - and to change the color of the line.
185
-
- Next we'll add text labels for the x and y axis using `.xlabel()` and `.ylabel`.
186
-
- Finally, we need a separate function `.show()` to display the plot using Matplotlib.
182
+
What if we want to alter the axis labels and the title of the graph? Pandas' built-in plotting functions, which are backended by Matplotlib, allow us to customize various aspects of a plot without needing to import Matplotlib directly.
187
183
184
+
- We can pass parameters to Pandas' `.plot()` function to add a plot title, specify a figure size, and change the color of the line.
185
+
- Additionally, we can directly set the x and y axis labels within the `.plot()` function.
188
186
189
187
```python
190
-
# the plotting package pandas is using under the hood to `plot()`
191
-
import matplotlib.pyplot as plt
192
-
193
-
albany['circulation'].plot(title='Circulation Count Over Time', figsize=(10, 5), color='blue')
194
-
# Adding labels and showing the plot
195
-
plt.xlabel('Date')
196
-
plt.ylabel('Circulation Count')
197
-
plt.show()
188
+
albany['circulation'].plot(title='Circulation Count Over Time',
189
+
figsize=(10, 5),
190
+
color='blue',
191
+
xlabel='Date',
192
+
ylabel='Circulation Count')
198
193
```
199
194
200
195
{alt="Line plot of the Albany Park branch circulation with matplotlib styles applied."}
201
196
202
-
### Changing plot types with Matplotlib
197
+
### Changing plot types
203
198
204
199
What if we want to use a different plot type for this graphic? To do so, we can change the `kind` parameters in our `.plot()` function.
205
200
206
201
```python
207
-
albany['circulation'].plot(kind='area', title='Circulation Count Area Plot at Albany Park', alpha=0.5)
208
-
plt.xlabel('Date')
209
-
plt.ylabel('Circulation Count')
210
-
plt.show()
202
+
albany['circulation'].plot(kind='area',
203
+
title='Circulation Count Area Plot at Albany Park', alpha=0.5,
204
+
xlabel='Date',
205
+
ylabel='Circulation Count')
211
206
```
207
+
212
208
{alt="Area plot of the Albany Park branch circulation."}
213
209
214
210
We can also look at our circulation data as a histogram.
215
211
216
212
```python
217
-
albany['circulation'].plot(kind='hist', bins=20, title='Distribution of Circulation Counts at Albany Park')
218
-
plt.xlabel('Circulation Count')
219
-
plt.show()
213
+
albany['circulation'].plot(kind='hist', bins=20,
214
+
title='Distribution of Circulation Counts at Albany Park',
215
+
xlabel='Circulation Count')
220
216
```
221
217
222
218
{alt="histogram of the Albany branch circulation."}
0 commit comments