Python中一些数据点没有出现在PyPlot上

2024-04-25 05:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

然而,当我在绘图时,红色的Observation点没有出现在我的图上;我不确定为什么。在

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

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

但我用来绘制的代码不允许它们出现:

^{pr2}$

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

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


Tags: 数据代码命令绘图plot图表fig绘制
1条回答
网友
1楼 · 发布于 2024-04-25 05:58:59

您可能需要考虑下面的例子,在这个例子中,比较了问题中有和没有fill函数的两种情况。在

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曲线可能位于图中完全不同的位置,这取决于数据帧的索引。在

相关问题 更多 >