Locked lesson.
About this lesson
We discuss how to create another type of data visualization: area plots.
Exercise files
Download this lesson’s related exercise files.
Plotting Area Plots.docx58.6 KB Plotting Area Plots - Solution.docx
55.4 KB
Quick reference
Plotting Area Plots
Area plots allow us to graph the area below our data.
When to use
Use this technique whenever your data calls for an area plot.
Instructions
To create an area plot on a dataframe:
my_df.plot(kind='area')
To add transparency to the plot:
my_df.plot(kind='area', alpha=0.4)
Hints & tips
- The data for an area plot needs to be all negative, or all positive, it can't be both.
- Area Plots: my_df.plot(kind='area')
- Add Transparency: my_df.plot(kind='area', alpha=0.4)
- 00:05 Okay, in this video I want to look at area plots.
- 00:08 And an area plot just graphs the area underneath your data, right?
- 00:13 So we could call my_df and we could pick the column or
- 00:17 we could just pick the whole thing and let's go .plot and
- 00:22 we want the kind='area'.
- 00:26 Now, this actually won't work.
- 00:27 If we run this, we get an error.
- 00:29 And if we look down here, it says each column either has to be all positive or
- 00:34 negative, it can't contain both positive and negative.
- 00:38 And if we look back up at our data here, we see there is negative numbers and
- 00:42 positive numbers.
- 00:42 So to tweak our data a little bit here, we can just run our function on this as
- 00:46 we've learned way back at the beginning of the course.
- 00:48 We could call the absolute value function and Python is object oriented, so
- 00:53 you could stack .abs on top of .plot,
- 00:57 on top of dot whatever, you can just keep adding dot things with Python.
- 01:01 And so that's cool.
- 01:02 Now, we get this sort of crazy jumbled up area plot and if we come back to our data,
- 01:08 we've got 100 points, maybe we want to make this a little more simpler.
- 01:11 We can run this with 50 points if we rerun our thing here and
- 01:15 then come down here and run this again.
- 01:18 Okay, maybe it's a little less hectic and easier to see.
- 01:21 Now,you'll notice each of these things are not very transparent,
- 01:26 so you can't really see through to see exactly what's going on, for instance,
- 01:30 with the red, it's in the background there, right?
- 01:32 So we can actually come up here to the kind and now, we can add alpha= and
- 01:37 then designate some transparency.
- 01:40 So I'm going to go 0.3, let's say, and a sort of very light.
- 01:45 We could go 0.6 and it's a little darker.
- 01:49 So whatever you want, transparency wise, that's cool.
- 01:53 And that's area plot.
- 01:55 Now, if we want to learn about the different attributes that we can
- 01:58 use in this function, you normally hit Shift Tab.
- 02:00 And when I do that now, nothing happens.
- 02:02 That's because we're stacking this on top of the dot absolute thing.
- 02:06 So we take this out and then come back and Shift Tab.
- 02:09 Now, we can get in here and we can see some of the things.
- 02:13 So we can add a legend true false, that's this little box right here.
- 02:18 We can put a title on here, all kinds of stuff.
- 02:22 And you can always come in here and
- 02:24 read about any of these things to learn more about them.
- 02:26 So, let's try this, let's go legend=false and
- 02:31 we're going to have to come back here and
- 02:34 put our .abs back and now that box disappears.
- 02:38 So if we want this to be true, the box comes back.
- 02:43 It's true by default, so we could just leave that off.
- 02:46 We can add a title and
- 02:49 we can put, I don't know, My Area Plot.
- 02:55 And now, we got a little title up there.
- 02:57 So all kinds of cool things you can do with this.
- 02:59 So that's an area plot.
- 03:01 In the next video, we'll look at bar plots.
Lesson notes are only available for subscribers.