Python Data Visualization Transcripts
Chapter: Plotly
Lecture: Regression treemap heatmap

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now I'm going to continue showing some additional plot types by creating a new data frame that we call average by year.
0:08 And this is a simple group by where we group each value by year and they get the average fuel cost. So I have this simple data frame and I'm gonna
0:18 do a scatter plot but actually add a trend line and show how easy that is to do in plotly express.
0:26 We've already gone through creating a scatter plot but I've changed it by adding trend line
0:31 equals ols. And what that will do is to plot the data and then add a trend line. And the hover ability that we've shown also applies to this trend
0:41 line. So I can see what the formula it develops as well as the R Squared and what the trended value is along each data point.
0:50 Now you may look at that plot and say that it doesn't look like a good trend line. If we want to add a Lowess trend line,
0:58 we can do that as well which generates more of a nonlinear plot. But the same hover ability as well.
1:05 So this is just a really simple example to show how useful plotly express can be for scatter plots and adding trend lines.
1:13 One of the other things I really like about plotly expresses. There are some really unique plots and I'm going to go through one called a tree
1:19 map which you may have seen before and I'm gonna show the plot and then I'll walk through what it does and this gives us each vehicle.
1:27 So if we want to see who the manufacturers are and what types of vehicles they
1:32 manufacture. So we can see that this is the whole universe in this dataset. Chevrolet has 1935 vehicles. They have cars,
1:41 pickups, SUVs and then if you look at jeep they all have SUVs. So this is kind of a really fun visualization and this is what the tree map
1:51 function does. You passing your data frame and you tell it the path to follow So we start with all,
1:57 this is a handy 'px.Constant' function that covers this little box right here to
2:04 make sure everything is included and then we tell it to make and then the class
2:08 summary, which we've developed and then it counts the number of vehicles.
2:13 So it's just a really fun visualization shows the power of that simple api that we've talked through with plotly Express for a tree map.
2:21 If we want to follow the same thing. There's another plot type called a sunburst plot which uses a similar sort of structure where
2:28 we specify the path, but it does it in a circle. So it shows how they are all related to the BMW cars SUVs ford, really kind of a fun plot.
2:40 This has a lot of data, so it may not be too useful, but it just highlights how much flexibility you have with plotly express out of the
2:48 box, we'll go through another example just to show that average by year by class and how we can use that with a sunburst.
2:57 So we can use that to zero in on a specific time frame. So it's a little easier to read.
3:02 So now we just look at in this case I started the year and then class summary. So you can see for 2018, here's the cars SUVs and pickups,
3:11 2020 car suv and pickup in the number in each bucket. And then the final plot I want to do is a heat map. So we've talked about these a little bit.
3:21 Let's do a heat map of the year in class summary. So now we can see by each year what vehicles there are in the count. So we have our low to high.
3:34 So you can see that the vast majority of our vehicles, our cars, not surprisingly and see what the difference is for SUVs,
3:41 how that number is starting to grow over time. And the final thing I want to show is this density map.
3:49 But we can put some other information on here together. So now we have our heat map in plotly terms,
3:57 it's called a density map. And so we have our year versus our highway fuel economy. And then we use the marginal wide to show histogram similar to
4:06 what we showed with the box plot in a previous plot. Now we have a histogram of the highway 08 fuel economy,
4:13 showing that distribution. I use this as an example to put the template in there to change the visualization a little bit.
4:20 And now I'm defining the wide range from 10 to 40 because when it was too wide it really was including a lot of the outlier values.
4:29 So this gives us a little bit more granularity to see where the majority of the
4:36 highway fuel economy is over time and how it is starting to trend up over time But once again, this just shows the power of the plotly. express API
4:48 And how you can do some really complex visualizations with very little amounts of code.

Talk Python's Mastodon Michael Kennedy's Mastodon