0

I'm trying to plot the predicted vs actual of a stock using Seaborn's scatterplot, I can plot the scatter fine, but what I want to do is also visualise where today's data is, in a different colour.

I tried doing:

current_x = df['Prediction'].iloc[-1]
current_y = df['Actual'].iloc[-1]

and plotting that but got this error message :

ValueError: If using all scalar values, you must pass an index.

Any help would be really appreciated cheers.

EDIT:

So I have a df containing a df['Prediction'] and df['Actual'] columns of price data, the code to print the scatterplot I have used so far is very imple:

sns.scatterplot(x='Predicted', y='Actual, data=data)

What I'm looking for is just to also plot, on top of this original scatter graph, todays most recent data for x and y, so .iloc[-1] for each, if you will.

Hami
  • 704
  • 2
  • 9
  • 27
top bantz
  • 585
  • 1
  • 12
  • 29

3 Answers3

3

Use the argument hue when creating the scatterplot. If you don't have a hue, you can create one easily for your simple case:

data['hue']=[0]*(len(df)-1)+[1]
sns.scatterplot(x='Predicted', y='Actual', hue='hue', data=data)
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
2

Bit hacky, but this works.

data = pd.DataFrame({'Prediction': np.random.rand(10), 
                     'Actual': np.random.rand(10)})

sns.scatterplot(x='Prediction', y='Actual', data=data)
sns.scatterplot(x='Prediction', y='Actual', data=data.iloc[-1].to_frame().T)
Simon Rogers
  • 344
  • 1
  • 10
2

In case you want to use matplotlib directly instead of seaborn, it's very straight forward to plot all but the last point and the last point separately.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({'Prediction': np.random.rand(10), 
                     'Actual': np.random.rand(10)})

# Plot all but the last point
plt.scatter(x='Prediction', y='Actual', data=data.iloc[:-1])
# Plot only the last point
plt.scatter(x='Prediction', y='Actual', data=data.iloc[-1])

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712