Data Science Jumpstart with 10 Projects Transcripts
Chapter: Project 10: Making a Snow Report Dashboard with Dash and Plotly
Lecture: Creating a Bar plot in Plotly
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In this video, I'm going to show you how to do a bar plot with Plotly. Let's go through what we're trying to do here. I've got a little chain.
0:08
So I'm taking off the snow column. This is how much snow fell during a given day. I'm going to pipe that into PD cut.
0:15
This is going to put it into bins. So we're going to take this continuous variable and put it into 10 bins. I'll do a value counts of that.
0:24
So how many times did each bin occur? And then we'll do a bar plot of that. And when we do that, we get an error.
0:33
If we scroll down, we see that it says, object of type interval is not JSON serializable.
0:39
Scroll down a little bit more and see if we can make sense of that. Remember, pandas generally is going to put the index in that x-axis.
0:46
And that is an interval or categorical. And it doesn't work with Plotly. It needs to be JSON serializable. So let's mess with that a little bit.
0:56
Basically, I'm going to convert that to a string. So I've got a little chain here that will do that. And again, let me just walk through this chain
1:03
so you can see what's going on. So here's what we had. We had our cutting. I'll do our value counts. That's what we had before. Let's reset the index.
1:11
This is going to push the index into a column. Right now, we have the snow index, which is the bins. We're going to put that into its own column.
1:19
There we go. It's capital snow. That's how much snow fell in bins. I'm going to just rename that to a column called bin, print c-i-n for inches.
1:30
And I'm going to just capitalize count. OK, that looks like that worked. And then let's convert bin in to a string.
1:41
And at this point, we should be able to plot that. And that looks like that worked. So what we see is there is a lot of snowfall in that 0 to 3.6
1:53
inches. And then the other ones look like they're outliers. Another thing I can do with pipe, which is kind of cool, is I can specify the bin edges.
2:01
So here I'm going to say minus 1 to 0, 0 to 1, 1 to 5, et cetera. Let's look at that. And we'll look at the output here. The rest of this is the same.
2:10
That's kind of cool. We can easily specify those bin edges. So here we see that a lot of snow was in that 0 range.
2:17
So let's use our Panda skills to filter that and say, we only want snow that was greater than 0. So we're getting rid of the tall column there.
2:26
And this lets us zoom in to the other values.