Python Classes
  • Course Outline
  • Course Organization
  • Material
    • Week 1
    • Week 2
      • Extensions
    • Week 3
      • Extensions
    • Week 4
    • Week 5
      • Extensions
    • Week 6
      • Extensions
    • Week 7
    • Numpy
    • Matplot
    • Pandas
    • Week 10
    • Week 11
  • Exercises
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5
    • Week 6
    • Week 7
    • Matplotlib
    • Numpy
    • Pandas
    • Matplot with Numpy
  • Solutions
    • Week 2
    • Week 3
    • Week 4
    • Week 5
    • Week 6
    • Week 7
  • Additional
    • More Resources
    • Setting up Python
  • Lectures
    • Slides
    • Recordings
Powered by GitBook
On this page

Was this helpful?

  1. Exercises

Matplotlib

Plotting graphs from data

Part 1: Creating a line graph

You'll want to start by importing matplotlib.pyplot as plt because no one wants to write so much stuff everytime they use the module.

Use plt.plot() to plot the data we've provided for you below.

You would need to store the data to be able to access it. After plotting the graph, add a title to it and labels to each axis to make the graph and table more readable.

Data for this exercise:

Months:

["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

Temperatures:

[4.1, 2.4, 3.8, 8.4, 12.1, 14.8, 17.3, 15.3, 12.4, 9.6, 7.3, 5.8]

Extension:

  • Once you've finished the above assignment try to make the graph look better by adding individual marker for each data point on the graph change their colour (You might have to do some research for this one!)

Part 2: Plotting with Matplotlib

Use plt.scatter() to plot the data we've provided for you about the sales of ice cream against the temperature outside. Add labels to each axis and a title to the graph.

Data for this exercise:

Sales(in thousands of pounds):

[73, 57, 81, 94, 110, 124, 134, 139, 124, 103, 81, 80]

Temperatures:

[4.1, 2.4, 3.8, 8.4, 12.1, 14.8, 17.3, 15.3, 12.4, 9.6, 7.3, 5.8]

Part 3: Bar Charts

Using the plt.bar() function, create a barchart visualizing the choice of pets of UK household in 2018. This exercise should be slightly tougher to complete as plt.bar() has a more dissimilar syntax to the previous two functions used.

Data for this exercise:

Pet types:

["Any", "Dog", "Cat", "Rabbit", "Bird", "Hamster", "Turtle", "Lizard", "Snake"]

Percentage owned per type:

[40, 25, 17, 1, 1, 1, 0.7, 0.6, 0.4]

Extension to all tasks

Modify all the previous tasks to get their data from their respective text file down below. Read in the data from the file and store it before performing the same plotting functions. (The graphs should remain the same after changing the approach to reading the data!)

PreviousWeek 7NextNumpy

Last updated 5 years ago

Was this helpful?

161B
Task1.txt
Task1_data
117B
Task2.txt
Task2_data
95B
Task3.txt
Task3_data