如何使用熊猫制作条形图

时间:2020-10-24 03:11:31

标签: python pandas dataframe matplotlib

我是python的新手,我正在尝试使用bar plot using python结果来制作类似于链接的SNAP2。 SNAP2的结果是一个大型的Excel文件,其中包含四列。我想制作一个条形图,在x轴上绘制“变量”,在y轴上绘制“预期精度”。 初始excel文件在“期望精度”列的每个单元格中都有一个百分号,这似乎将其转换为小数。现在我很难将其绘制为条形图。

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_excel("snap2_data.xlsx", dtype={'Score': int}, sheet_name='sheet1', usecols=['Variant', 'Expected Accuracy'])

df[["Variant", "Expected Accuracy"]].plot(x="Variant", y='Expected Accuracy', kind="bar")

plt.title('Title')
plt.xlabel('Variant')
plt.ylabel('Expected Accuracy')

plt.show()

enter image description here

print(df) results

1 个答案:

答案 0 :(得分:0)

如果删除百分号并将其转换为数字,则会得到以下图形。

df['Expected Accuracy'] = df['Expected Accuracy'].apply(lambda x: float(x.rstrip('%')))

enter image description here