gpt4 book ai didi

python - 一些数据点没有出现在 Python 的 PyPlot 上

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

我正在尝试绘制一个图表,显示观察数据点以及相应的预测。

但是,当我绘图时,红色观察点没有出现在我的绘图上;我不确定为什么。

当我在另一行中运行以下命令时,它们确实会出现:

fig = plt.figure(figsize = (20,6))
plt.plot(testY, 'r.', markersize=10, label=u'Observations')
plt.plot(predictedY, 'b-', label=u'Prediction')

但是我用来绘制的代码不允许它们显示:

def plotGP(testY, predictedY, sigma):
fig = plt.figure(figsize = (20,6))
plt.plot(testY, 'r.', markersize=10, label=u'Observations')
plt.plot(predictedY, 'b-', label=u'Prediction')
x = range(len(testY))
plt.fill(np.concatenate([x, x[::-1]]), np.concatenate([predictedY - 1.9600 * sigma, (predictedY + 1.9600 * sigma)[::-1]]),
alpha=.5, fc='b', ec='None', label='95% confidence interval')


subset = results_dailyData['2010-01':'2010-12']
testY = subset['electricity-kWh']
predictedY = subset['predictedY']
sigma = subset['sigma']

plotGP(testY, predictedY, sigma)

我当前的绘图,红色观察点没有出现。 The red Observation data points are not appearing

当我在它自己的行中运行绘图代码时的绘图。我希望这些点和蓝线出现在上图中: enter image description here

最佳答案

您可能需要考虑以下示例,其中比较了问题中使用和不使用填充函数的两种情况。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import pandas as pd


def plotGP(ax, testY, predictedY, sigma, showfill=False):
ax.set_title("Show fill {}".format(showfill))
ax.plot(testY, 'r.', markersize=10, label=u'Observations')
ax.plot(predictedY, 'b-', label=u'Prediction')
x = range(len(testY))
if showfill:
ax.fill(np.concatenate([x, x[::-1]]), np.concatenate([predictedY - 1.9600 * sigma, (predictedY + 1.9600 * sigma)[::-1]]),
alpha=.5, fc='b', ec='None', label='95% confidence interval')

x = np.linspace(-5,-2)
y = np.cumsum(np.random.normal(size=len(x)))
sigma = 2

df = pd.DataFrame({"y" : y}, index=x)

fig, (ax, ax2) =plt.subplots(2,1)
plotGP(ax,df.y, df.y, sigma, False)
plotGP(ax2, df.y, df.y, sigma, True)

plt.show()

enter image description here

可以看出,plot 曲线可能位于图表中完全不同的位置,这取决于数据帧的索引。

关于python - 一些数据点没有出现在 Python 的 PyPlot 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43312005/

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