Python Data Visualization Transcripts
Chapter: Plotly
Lecture: Facetting

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final concept I want to cover is facetting and it's actually relatively simple to do
0:07 this with Plotly Express. So I've created a new notebook for exercise three with
0:13 our standard data frame and I think the easiest way to show a facet is we'll do one for a histogram.
0:20 And what we want to show is the fuel cost distribution over two different date ranges from 2000 to 2010 and 2011 to 2020.
0:31 So the way we generate that is through our histogram. We tell it that the X axis is fuel cost.
0:36 08 and then we tell it to Facet the columns by date range as you recall, date range is field that we derived to break our datasets into two distinct
0:47 categories. So that's relatively simple and we can do the same thing by row if we like. Let's facet this by the drive column and now we have a date
1:01 range and a drive row a date range, column and drive row. So now you can see by date range if it's a
1:08 two wheel drive or a four wheel drive and what that distribution looks like. So facetting. Is really pretty simple with plotly express.
1:17 It's just these two additional parameters that we can pass to our plots. Let's say we want to control the visualization a little bit more.
1:26 Maybe we wanna facet by class summary but we want to wrap it at three columns So we use facet col wrap. We can also specify the number of bins.
1:36 So here we have a nice summary for the various class summaries of what the fuel
1:41 cost distribution looks like for each of those different types of vehicles. And I want to show a simple example.
1:48 One of the things that you may not like is the way it says class summary equals car class summary equals wagon.
1:53 There is a trick to get those filtered out. So if we just want to say car wagon,
1:59 pickup, suv and other we create our figure and then use for each annotation,
2:05 we write this lambda to update the text and split it on equals and return the last value. So it's a little little trick may not be very evident but I
2:16 wanted to throw that in there if you were looking at these various plots and wondered
2:19 how you could update it. It just shows that concept of creating your figure and
2:24 then updating it and then finally showing it and everything we've done for histogram We can do for box plots,
2:30 lets create a box plot with a couple other parameters. So now we have a box plot with our fuel type summary and the average fuel
2:38 cost. So we have a nice two by two grid. Let's walk through the code that does that. So we pass in our data frame the X and Y axis.
2:47 We tell it to facet on the fuel type summary. We tell it here that the box mode,
2:51 we want to group them together so that these two the two wheel drive and four wheel drives are grouped together. We want the color to be drive,
3:00 we don't want to show the point so it won't show the outlier points. And then here I'm introducing how to modify the height.
3:07 You could also modify the width if you'd like, but this just shows how you can start to use the different parameters for the various
3:14 plotting functions to customize the way your visualization appears.


Talk Python's Mastodon Michael Kennedy's Mastodon