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

2024-04-29 11:09:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框架(经合组织不同国家的季度国民账户数据),是一个包含国家和时间指数的面板。我想创建一个条形图,上面显示一个给定日期各国国内生产总值的增长率,并将国家名称作为XTICK。。。你知道吗

到目前为止,我已经能够创建一个带有排序值的图表,但是我不能将排序后的国家名创建为xtick


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


Tags: 数据框架排序时间plt账户国家print
1条回答
网友
1楼 · 发布于 2024-04-29 11:09:10

使用一些虚拟数据:

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

结果:enter image description here

相关问题 更多 >