【发布时间】:2021-12-18 03:10:51
【问题描述】:
我想在一个小图表中显示每年每个月的所有平均温度。我的第一个问题是打印较冷(蓝色)和较暖(分别为红色)温度的图例,并为图表本身着色。
我的第二个问题与循环数据有关,最终出现以下错误:
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'。年数不一定是偶数。我怎样才能在循环中很好地表示它?
我怎样才能把图表像下面的图片一样,包括彩色图例和图表。
我想要什么:
- 显示所有年份和每个月的所有平均温度
- 彩色图例和图表
- (不管是 matloblib 还是 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'
【问题讨论】:
标签: python pandas matplotlib charts seaborn