# Week 8

## Hatching a Plot <a href="#hatching-a-plot" id="hatching-a-plot"></a>

{% file src="/files/-LoobaVQKxfAOikH33ir" %}
Alcohol Price Data
{% endfile %}

{% file src="/files/-LoobaVSTKHSzGS8Xdcl" %}
Deaths Data
{% endfile %}

{% file src="/files/-LoobaVUHEd-1Zx92yzm" %}
Drug Prescription Data
{% endfile %}

{% file src="/files/-LoobaVWX9QSCj0eUW\_a" %}
Household Expenditure
{% endfile %}

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

Use **np.genfromtext()** to load the data we've provided for you. For this exercise you'll want to use the "Deaths" and "Household\_expenditure" data.

In order to get the desired behaviour, you'll need to specify some arguments in **np.genfromtxt()**, as we did in the example (What's the delimiter?, Is there a column names row?, Are there multiple types of data in each row?).

* Once you've imported these two data sets succesfully, double check that you can access and print items from the numpy arrays in the same was we did with lists (i.e. using a for loop and \[] index notation)

### Part 2: Plotting with Matplotlib <a href="#part-2-plotting-with-matplotlib" id="part-2-plotting-with-matplotlib"></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. Then

* Using for loops, create lists containing the years, total spending and percentage spending from the Household expenditure array
* Plot the total spending and percentage spending against years, in two seperate plots
* Make your plots look nice by setting labels and titles using plt.title("my title"), plt.xlabel("x axis label")

**Extension:**

* Plot both the percentange and total spending on the same plot, first normalizing (i.e. rescaling) the data, so only the trend information is seen.
* Using the label="my label" argument in the plotting step, create a legend (you'll need to call plt.legend() before plt.show())

### Part 3: Bar Charts <a href="#part-3-bar-charts" id="part-3-bar-charts"></a>

Using the **plt.barh(bin\_labels, bin\_frequencies)** function, create a barchart visualizing the relative frequencies of alchol-related deaths in 2015 (i.e. the data imported from the Deaths.csv)

* You'll need to use the first column of the data as the axis labels

## A fitting challenge <a href="#a-fitting-challenge" id="a-fitting-challenge"></a>

Choosing an interesting dataset (e.g. Alcohol-related expenditure), try to fit a polynomial function to the data using **np.polyfit** (see: [https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html)](https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html%29), and based on the best fitting polynomial, extrapolate the trend for the next five years - plot this prediction and see whether it makes sense!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://python.afspies.com/exercises/week-7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
