gpt4 book ai didi

Python Pandas 错误栏未绘制以及如何自定义索引

转载 作者:行者123 更新时间:2023-12-01 03:52:14 25 4
gpt4 key购买 nike

我需要您帮助在 Python 中使用 Pandas 绘制误差线。我阅读了 Pandas 文档,并做了一些尝试和错误,但没有得到令人满意的结果。

这是我的代码:

'''
usage : (python) rc-joint-plot-error-bar.py
'''

from __future__ import print_function
import pandas as pd
import matplotlib.pyplot as plt

filename = 'rc-plot-error-bar.csv'

df = pd.read_csv(filename, low_memory = False)

headers = ['Specimen', 'CA_Native_Mean', 'CA_Implant_Mean', 'CP_Native_Mean',
'CP_Implant_Mean', 'CA_Native_Error', 'CA_Implant_Error', 'CP_Native_Error',
'CP_Implant_Error']

for header in headers :
df[header] = pd.to_numeric(df[header], errors = 'coerce')

CA_means = df[['CA_Native_Mean','CA_Implant_Mean']]
CA_errors = df[['CA_Native_Error','CA_Implant_Error']]

CP_means = df[['CP_Native_Mean', 'CP_Implant_Mean']]
CP_errors = df[['CP_Native_Error', 'CP_Implant_Error']]

CA_means.plot.bar(yerr=CA_errors)
CP_means.plot.bar(yerr=CP_errors)

plt.show()

这是我的数据框的样子:

   Specimen  CA_Native_Mean  CA_Implant_Mean  CP_Native_Mean  CP_Implant_Mean  \
0 1 1 0.738366 1 1.087530
1 2 1 0.750548 1 1.208398
2 3 1 0.700343 1 1.394535
3 4 1 0.912814 1 1.324024
4 5 1 1.782425 1 1.296495
5 6 1 0.415147 1 0.479259
6 7 1 0.934014 1 1.084714
7 8 1 0.526591 1 0.873022
8 9 1 1.409730 1 2.051518
9 10 1 1.745822 1 2.134407

CA_Native_Error CA_Implant_Error CP_Native_Error CP_Implant_Error
0 0 0.096543 0 0.283576
1 0 0.076927 0 0.281199
2 0 0.362881 0 0.481450
3 0 0.400091 0 0.512375
4 0 2.732206 0 1.240796
5 0 0.169731 0 0.130892
6 0 0.355951 0 0.272396
7 0 0.258266 0 0.396502
8 0 0.360461 0 0.451923
9 0 0.667345 0 0.404856

如果我运行代码,我得到以下数据: plotting CA_means, but CA_errors are not shown plotting CP_means, but CP_errors are not shown

我的问题是:

  1. 您能否告诉我如何使误差线出现在图中?
  2. 如何将索引(x 轴的值)从 0-9 更改为 1-10?

非常感谢!

问候,阿诺德·A.

最佳答案

你就快到了!

  1. 为了使误差线显示在图中,yerr 中的列名称应与条形图中数据的名称相匹配。尝试重命名 CA_errors

  2. 要更改 x 标签,请尝试 ax.set_xticklabels(df.Specimen);


_, ax= plt.subplots()
CA_means = df[['CA_Native_Mean','CA_Implant_Mean']]
CA_errors = df[['CA_Native_Error','CA_Implant_Error']].\
rename(columns={'CA_Native_Error':'CA_Native_Mean',
'CA_Implant_Error':'CA_Implant_Mean'})
CA_means.plot.bar(yerr=CA_errors, ax=ax)
ax.set_xticklabels(df.Specimen);

enter image description here

关于Python Pandas 错误栏未绘制以及如何自定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com