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 (TWh) of electricity in Sweden for the years 1990 – 2023 by type of production: hydro, wind, solar, nuclear, and other sources of power. Also, you find total production of electricity in Sweden and trade of electricity, that is import of electricity minus export of electricity.
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)
Create a new variable of the total yearly electricity used, where Used = Total – Trade. Plot the time series.
To create the total yearly electricity used (and add the Used variable 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. Find the fraction of electricity produced by nuclear power. The fractions must be given in percentage points. Plot the two time series’ in one plot.
You can use the same code as above, but with a few changes.
First, create a Fraction variable defined as (Solar + Wind) / Total. Now, refer to Fraction as the y variable in your plot instead of Used.
Second, create a NuclearFraction variable defined as Nuclear / Total. Duplicate the plot code line (starting with sns.lineplot…) and refer to NuclearFraction as the y variable.
Third, adjust the title of the y-axis.
c)
Calculate the share of each type of production for the year 2000. Illustrate the five shares in a pie chart.
To calculate the share of each type of production for the year 2000 you can use the following code:
data_2000 = data[data['Year'] == 2000]
shares_2000 = data_2000[['Hydro',
'Wind',
'Solar',
'Nuclear',
'Other']].div(data_2000['Total'],
axis=0).sum()
plt.figure(figsize=(4, 4))
plt.pie(shares_2000,
labels=shares_2000.index,
autopct='%1.1f%%', startangle=140,
colors=sns.color_palette('Set3'))
plt.title('Share of Electricity Production by Type 2000')
plt.show()
d)
Do the same calculations for the year 2023 and illustrate in a pie chart.
You can use the same code as above, but changing all instances of 2000 to 2023 instead.