简体   繁体   English

关于数据可视化的简单python问题

[英]Simple python question about data visualization

I want a bar plot that shows the number of all diseases in 2000 for Albania.我想要一个条形图,显示 2000 年阿尔巴尼亚所有疾病的数量。

数据图像

I tried this, but I could not get what I want.我试过这个,但我无法得到我想要的。

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()

Let's first set up a dummy example:让我们首先设置一个虚拟示例:

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

You can then subset one multi-indexed row per country and year然后,您可以对每个国家和年份的一个多索引行进行子集化

df.loc[('Country_B', 2000)]

output:输出:

disease_1    72
disease_2     9
disease_3    20
disease_4    80
Name: (Country_B, 2000), dtype: int64

and plot (here using pandas+matplotlib):和情节(这里使用熊猫+ matplotlib):

ax = df.loc[('Country_B', 2000)].plot.bar()
ax.set_ylabel('number of cases')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM