Path: blob/master/section-2-data-science-and-ml-tools/matplotlib-exercises.ipynb
874 views
Matplotlib Practice
This notebook offers a set of exercises to different tasks with Matplotlib.
It should be noted there may be more than one different way to answer a question or complete an exercise.
Different tasks will be detailed by comments or text.
For further reference and resources, it's advised to check out the Matplotlib documentation.
If you're stuck, don't forget, you can always search for a function, for example if you want to create a plot with plt.subplots()
, search for plt.subplots()
.
There's another way to create plots with Matplotlib, it's known as the object-orientated (OO) method. Let's try it.
Now let's try a small matplotlib workflow.
Okay, this is a simple line plot, how about something a little different?
To help us, we'll import NumPy.
We'll start with scatter plots.
How about we try another type of plot? This time let's look at a bar plot. First we'll make some data.
All this food plotting is making me hungry. But we've got a couple of plots to go.
Let's see a histogram.
Notice how the distributions (spread of data) are different. Why do they differ?
What else can you find out about the normal distribution?
Can you think of any other kinds of data which may be normally distributed?
These questions aren't directly related to plotting or Matplotlib but they're helpful to think of.
Now let's try make some subplots. A subplot is another name for a figure with multiple plots on it.
Notice how the subplot has multiple figures. Now let's add data to each axes.
Woah. There's a lot going on there.
Now we've seen how to plot with Matplotlib and data directly. Let's practice using Matplotlib to plot with pandas.
First we'll need to import pandas and create a DataFrame work with.
Why doesn't it work?
Hint: It's not numeric data.
In the process of turning it to numeric data, let's create another column which adds the total amount of sales and another one which shows what date the car was sold.
Hint: To add a column up cumulatively, look up the cumsum() function. And to create a column of dates, look up the date_range() function.
Now we've got a numeric column (Total Sales
) and a dates column (Sale Date
), let's visualize them.
Now we've seen a few examples of plotting directly from DataFrames using the car_sales
dataset.
Let's try using a different dataset.
That plot looks pretty squished. Let's change the figsize.
Now let's try comparing two variables versus the target variable.
More specifially we'll see how age and cholesterol combined effect the target in patients over 50 years old.
For this next challenge, we're going to be replicating the following plot:
Beatiful, now you've created a plot of two different variables, let's change the style.
Now the style has been changed, we'll replot the same figure from above and see what it looks like.
If you've changed the style correctly, it should look like the following:
Wonderful, you've changed the style of the plots and the figure is looking different but the dots aren't a very good colour.
Let's change the cmap
parameter of scatter()
as well as the color
parameter of axhline()
to fix it.
Completing this step correctly should result in a figure which looks like this:
Beautiful! Now our figure has an upgraded color scheme let's save it to file.
Extensions
For more exercises, check out the Matplotlib tutorials page. A good practice would be to read through it and for the parts you find interesting, add them into the end of this notebook.
The next place you could go is the Stack Overflow page for the top questions and answers for Matplotlib. Often, you'll find some of the most common and useful Matplotlib functions here. Don't forget to play around with the Stack Overflow filters! You'll likely find something helpful here.
Finally, as always, remember, the best way to learn something new is to try it. And try it relentlessly. Always be asking yourself, "is there a better way this data could be visualized so it's easier to understand?"