无法通过 pandas 条形图中的值来标记 x

问题描述 投票:0回答:1

以下代码无法显示参考值“F”、“C”、“H”、“B”、“G”、“A”、“D”、“E”。仅显示索引值:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({'Ref': ['F', 'C', 'H', 'B', 'G', 'A', 'D', 'E'],
                     'Nombre Pieces': [150, 130, 100, 60, 30, 30, 20, 10],
                     'PC100': [28.301887, 52.830189, 71.698113, 83.018868, 88.679245, 94.339623, 98.113208, 100.000000],
                     'categories': ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']})

colors = {'a': 'red', 'b': 'blue'}

data['PC100'].plot(kind='bar', color=data['categories'].map(colors))

plt.show()

如何编辑此代码?谢谢

pandas plot
1个回答
0
投票

仅绘制:

data.set_index('Ref')['PC100'].plot(kind='bar', color=data['categories'].map(colors))

对于永久索引更改和绘图:

data.set_index('Ref', inplace=True)
data['PC100'].plot(kind='bar', color=data['categories'].map(colors))
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.