1

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

2
  • It would help to have a minimal reproducible example available. Commented Apr 9, 2017 at 21:51
  • 1
    Have you checked if the red dots are behind the blue area? If so, you can change their zorder. Commented Apr 10, 2017 at 0:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.