--- tags: python-course title: lesson-10 --- # Plotting Data :::info :bulb: In this lesson students learn about how to use the matplotlib third-party package to plot data. :school: Teacher is Nina, student is James. ::: :::success :movie_camera: VIB background fading to course title slide. James and Ninas smiling faces appear. ::: :::warning :notes: Upbeat intro music ::: > scene 1 **Nina**: Congratulations! You've graduated from the basics of programing young padawan. And now you must complete one final challenge! Create a scatter plot for me you must. **James**: You're asking me the impossible! **Nina**: It is indeed a more advanced topic. But you've completed all of the basics. Now you're definitely ready for something more advanced. Now you can... plot data! **James**: Ok well, can you at least tell me what I need? The module name, maybe? **Nina**: Oh ok yeah sure I suppose. You can use your first third-party module, called "matplotlib". Start by importing it now... **James** (over animation): Ok... :::success :movie_camera: Start of animation showing a scatter plot of iris data. ```python import matplotlib.pyplot as plt ``` ::: > scene 2 **James**: That's.... what is this `pyplot`? **Nina**: `pyplot` is the plotting module provided by matplotlib. But matplotlib does so much more than that. Since you want to plot you can just work with `pyplot`. To save you having to type out `matplotlib.pyplot` every time you want to use it, you can alias it using the `as` keyword. So now you can simply type `plt.something` instead. **James**: Ok. That makes sense. How do I use this to make a scatter plot? **Nina**: There's a function called, `scatter` which will draw a scatter plot for you. **James**: just that? hummm... (thinking like something is missing) **Nina**: But you're missing something essential... **James**: Oh yeah! The data I want to plot!! Ok ok (excitedly), let me plot data from the famous Iris data set. I remember from the lesson on I/O that I can use `urllib` to read data from the internet... :::success :movie_camera: Animate adding this download. ```python= import csv from urllib.request import urlopen import matplotlib.pyplot as plt with urlopen("https://storage.googleapis.com/vib-training-data/python-course/iris.csv") as iris: irisdata = iris.read().decode("utf8").splitlines() ``` ::: > scene 3 **James**: Now I have my data and a CSV `reader`. Ok I want to plot the first column on the x-axis, and the second column on the y-axis... :::success :movie_camera: Animation of a dictionary definition ```python= import csv from urllib.request import urlopen import matplotlib.pyplot as plt x = [] y = [] with urlopen("https://storage.googleapis.com/vib-training-data/python-course/iris.csv") as iris: irisdata = iris.read().decode("utf8").splitlines() reader = csv.reader(irisdata) for row in reader: x = x + [row[0]] y = y + [row[1]] plt.scatter(x=x, y=y) ``` ::: > scene 4 **James**: That doesn't look right.... What went wrong? **Nina**: You now have all the tools you need to find out for yourself padawan. **James**: (sarcasm) Ok great thanks! ... Well I can't see how to easily test this. I suppose I could print out what the data is here.... oh! I'm putting strings into `x` and `y` rather than numbers. I wonder if that's what is wrong? (Pause) **James**: Yes! That looks better! > Add the XKCD comic about labelling axes Scene 5 **James**: Now I have the data I want to plot and it "works". But it's still ugly. I don't have a title and the axes aren't labeled. **Nina**: You can use the `xlabel()`, `ylabel()`, and `title()` methods for that... `(animation james typing)` **Nina**: Ok well done. That looks very good. As a final trial, for extra credit, can you produce a labeled histogram of the data in the first column? **James**: Ok well I already have the raw data in my `x` variable. Maybe there's a histogram function I can use... oh... no there isn't. But wow! There are a lot of plots here!! > Show the examples website. **Nina**: That's a good guess, but the function is called `hist()`. **James**: Ah oh I can fix it then... **Nina**: Very well done. Scene 6 **James**: Can I save these plots to an image file? **Nina**: Yes absolutely. I'll help you out on this one though. There's a function called, `savefig`. (Long pause) **Nina**: Congratulations! You've synthesised everything you've learned in the course to produce these plots. You should be proud of yourself! Can you tell me what you've learned in this lesson? **James**: Thanks Nina! Ok let me see. I used the matplotlib module to produce plots and save them. But I needed data to do that so I downloaded the famous Iris dataset that using urllib. **Nina** (to the camera): Well done once again all of you! :::warning :notes: Upbeat outro music ::: :::success :movie_camera: Fade to VIB logo slide. :::