Interactive Data Visualization with Python by Anshu Kumar & Shubhangi Hora & Sharath Chandra Guntuku & Abha Belorkar

Interactive Data Visualization with Python by Anshu Kumar & Shubhangi Hora & Sharath Chandra Guntuku & Abha Belorkar

Author:Anshu Kumar & Shubhangi Hora & Sharath Chandra Guntuku & Abha Belorkar [Anshu Kumar]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2019-10-23T16:00:00+00:00


Add the layer function from the altair library:import altair as alt

bars = alt.Chart().mark_bar().encode(

x='Region:N',

y='mean(Happy Planet Index):Q',

).properties(width=400)

line = alt.Chart().mark_rule(color='firebrick').encode(

y='mean(Happy Planet Index):Q',

size=alt.SizeValue(3)

)

alt.layer(bars, line, data=hpi_df)

The output is as follows:

Figure 4.20: Showing the mean on the bar plot

So, now we know that the mean Happy Planet Index across all regions is around 26. Looks like there's a lot more happiness that our planet could take. Interesting!

By the way, you should also note that we didn't specify the dataset until we used the layer function. That is, we did not provide the hpi_df dataset in the Chart() function as we would usually do. Instead, we mentioned it in the layer function with the data=hpi_df parameter.

Now that you know about the concept of layering in altair, you can be trusted with a shortcut for it. Just write code independently for different plots, as you would usually write it, then use the + operator, as shown in the following example!



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.