Skip to content

Commit 1a4cac7

Browse files
improved barplot text in viz; title setting
1 parent cf55260 commit 1a4cac7

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

source/viz.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,12 +1247,10 @@ and allows us to answer our initial questions:
12471247
"Are the seven continents Earth's largest landmasses?"
12481248
and "Which are the next few largest landmasses?".
12491249
However, we could still improve this visualization
1250-
by organizing the bars by landmass size rather than by alphabetical order
1251-
and by coloring the bars based on whether they correspond to a continent.
1252-
The data for this is stored in the `landmass_type` column.
1253-
To use this to color the bars,
1250+
by coloring the bars based on whether they correspond to a continent, and
1251+
by organizing the bars by landmass size rather than by alphabetical order.
1252+
The data for coloring the bars is stored in the `landmass_type` column, so
12541253
we set the `color` encoding to `landmass_type`.
1255-
12561254
To organize the landmasses by their `size` variable,
12571255
we will use the altair `sort` function
12581256
in the y-encoding of the chart.
@@ -1262,18 +1260,37 @@ This plots the values on `y` axis
12621260
in the ascending order of `x` axis values.
12631261
This creates a chart where the largest bar is the closest to the axis line,
12641262
which is generally the most visually appealing when sorting bars.
1265-
If instead
1266-
we want to sort the values on `y-axis` in descending order of `x-axis`,
1267-
we can add a minus sign to reverse the order and specify `sort="-x"`.
1263+
If instead we wanted to sort the values on `y-axis` in descending order of `x-axis`,
1264+
we could add a minus sign to reverse the order and specify `sort="-x"`.
12681265

12691266
```{index} altair; sort
12701267
```
12711268

1272-
```{code-cell} ipython3
1273-
islands_plot_sorted = alt.Chart(islands_top12).mark_bar().encode(
1274-
x="size",
1275-
y=alt.Y("landmass").sort("x"),
1276-
color=alt.Color("landmass_type")
1269+
To finalize this plot we will customize the axis and legend labels using the `title` method,
1270+
and add a title to the chart by specifying the `title` argument of `alt.Chart`.
1271+
Plot titles are not always required, especially when it would be redundant with an already-existing
1272+
caption or surrounding context (e.g., in a slide presentation with annotations).
1273+
But if you decide to include one, a good plot title should provide the take home message
1274+
that you want readers to focus on, e.g., "The Earth's seven largest landmasses are all continents,"
1275+
but it could also more general, e.g., "The twelve largest landmasses on Earth."
1276+
1277+
Note that
1278+
For categorical encodings,
1279+
such as the color and y channels in our chart,
1280+
it is often not necessary to include the axis title
1281+
as the labels of the categories are enough by themselves.
1282+
Particularly in this case where the title clearly states
1283+
that we are landmasses,
1284+
the titles are redundant and we can remove them.
1285+
1286+
```{code-cell} ipython3
1287+
islands_plot_sorted = alt.Chart(
1288+
islands_top12,
1289+
title="The Earth's seven largest landmasses are all continents"
1290+
).mark_bar().encode(
1291+
x=alt.X("size").title("Size (1000 square mi)"),
1292+
y=alt.Y("landmass").sort("x").title("Landmass"),
1293+
color=alt.Color("landmass_type").title("Type")
12771294
)
12781295
```
12791296

0 commit comments

Comments
 (0)