简体   繁体   English

如何在Python的面板数据框中创建排序值的条形图

[英]How to create a bar chart of sorted values in a panel data frame in Python

I have a data frame (quarterly national account data for different countries in the OECD) that is a panel with indexes country and time. 我有一个数据框(经合组织中不同国家的季度国民账户数据),它是一个包含国家和时间索引的面板。 I want to create a bar chart with the rates of growth of GDP across countries for a given date with country names as xticks... 我想创建一个条形图,以给定日期的国家/地区国内生产总值增长率来表示国家名称为xticks ...

So far I have been able to create a chart with sorted values, but I am not able to create sorted country names as xticks 到目前为止,我已经能够创建带有排序值的图表,但是我无法将排序后的国家/地区名称创建为xticks


quant = quant.sort_values('rGDP_Chg', ascending = False)

#print(quant.rGDP_Chg[:, '2019-Q1'])

ind = np.argsort(quant.rGDP_Chg[:, '2019-Q1'])

print(ind)

plt.bar(ind, quant.rGDP_Chg[:, '2019-Q1'])

plt.show()

Using some dummy data: 使用一些伪数据:

import pandas as pd
import matplotlib.pyplot as plt

data = [
    ['B', 5, '2019-Q1'],
    ['A', 2, '2019-Q1'],
    ['A', 3, '2019-Q2'],
]
columns = ['Country', 'Growth', 'Date']
df = pd.DataFrame(data=data, columns=columns)

df[df['Date'] == '2019-Q1'].sort_values(['Country']).plot(x='Country', y='Growth', kind='bar')      
plt.show()

Result: 结果: 在此处输入图片说明

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

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