numpy 用matlibplot用误差条绘制许多样本随时间的变化-无论我尝试什么都会收到错误

mpgws1up  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(203)

我是matlibplot的新手,我一直在断断续续地尝试弄清楚如何用matlibplot而不是prism绘图,以使我的工作更快,但它实际上是使它慢得多,因为我尝试的一切都给出了错误。我有这个 Dataframe “最终”与163个时间点和18个样本(一式三份)。我想用标准差误差条和每个样本不同颜色的线来绘制平均值随时间的变化。我没想到这会如此困难。
我认为最接近的尝试如下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#code to analyze data and create final dataframe (called "final"):
  #some code here

#find the mean and std of each sample which are in triplicate from the final dataframe:

mean=final.groupby(np.arange(len(final.columns))//3, axis=1).mean()
std=final.groupby(np.arange(len(final.columns))//3, axis=1).std()

#insert is a dataframe with 163 timepoints, the x value (i.e. time=0, time=0.34 etc)
plt.errorbar(insert,mean,yerr=std, label="work please")
plt.show

我得到错误“ValueError:解包的值太多(预期值为1)”,尽管我检查了所有 Dataframe 的长度为163个时间点,平均值和标准值有18个样本,因此它们应该是相同的维度。我尝试将 Dataframe 转换为numpy数组,因为有人说这是必需的,但没有帮助。
任何帮助将不胜感激,因为我已经在互联网上搜索了太长时间。还有,我如何才能保持名称的每个样品是在数据框时,绘图?
更新:我使用Pandas图而不是matlibplot进行了排序。我可以看到所有的样本和数据条,但不能修改x或y轴。我做到了:

mean.plot(yerr=std)

然而,当我尝试添加x值时(如下所示),误差线消失了:

mean.insert(0,"Elapsed", insert, True)
mean.set_index("Elapsed", inplace=True)
mean.plot(  yerr=std)

如何解决这个问题?
供参考的最终 Dataframe 图像。

to94eoyn

to94eoyn1#

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#code to analyze data and create final dataframe (called "final"):
#some code here

#find the mean and std of each sample which are in triplicate from the final dataframe:
mean = final.groupby(np.arange(len(final.columns))//3, axis=1).mean()
std = final.groupby(np.arange(len(final.columns))//3, axis=1).std()

#plot mean with error bars and different colored lines for each sample
insert = range(1, 164)
for i in range(mean.shape[1]):
    plt.errorbar(insert, mean.iloc[:, i], yerr=std.iloc[:, i], label=mean.columns[i])
plt.legend()
plt.xlabel('Elapsed')
plt.ylabel('Mean')
plt.show()

相关问题