gpt4 book ai didi

python - 当数据具有 NaN 时,在 matplotlib 中的线图中绘制阴影不确定区域

转载 作者:太空狗 更新时间:2023-10-29 20:57:55 25 4
gpt4 key购买 nike

我想要一个看起来像这样的情节: plot with uncertainty

我正在尝试使用 matplotlib 来做到这一点:

fig, ax = plt.subplots()

with sns.axes_style("darkgrid"):
for i in range(5):
ax.plot(means.ix[i][list(range(3,104))], label=means.ix[i]["label"])
ax.fill_between(means.ix[i][list(range(3,104))]-stds.ix[i][list(range(3,104))], means.ix[i][list(range(3,104))]+stds.ix[i][list(range(3,104))])
ax.legend()

我希望阴影区域与中心线的颜色相同。但现在,我的问题是 means 有一些 NaNfill_between 不接受。我得到错误

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

关于如何实现我想要的目标有什么想法吗?该解决方案不需要使用 matplotlib,只要它可以绘制我的一系列点及其对多个系列的不确定性。

最佳答案

好的。所以问题之一是我的数据的 dtypeobject 而不是 float 这导致 fill_between 到当它查看数字是否有限时失败。我终于设法通过 (a) 转换为 float 然后 (b) 解决不确定性和线条的匹配颜色问题,使用调色板。所以我有:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots()
clrs = sns.color_palette("husl", 5)
with sns.axes_style("darkgrid"):
epochs = list(range(101))
for i in range(5):
meanst = np.array(means.ix[i].values[3:-1], dtype=np.float64)
sdt = np.array(stds.ix[i].values[3:-1], dtype=np.float64)
ax.plot(epochs, meanst, label=means.ix[i]["label"], c=clrs[i])
ax.fill_between(epochs, meanst-sdt, meanst+sdt ,alpha=0.3, facecolor=clrs[i])
ax.legend()
ax.set_yscale('log')

这给了我以下结果: enter image description here

关于python - 当数据具有 NaN 时,在 matplotlib 中的线图中绘制阴影不确定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43064524/

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