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/visualizing-matplotlib.md
+58-3Lines changed: 58 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,7 +170,7 @@ As arguments in this function, we add the kind of plot we want, and how our data
170
170
With the following code, we will make a scatter plot (argument `kind = "scatter"`) to analyze the relationship between the weight (which will plot in the x axis, argument `x = "weight"`) and the hindfoot length (in the y axis, argument `y = "hindfoot_length"`) of the animals sampled at the study site.
This scatter plot shows there seems to be a positive relation between weight and hindfoot length, were heavier animal tend to have bigger hindfoots.
@@ -196,11 +196,66 @@ As shown in the previous image, the x-axis labels overlap with each other, which
196
196
At this point we realize a more fine-grained control over our graph is needed, and here is where Matplotlib shows in the picture.
197
197
198
198
199
+
200
+
199
201
## Advanced plots with Matplotlib
200
202
201
-
[Matplotlib](https://matplotlib.org/) is a Python library that is widely used throughout the scientific Python community to create high-quality and publication-ready graphics. It supports a wide range of raster and vector graphics formats including PNG, PostScript, EPS, PDF and SVG.
203
+
[Matplotlib](https://matplotlib.org/) is a Python library that is widely used throughout the scientific Python community to create high-quality and publication-ready graphics.
204
+
It supports a wide range of raster and vector graphics formats including PNG, PostScript, EPS, PDF and SVG.
205
+
206
+
Moreover, matplotlib is the actual engine behind the plotting capabilities of Pandas, and other plotting libraries like seaborn and plotnine.
207
+
For example, when we call the `.plot()` methods on Pandas data objects, we are actually using the matplotlib library in the backstage.
208
+
209
+
Let's start by recreating our scatter plot, but this time, using matplotlib.
210
+
We'll do it one step at a time.
211
+
The first thing we need is to create our figure and our axes (or plots), using the `.subplots()` function.
212
+
The `fig` object we are creating is the entire plot area, which can contain one or multiple axes.
213
+
In this case, we will have only one set of axes, which is the `ax` object.
214
+
Only this line of code will result in an empty plot.
215
+
We show our plot with the `plt.show()` function
216
+
217
+
```python
218
+
fig, ax = plt.subplots()
219
+
plt.show()
220
+
```
221
+
222
+
If we want more than one plot in the same figure, we could specify the number of rows (`nrows` argument) and the number of columns (`ncols`) in this function.
223
+
For example, let's say we want two plots (or axes), organized in two columns and one row.
224
+
225
+
```python
226
+
fig, ax = plt.subplots(nrows=1, ncols=2)
227
+
plt.show()
228
+
```
229
+
230
+
Let's focus for now only in making our scatter plot, so just one set of axes.
231
+
For this, in our created `ax` axes, we'll modify it with the `.scatter()` function.
232
+
In the x axis, we'll use the `weight` column, so we use the argument `x = complete_old["weight"]`.
233
+
In the y axis, we'll use the `handfoot_length` column, so we use the argument `y = complete_old["hindfoot_length"]`.
Moreover, matplotlib is the actual engine behind the plotting capabilities of Pandas, and other plotting libraries like seaborn and plotnine. For example, when we call the `.plot()` methods on Pandas data objects, we are actually using the matplotlib library in the backstage.
0 commit comments