Gary
Gary

Reputation: 2167

Some Data Points not Appearing on PyPlot in Python

I am trying to plot a chart that shows the Observation data points, along with the corresponding prediction.

However, as I am plotting, the red Observation dots are not appearing on my plot; and I am unsure as to why.

They do appear when I run the following in another line:

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

But the code that I am using to plot does not allows them to show up:

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)

My current plot, where the red Observation points are not appearing. The red Observation data points are not appearing

The plot when I run the plotting code in it's own line. I'd like these dots and the blue line to appear in the plot above: enter image description here

Upvotes: 1

Views: 2829

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

You may want to consider the following example, where the two cases with and without the fill function from the question are compared.

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

As can be seen, the plot curves may sit at completely different positions in the diagram, which would depend on the index of the dataframe.

Upvotes: 2

Related Questions