关于数据可视化的简单 python 问题
Simple python question about data visualization
我想要一个显示阿尔巴尼亚 2000 年所有疾病数量的条形图。
我试过了,但我无法得到我想要的。
fig, ax = plt.subplots()
ax.bar(df[country['Albania']], df['2000'])
plt.xlabel('Fruit', fontsize=17, fontname='Times New Roman')
plt.ylabel('Spent', fontsize=17, fontname='Times New Roman')
plt.title('Share of diseases in Albania in 2000 ', fontsize=17, fontname="Times New Roman")
plt.show()
让我们首先设置一个虚拟示例:
import numpy as np
import pandas as pd
import itertools
np.random.seed(0)
df = pd.DataFrame({('Country_%s' % c, y): {'disease_%d' % (i+1): np.random.randint(100)
for i in range(4)}
for c,y in itertools.product(list('ABCD'), range(1998,2002))
}).T
df.index.names = ('country', 'year')
disease_1 disease_2 disease_3 disease_4
country year
Country_A 1998 44 47 64 67
1999 67 9 83 21
2000 36 87 70 88
2001 88 12 58 65
Country_B 1998 39 87 46 88
1999 81 37 25 77
2000 72 9 20 80
2001 69 79 47 64
Country_C 1998 82 99 88 49
1999 29 19 19 14
2000 39 32 65 9
2001 57 32 31 74
Country_D 1998 23 35 75 55
1999 28 34 0 0
2000 36 53 5 38
2001 17 79 4 42
然后您可以按国家和年份对一个多索引行进行子集化
df.loc[('Country_B', 2000)]
输出:
disease_1 72
disease_2 9
disease_3 20
disease_4 80
Name: (Country_B, 2000), dtype: int64
并绘图(这里使用pandas+matplotlib):
ax = df.loc[('Country_B', 2000)].plot.bar()
ax.set_ylabel('number of cases')
我想要一个显示阿尔巴尼亚 2000 年所有疾病数量的条形图。
我试过了,但我无法得到我想要的。
fig, ax = plt.subplots()
ax.bar(df[country['Albania']], df['2000'])
plt.xlabel('Fruit', fontsize=17, fontname='Times New Roman')
plt.ylabel('Spent', fontsize=17, fontname='Times New Roman')
plt.title('Share of diseases in Albania in 2000 ', fontsize=17, fontname="Times New Roman")
plt.show()
让我们首先设置一个虚拟示例:
import numpy as np
import pandas as pd
import itertools
np.random.seed(0)
df = pd.DataFrame({('Country_%s' % c, y): {'disease_%d' % (i+1): np.random.randint(100)
for i in range(4)}
for c,y in itertools.product(list('ABCD'), range(1998,2002))
}).T
df.index.names = ('country', 'year')
disease_1 disease_2 disease_3 disease_4
country year
Country_A 1998 44 47 64 67
1999 67 9 83 21
2000 36 87 70 88
2001 88 12 58 65
Country_B 1998 39 87 46 88
1999 81 37 25 77
2000 72 9 20 80
2001 69 79 47 64
Country_C 1998 82 99 88 49
1999 29 19 19 14
2000 39 32 65 9
2001 57 32 31 74
Country_D 1998 23 35 75 55
1999 28 34 0 0
2000 36 53 5 38
2001 17 79 4 42
然后您可以按国家和年份对一个多索引行进行子集化
df.loc[('Country_B', 2000)]
输出:
disease_1 72
disease_2 9
disease_3 20
disease_4 80
Name: (Country_B, 2000), dtype: int64
并绘图(这里使用pandas+matplotlib):
ax = df.loc[('Country_B', 2000)].plot.bar()
ax.set_ylabel('number of cases')