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: lessons/11_plotting_basics.qmd
+30-14Lines changed: 30 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -60,6 +60,7 @@ First, we will import the `MatPlotLib` and `Pandas` libraries as well as load `n
60
60
```{python}
61
61
#| label: tbl-load_new_metadata
62
62
#| tbl-cap: DataFrame containing updated metadata for each of our 12 samples.
63
+
# Import libraries
63
64
import matplotlib.pyplot as plt
64
65
import pandas as pd
65
66
@@ -86,10 +87,9 @@ So first, we'll create an empty plot of size 8 inches by 6 inches:
86
87
plt.figure(figsize=(8, 6))
87
88
```
88
89
89
-
90
90
### Adding a Scatterplot Layer
91
91
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`.
93
93
94
94
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.
95
95
@@ -99,10 +99,27 @@ The `plt.figure` and `plt.scatter` calls are connected because `MatPlotLib` comm
99
99
# Initialize a plot with a specific size
100
100
plt.figure(figsize=(8, 6))
101
101
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
+
102
118
# Add a scatterplot layer to the plot
103
119
plt.scatter(x=new_metadata["age_in_days"],
104
120
y=new_metadata["mean_expression"])
105
121
```
122
+
:::
106
123
107
124
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.
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).
128
145
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!
130
147
131
148
```{python}
132
149
#| label: fig-add_color_scatterplot_layer
133
150
#| fig-cap: Scatterplot of age in days vs. mean expression, colored by genotype.
151
+
# Import library
134
152
import seaborn as sns
153
+
135
154
# Initialize a plot with a specific size
136
155
plt.figure(figsize=(8, 6))
137
156
@@ -165,7 +184,7 @@ The data points are quite small. We can also adjust the `s` (size) of the data p
165
184
166
185
```{python}
167
186
#| 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.
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`.
185
203
186
204
```{python}
187
205
#| 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.
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).
208
226
:::
209
227
210
-
211
228
### Changing Labels
212
229
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.
214
231
215
232
Let’s increase the size of the x-axis titles to be 20.
216
233
217
234
```{python}
218
235
#| 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.
0 commit comments