Skip to content

Commit 9cf4089

Browse files
authored
Grammar
1 parent 849fbd4 commit 9cf4089

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

lessons/11_plotting_basics.qmd

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ First, we will import the `MatPlotLib` and `Pandas` libraries as well as load `n
6060
```{python}
6161
#| label: tbl-load_new_metadata
6262
#| tbl-cap: DataFrame containing updated metadata for each of our 12 samples.
63+
# Import libraries
6364
import matplotlib.pyplot as plt
6465
import pandas as pd
6566
@@ -86,10 +87,9 @@ So first, we'll create an empty plot of size 8 inches by 6 inches:
8687
plt.figure(figsize=(8, 6))
8788
```
8889

89-
9090
### Adding a Scatterplot Layer
9191

92-
We will once again first initialize the plot with `figure()` and then add the scatterplot layer to the plot with `scatter()`. We need to specify the `x` and `y` values for our scatterplot, which in this case will be `age_in_days` for `x` and `mean_expression` for `y`.
92+
We will once again first initialize the plot with `figure()` and then add the scatterplot layer to the plot with `scatter()`. We need to specify where we are pulling the data to plot from, which in this case will be the `new_metadata` DataFrame, and the `x` and `y` values for our scatterplot, which in this case will be `age_in_days` for `x` and `mean_expression` for `y`.
9393

9494
The `plt.figure` and `plt.scatter` calls are connected because `MatPlotLib` commands build upon each other. The `plt.figure` command initializes the plot and sets the size, while the `plt.scatter` command adds the scatterplot layer to the existing plot. When we call these functions sequentially, we are building our plot layer by layer.
9595

@@ -99,10 +99,27 @@ The `plt.figure` and `plt.scatter` calls are connected because `MatPlotLib` comm
9999
# Initialize a plot with a specific size
100100
plt.figure(figsize=(8, 6))
101101
102+
# Add a scatterplot layer to the plot
103+
plt.scatter(data = new_metadata,
104+
x="age_in_days",
105+
y="mean_expression")
106+
```
107+
108+
::: callout-note
109+
# Alternative
110+
Instead of providing the `data` argument, you could specify the `x` and `y` axes as the given columns from `new_metadata`.
111+
112+
```{python}
113+
#| label: fig-add_scatterplot_layer_alt
114+
#| eval: false
115+
# Initialize a plot with a specific size
116+
plt.figure(figsize=(8, 6))
117+
102118
# Add a scatterplot layer to the plot
103119
plt.scatter(x=new_metadata["age_in_days"],
104120
y=new_metadata["mean_expression"])
105121
```
122+
:::
106123

107124
Now that we have the required fundamentals, let’s add some extra details like color to the plot. We can color the points on the plot based on the genotype column with the `c` argument.
108125

