Assignment B

Notebook

In JupyterLab, choose File > New > Notebook.

Select File > Save Notebook As… and save your notebook in a location that you can easily find. Suggested file name: Python Assignment B.ipynb

Type a title, your name and today’s date in the first cell, like this:

## Assignment B
Your Name\
YYYY-MM-DD

Select cell type Markdown for this cell from the drop down menu at the top of your notebook.

Click Run (“play”).

Import

Click Insert (+).

Type the following code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Click Run.

Data Set

Download the following data set and upload it to the same folder as your Python Assignment B.ipynb file:

Electricity Production in Sweden

In the data file you find the yearly production (GWh) of electricity in Sweden for the years 2001 – 2022 by type of production: hydro, wind, solar, nuclear, and other sources of power.


IMPORTANT! The Python Assignment B.ipynb file and the Electricity Production in Sweden.xlsx file must be located in the same folder before you proceed! See tutorial.


Import Data

Click Insert (+).

Type the following code:

data = pd.read_excel("Electricity Production in Sweden.xlsx")

Click Run.

To view the first five rows of the data, insert a new cell and run the following code:

data.head() 

a)

Find the total yearly production of electricity. Plot the time series.

To find the total yearly production of electricity (and add the total to the data) you can use the following code:

data['Total'] = data[ ['Hydro', 
                       'Wind', 
                       'Solar', 
                       'Nuclear', 
                       'Other']].sum(axis=1)

View the first five rows of the data again:

data.head() 

Plot the time series using the following code:

plt.figure(figsize=(8, 4))
sns.lineplot(data=data, x='Year', y='Total')

plt.xlabel('Year')
plt.ylabel('Total Electricity Production')
plt.title('Yearly Total Electricity Production')
plt.show()

b)

Find the fraction of electricity produced by wind and solar power. The fraction must be given in percentage points. Plot the time series.

You can use the same code as above, but with a couple of changes.

First, instead of plotting the Total variable as y, you want to plot the fraction (Solar + Wind) / Total instead. Try adjusting the code.

Second, adjust the title of the y-axis.

c)

Calculate the share of each type of production for the year 2001. Illustrate the five shares in a pie chart.

To calculate the share of each type of production for the year 2001 you can use the following code:

data_2001 = data[data['Year'] == 2001]

shares_2001 = data_2001[['Hydro', 
                         'Wind', 
                         'Solar', 
                         'Nuclear',
                         'Other']].div(data_2001['Total'], 
                                       axis=0).sum()

plt.figure(figsize=(4, 4))
plt.pie(shares_2001, 
        labels=shares_2001.index, 
        autopct='%1.1f%%', startangle=140, 
        colors=sns.color_palette('Set3'))
plt.title('Share of Electricity Production by Type 2001')
plt.show()

d)

Do the same calculations for the year 2022 and illustrate in a pie chart.

You can use the same code as above, but changing all instances of 2001 to 2022 instead.

e)

Compare the pie charts.

Click Insert (+). Select Markdown as cell type.

Type your answer in the cell and click Run.

You do not need to hand in Assignment B.