I want to show all average temperatures of each month in a small chart for every year. My first issue is printing the legend for the colder (blue) and warmer (respectively red) temperatures and giving colour to the chart itself.
My second issue is connected with looping through the data, ending in getting the following error:
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
. The amount of the years doesn't always have to be an even number. How can I nicely represent that in the loop?
How can I nake the chart like the picture underneath, including the coloured legend and chart.
What I want:
- displaying all the years with all average temperatures of each month
- coloured legend and chart
- (It doesn't matter whether matloblib or seaborn)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
d = {'year': [2001, 2001, 2001, 2001,
2002, 2002, 2002, 2002],
'month': [1, 2,3,4,
1,2,3,4],
'temperature': [10,20,15,20,
20,10,5,10]}
df = pd.DataFrame(data=d)
df.head()
fig, axs = plt.subplots(int(len(df.year.unique()) / 2), int(len(df.year.unique()) / 2))
for i in enumerate(df.year.unique()):
for j in range(int(i/2)):
for k in range(int(i/2)):
month = df['month'].unique()
temperature = df[df['year'] == i].groupby('month')['temperature'].mean().values
axs[j][k].bar(month, temperature)
plt.show()
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'