> For the complete documentation index, see [llms.txt](https://hacksoc.gitbook.io/python-classes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacksoc.gitbook.io/python-classes/exercises/matplotlib.md).

# Matplotlib

Plotting graphs from data

#### Part 1: Creating a line graph <a href="#part-1-loading-data-with-numpy" id="part-1-loading-data-with-numpy"></a>

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:

&#x20;            \["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

Temperatures:

&#x20;            \[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 <a href="#part-2-plotting-with-matplotlib" id="part-2-plotting-with-matplotlib"></a>

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):

&#x20;             \[73, 57, 81, 94, 110, 124, 134, 139, 124, 103, 81, 80]

Temperatures:

&#x20;             \[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 <a href="#part-3-bar-charts" id="part-3-bar-charts"></a>

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:

&#x20;             \["Any", "Dog", "Cat", "Rabbit", "Bird", "Hamster", "Turtle", "Lizard", "Snake"]

Percentage owned per type:

&#x20;             \[40, 25, 17, 1, 1, 1, 0.7, 0.6, 0.4]

### Extension to all tasks <a href="#a-fitting-challenge" id="a-fitting-challenge"></a>

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!)

{% file src="/files/-Lu9--\_KisP4i29BTQVZ" %}
Task1\_data
{% endfile %}

{% file src="/files/-Lu901sOIwoahPqVjPEU" %}
Task2\_data
{% endfile %}

{% file src="/files/-Lu905gK4BD8c2jOiVV-" %}
Task3\_data
{% endfile %}
