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:
Select cell type Markdown for this cell from the drop down menu at the top of your notebook.
Click Run (“play”).
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:
Click Run.
To view the first five rows of the data, insert a new cell and run the following code:
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:
View the first five rows of the data again:
Plot the time series using the following code:
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()