@@ -126,12 +143,14 @@ plt.scatter(data = new_metadata,
126143

127144
To work around the error from `plt.scatter`, we will instead use the `seaborn` package's `scatterplot()` function and use the `hue` argument instead of `c`, which allows us to specify a categorical variable in order to color the plot points. The documentation for `seaborn.scatterplot()` is quite extensive and can be found [on their official website](https://seaborn.pydata.org/generated/seaborn.scatterplot.html).
128145

129-
You will notice that there are a default set of colors that we can use so we do not have to specify a color. The legend has also been automatically plotted for us!
146+
You will notice that there are a default set of colors that we can use, so we do not have to specify a color. The legend and axis labels have also been automatically plotted for us!
130147

131148
```{python}
132149
#| label: fig-add_color_scatterplot_layer
133150
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype.
151+
# Import library
134152
import seaborn as sns
153+
135154
# Initialize a plot with a specific size
136155
plt.figure(figsize=(8, 6))
137156
@@ -165,7 +184,7 @@ The data points are quite small. We can also adjust the `s` (size) of the data p
165184

166185
```{python}
167186
#| label: fig-add_size_scatterplot_layer
168-
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype with adjusted size.
187+
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype, with adjusted size.
169188
# Initialize a plot with a specific size
170189
plt.figure(figsize=(8, 6))
171190
@@ -178,14 +197,13 @@ sns.scatterplot(data = new_metadata,
178197
s=50)
179198
```
180199

181-
182200
### Themes
183201

184202
There are a variety of themes that you can apply to your plot to change the background and gridlines. The default theme is `darkgrid`, but you can change it with the `set_style()` function from `seaborn`.
185203

186204
```{python}
187205
#| label: fig-change_theme
188-
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype with adjusted size and a different theme.
206+
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype, with adjusted size and a different theme.
189207
190208
# Set the theme to "whitegrid"
191209
sns.set_style(style = "whitegrid")
@@ -207,16 +225,15 @@ sns.scatterplot(data = new_metadata,
207225
You can also customize themes further with `rc_params` when you want to adjust specific elements of the theme. The documentation for `set_style()` can be found [on their official website](https://seaborn.pydata.org/generated/seaborn.set_style.html).
208226
:::
209227

210-
211228
### Changing Labels
212229

213-
The axis labels and tick labels don't get any larger by changing themes? We can, however, change both the x-axis labels and size labels with the `plt.xlabel()` functions from `matplotlib`. Since we will be adding this layer “on top” of, or after, `sns.set_style()`, any features we change will override what is set by the `sns.set_style()` layer.
230+
The axis labels and tick labels don't get any larger by changing themes. We can, however, change both the x-axis labels and size labels with the `plt.xlabel()` functions from `matplotlib`. Since we will be adding this layer “on top” of, or after, `sns.set_style()`, any features we change will override what is set by the `sns.set_style()` layer.
214231

215232
Let’s increase the size of the x-axis titles to be 20.
216233

217234
```{python}
218235
#| label: fig-change_axis_label_size
219-
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype with adjusted size, a different theme and larger x-axis title.
236+
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype and shaped by celltype, with adjusted size, a different theme and larger x-axis title.
220237
# Set the theme to "whitegrid"
221238
sns.set_style(style = "whitegrid")
222239
@@ -231,7 +248,7 @@ sns.scatterplot(data = new_metadata,
231248
style="celltype",
232249
s=50)
233250
234-
# Change the size of the axis label
251+
# Change the size and text of the axis label
235252
plt.xlabel(xlabel = "Age in Days",
236253
fontsize=20)
237254
```
@@ -257,7 +274,7 @@ sns.scatterplot(data = new_metadata,
257274
style="celltype",
258275
s=50)
259276
260-
# Change the size of the axis label
277+
# Change the size and text of the axis label
261278
plt.xlabel(xlabel = "Age in Days",
262279
fontsize=20)
263280
@@ -285,7 +302,7 @@ sns.scatterplot(data = new_metadata,
285302
style="celltype",
286303
s=50)
287304
288-
# Change the size of the axis label
305+
# Change the size and text of the axis label
289306
plt.xlabel(xlabel = "Age in Days",
290307
fontsize=20)
291308
@@ -295,7 +312,6 @@ plt.savefig(fname = "figures/scatterplot_dpi.png",
295312
dpi = 300)
296313
```
297314

298-
299315
:::{.callout-tip}
300316
# [**Exercise 1**](11_plotting_basics-Answer_key.qmd#exercise-1)
301317
1. Add an `plt.ylabel()` layers to the current plot such that the y-axis is labeled "Mean expression".
@@ -312,4 +328,4 @@ plt.savefig(fname = "figures/scatterplot_dpi.png",
312328

313329
[Next Lesson >>](12_boxplots.qmd)
314330

315-
[Back to Schedule](../schedule/schedule.qmd)
331+
[Back to Schedule](../schedule/schedule.qmd)

0 commit comments

Comments
 (0)