Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Plotting the World16:35 with Ken Alger
Charts and graphs are definitely a step up from viewing data in a spreadsheet. Let's take a look at how Bokeh can take visualizations even further.
Country coordinate URL:
Code for parsing JSON Data:
def get_coordinates(features): depth = lambda L: isinstance(L, list) and max(map(depth, L))+1 country_id =  longitudes =  latitudes =  for feature in json_data['features']: coordinates = feature['geometry']['coordinates'] number_dimensions = depth(coordinates) # one border if number_dimensions == 3: country_id.append(feature['id']) points = np.array(coordinates, 'f') longitudes.append(points[:, 0]) latitudes.append(points[:, 1]) # several borders else: for shape in coordinates: country_id.append(feature['id']) points = np.array(shape, 'f') longitudes.append(points[:, 0]) latitudes.append(points[:, 1]) return country_id, longitudes, latitudes
Now that we have seen how we can plot data to see if there's potentially 0:00 a correlation between two factors, like population and life expectancy. 0:04 It might be interesting to see if we can visualize out data in a different way, 0:09 something beyond the normal bar chart, or scatter plot. 0:13 First though, now that we have a basic understanding of Bokeh, 0:17 let's take a slight step back and 0:21 see a few other features that Bokeh provides in order to see if we can 0:23 generate a potentially more interesting visualization of a population data. 0:27 We have been using chip glyphs for 0:31 our scatter plot, which have been very relevant for our work this far. 0:33 Another method we can call in our figure object is called patches, 0:38 which allows us to draw shapes based on parsed-in coordinates. 0:42 We can then use the fill color parameter to set the color of our shape. 0:47 Let's start with some basic shapes to see how this works, 0:52 along with some additional parameters we can use and see if we can't generate 0:55 something that might be interesting with our raw population data. 0:59 To get started, lets go into a new file called shapes.pie, 1:03 and set up our imports from bokeh.plotting, 1:08 we'll want figure, output_file and show. 1:11 Now we need to name our output file. 1:17 Lets call it shapes.html. 1:19 Next, we need to create our figure and assign it a name. 1:22 For now, lets just call it plot. 1:25 We'll make the width and height 400 pixels. 1:28 And we'll give it a title called shape. 1:39 Great, now we're wanting to draw a shape. 1:42 For a single shape, we will use the patch method. 1:46 But we'll see the patches method here shortly as well. 1:49 We provide a list of x values to use along with an equal length list of y values into 1:52 the patch method, which results in that x, y coordinate pairing again. 1:57 Then let's assign a color and alpha value and align width of two pixels. 2:01 Patch do 1, 2, 3 and 4 for x values, try 7, 12, 9, and 3 for our y values. 2:08 For our color, 2B5B84 for 2:18 the hex value, 0.7. 2:25 Line_width = 2, which looks like it puts us or 2:32 our [INAUDIBLE] for line length so we'll knock that down there. 2:35 That's looking good. 2:41 Now we add our method to show our plot. 2:43 And run our script. 2:47 We see that Bokeh generates our shapes.html output file and 2:53 displays it in our browser thanks Bokeh. 2:57 We should see a four sided polygon, 3:00 filled with a slightly transparent blue color displayed in our browser. 3:03 But what if we want to draw multiple shapes on the same figure? 3:06 That's where patches comes in. 3:10 As the names would indicate, we will use patch for single polygons and 3:12 patches for displaying multiple polygons. 3:16 Let's generate another plot. 3:19 Plot multiple. 3:24 Will use the same plot width and height. 3:24 And we'll call it multiple shapes. 3:36 Now, to generate multiple shapes, 3:43 we need to parse in multiple arguments into our parameters in the form of list. 3:45 For our coordinates, we parse in a list of the x values for 3:50 our shapes, then a list of our y values. 3:53 Again, the list of the x and 3:56 y coordinates have to be the same length overall for each shape itself. 3:58 Since we'll be generating multiple polygons, we will want the patches method, 4:02 plot_multiple.patches. 4:07 As values for our first shape, we'll use [1, 1, 4:12 4, 4], [3, 5, 9] for a second shape. 4:17 y values [1, 4, 4, 1], [1, 4:22 9, 3] for our second shape. 4:27 It should come as no surprise here. 4:31 They will need to have list of the same length for our color, author, 4:33 and in line with values as well. 4:37 Hash first color, For our second color, 4:39 First alpha and the second alpha. 4:55 And line widths of 2 and 3. 5:03 Let's display this new plot at that same time next to the previous plot. 5:05 For that, we'll pass in row into the show method. 5:09 And we can't forget to import row. 5:23 From bokeh.layout import row. 5:33 Let's run our script and take a look. 5:38 There, we have our two plots. 5:43 One with a single blue tone polygon and the latest one with a green square and 5:45 overlapping gray triangle. 5:50 Now that we have some experience with plotting shapes, 5:52 let's get into drawing something a little more complex. 5:55 We should be able to use longitude and latitude coordinates as our x and 5:58 y values to generate a world map. 6:02 I have included a link to a JSON resource that has longitude and 6:05 latitude coordinates for most of the countries and land masses of the world. 6:09 We'll read them the coordinates as x and y values for 6:13 apaches plug and then have bokeh draw our map. 6:16 Let's move into a new file called the world_map.py. 6:20 To get started, we will want to import, request, and numpy. 6:24 We'll use request to get our JSON data, 6:28 and we'll want to bring it on our Bokeh import as well. 6:30 We'll need output_file and show, and since we'll be using HoverTool, 6:32 column data source and figure, we'll want to import those as well. 6:36 ColumnDataSource and figure. 6:46 We'll name our output_file world-map.html and 6:52 we'll give it a title of World Map We'll 6:58 create a URL convenience variable for our JSON string. 7:05 Again, check the teachers notes. 7:10 We'll be using the same tools we have been using. 7:13 So let's make it tools variable for those two. 7:16 Pan, wheel_zoom, box_zoom, 7:21 reset, hover and save. 7:27 Next, we need to use request to get the JSON data from our URL, 7:33 request.get our (url) and our json_data 7:39 We get our json_data. 7:46 Okay, this data set is a little interesting in that the coordinates for 7:48 each country can be represented at different depths in the JSON object, 7:52 dependent on the continuity of the country's borders. 7:57 If a country is a single polygon such as Uganda, 8:00 it is represented in a single list of coordinates. 8:04 If however, it is a multi-polygon, for example, the Philippines with multiple 8:07 different land masses, it is represented in a multi-depth list in the JSON data. 8:12 I have provided a function that parses through these different cases and 8:17 generates the required x and y pairs for Bokeh to use. 8:21 Check the teachers notes for a copy of the code. 8:25 Now we can get our coordinates and set up our plot. 8:28 We'll configure our plot, call this plot world_map_plot. 8:45 We'll make the plot width a little wider here to make the map look correct. 8:52 Our tools will be our TOOLS. 9:11 X_range of the graph will be -180 to 180. 9:15 y_range -90 to 90. 9:22 There we go. 9:28 Now we assign it the patches method. 9:31 For our x values we'll use lats, for y values we'll use longs. 9:35 Our fill_color, 9:42 let's us #F1EEF6, 9:46 fill_alpha 0.7 and 9:50 align_width will be 2. 9:55 Now we can show our plot. 10:04 And run. 10:10 We are presented with a nice visualization of a map of the world. 10:14 We can definitely use this map for our world population data visualization. 10:17 We'll need to convert our country data with latitudes and 10:22 longitudes into a pandas data frame, 10:25 merge it with our population data, and use that as a column data source. 10:29 Let's create an empty list to handle our country geospacial data. 10:33 Country_coords, and it populates 10:36 that list with our information. 10:42 Land_mass. 10:58 Country_code is gonna be country. 11:04 Longitudes Longs. 11:21 We'll append land_mass to our country coordinates. 11:36 Then, we can convert that into a pandas data frame to prepare it for 11:44 an upcoming merge. 11:48 But we didn't import pandas. 11:58 So let's go back up and do that. 12:00 There we go. 12:07 All right, next, we bring in our population data we were working with in 12:11 an earlier stage, as a data frame. 12:15 Input_file, these are country-pops.csv. 12:20 Country populations. 12:33 There we go. 12:40 And then we merge the two datasets together using pandas merge function for 12:42 use as a column data source. 12:47 We can merge on the three letter ISO value from country-pops.csv. 12:49 And then country code value from the JSON file in an outer join cell merge. 12:54 Three from our country codes. 13:09 Country code and how is outer. 13:17 With our merged data, we can create a column data source. 13:27 Then, we can figure our patches plot to utilize our column data source 13:41 information. 13:46 So we'll replace our x values here. 13:48 And then add in our source, As our map data. 14:08 Finally, we generate our hover tools. 14:14 In our hover tools, we'll keep the same data as we've seen before, 14:17 country name, population, and life expectancy. 14:21 Feel free to add in additional data to the tool tips on your own. 14:25 We want country name in English To map to our country English, right? 14:56 Population. 15:12 Map to population. 15:19 Life Expectancy in years. 15:23 To map to life expectancy. 15:32 Let's try it out. 15:39 Awesome. 15:43 Now when we hover over a country, we get our data included with the hover tool. 15:44 We can examine the different countries on the map and 15:48 visually see, in an interactive fashion, what the data is for each country. 15:52 Further, since we are all generally familiar with the world map, 15:57 it provides much better context for 16:01 us about where population centers are on a worldwide level. 16:03 That is in my opinion a pretty nice 16:07 upgrade to visualizing our world population data in a spreadsheet. 16:10 This is just the beginning of your journey with data visualization though. 16:16 There are many, many different ways to explore datasets and 16:19 we have just scratched the surface for what Bokeh can do. 16:22 Leave a message in the community forum or contact me on Twitter and 16:25 let me know how you have used Bokeh to better understand data. 16:29 I'll see you next time, and happy coding. 16:32
You need to sign up for Treehouse in order to download course files.Sign up