Colors and Legends6:12 with Ken Alger
Adding colors and legends to graphs and charts helps to make data easier to understand. Bokeh makes the process easy.
color_mapper = CategoricalColorMapper(factors=[‘Asia’, ‘Africa’, ‘Antarctica’, ‘Australia’, ‘Central America’, ‘Europe’, ‘North America’, “Oceania’, ‘South America’], palette=[‘#00FF00’, ‘#FFD343’, ‘darkgray’, ‘brown’, ‘cyan’, ‘crimson’, ‘red’, ‘#0000FF’, ‘purple’])
When we were last looking under data, 0:00 we were using a scatter plot to see if there were any correlation between 0:02 a country's population and life expectancy from our world population dataset. 0:05 We used Bokeh column data source to map our data into usable columns and 0:09 were able to generate the scatter plot to get a better idea of any relationship 0:13 between life expectancy and population. 0:17 There are couple of issues with our last plot though. 0:19 Everything was cluttered and same color, and nothing was labeled. 0:22 Obviously, this makes it a challenge to come with any conclusions about our 0:26 dataset. 0:30 Bokeh includes some great tools for this which allow us to map colors to 0:31 specific items or categories of items to quickly add legends to our visualizations. 0:36 If we remember back when we looked at the headers in our data set, one of the pieces 0:43 of data we get about each country is the continent on which the country resides. 0:47 It might be nice, 0:51 when we're trying to figure out a correlation between life expectancy and 0:52 population, if region of the world leads to any insightful information. 0:55 One of the functions Bokeh provides is categorical color mapper. 1:01 Which as the name would indicate maps colors to different 1:05 categorical data buckets. 1:09 This allows to utilize different categories or 1:11 in Bokeh terms factors which in our case are countries and 1:13 assigned certain color values, a pallet to each category. 1:17 One of the functions Bokeh provides is categorical color mapper, which, 1:22 as the name would indicate, maps colors to different categorical data buckets. 1:28 This allows us to utilize different categories, or 1:33 in Bokeh's terms, factors, which in our case are countries, and 1:36 assigned certain color values, palette, to each category. 1:39 The colors we pass into the categorical color mapper function can be any valid CSS 1:44 or hex color, meaning we can use either green, or 1:49 your favorite #00FF00 for our value. 1:53 Let's jump back into our project and add some colors to our continent buckets and 1:57 then show how we can add a legend to our plot to add context to the information. 2:01 Here's where we left off at the end of our last video. 2:06 To get started adding some color, we need to add a color mapper to our code so 2:08 that we can assign specific colors to our continents. 2:12 We'll import categorical color mapper from bokeh.models, then create our color mapper 2:16 with our continent buckets or factors, and then assign each factor a color. 2:22 From bokeh.models import 2:26 CategoricalColorMapper. 2:31 Now, we look at all of the continent options in our entire data set, 2:36 we see that we have ten different buckets, Asia, Africa, Antarctica, 2:40 Australia, Central America, Europe, North America, Oceana, and South America. 2:45 We pass those in as a list into the factors argument 2:52 of CategoricalColorMapper. 2:54 Then in a matching linked list, pass in our colors to the palette argument. 2:56 I've already typed this out and I'll paste it in here. 3:00 I've included that in the teachers note to save you from having to type it 3:10 in as well. 3:13 Then we can use our color mapper in our plot. 3:14 We do that by adding another argument one we call plot.circle. 3:17 Or whatever glyph we're using in our plot with the color parameter. 3:20 We'll pass into the color parameter a dictionary and 3:24 which field we are basing our color mapper on and the associated color mapper. 3:28 At this point we should remove our five country limitation though so 3:33 we can see all of the countries represented. 3:37 Let's also, just for fun, change our glyph from a circle to a diamond. 3:40 Just to show some other available glyphs. 3:45 We'll also adjust the size of our glyph down slightly. 3:47 Then when we show our plot and 3:50 run our script, we should see our data separated by color. 3:52 So we'll change circle to diamond. 3:55 Change our size down to 10. 4:02 And then add in our color paramter which the dict. 4:06 Based on the continent field And 4:12 a transform parameter Is our color_mapper. 4:18 Now, we should be able to run our script. 4:29 Nice, we have our data split out into buckets and 4:32 associated with each other by color based on which continent a country resides. 4:35 We are still missing a way to know which color represents which continent though. 4:40 We need a legend for our plot. 4:43 Fortunately that is easily done with Bokeh by passing in 4:45 a legend parameter with the column name that we want to use in the legend. 4:49 In this case continent. 4:53 So we can add our legend. 4:54 And base it on our continent field. 5:01 And run our script again. 5:05 Well that certainly helps but 5:09 it is sitting right on top of some of our data points. 5:11 Let’s adjust the location of the legend and 5:14 move it to the bottom right of our plot and add a background color to it 5:16 as well to help it stand out visually before we show the plot again. 5:20 We can do that by adjusting the plot.legend and 5:23 assigning it a location, bottom_right. 5:28 And the background fill_color. 5:39 I will do lightgrey. 5:45 And show our plot again. 5:53 This is looking more better as we can get a general idea of at least 5:56 which continents the countries are on and start to put some context to our data. 6:00 This still isn't super helpful though as our glyphs 6:04 aren't currently tagged in any way as to which country they represent. 6:07 Let's tackle that in our next video. 6:11
You need to sign up for Treehouse in order to download course files.Sign up