0

sample graph

I'd like to make comparing this Prediction and Test values easier, so I'm thinking two ways to achieve that:

  1. Scale the X and Y axis to the same scale
  2. Plot a linear line (y=x)
  3. Really like to have some way to either 'exclude' the outliers or perhaps 'zoom in' to the area where the points are dense, without manually excluding the outliers from the dataset (so its done automatically). Is this possible?
sns.scatterplot(y_pred, y_true)
plt.grid()

Looked around and tested plt.axis('equal') as mentioned on another question but it didn't seem quite right. Tried using plt.plot((0,0), (30,30)) to create the linear plot but it didn't show anything. Any other input on how to visualise this would be really appreciated as well. Thanks!

Joooeey
  • 3,394
  • 1
  • 35
  • 49
strivn
  • 309
  • 2
  • 8
  • For the linear plot, you should probably do `plt.plot([0,30], [0,30])` – Kins Jul 07 '22 at 14:20
  • This is almost off-topic as the technical questions in here have easy answers which the OP has already found. Maybe a better fit for the Data Science Stack Exchange? – Joooeey Jul 07 '22 at 14:22
  • It would be nice to get more examples on what the data set could look like. The way it's phrased, it's vague. – Joooeey Jul 07 '22 at 14:22
  • Lots of matplotlib backends support zooming out of the box. What environment do you use? What does a plot window look like for you? – Joooeey Jul 07 '22 at 14:25

2 Answers2

1

To plot the linear line:

plt.plot([0,30], [0,30])

To scale x and y axis to same scale (see doc for set_aspect):

plt.xlim(0, 30)
plt.ylim(0, 30)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()

From the doc for set_aspect:

Axes.set_aspect(aspect, adjustable=None, anchor=None, share=False)
Set the aspect ratio of the axes scaling, i.e. y/x-scale
aspect='equal': same as aspect=1, i.e. same scaling for x and y.

Kins
  • 547
  • 1
  • 5
  • 22
1

There are short ways to achieve everything you've suggested:

  1. Force scaled axes with matplotlib.axes.Axes.set_aspect.
  2. Add an infinite line with slope 1 through he origin with matplotlib.axes.Axes.axline
  3. Set your plot to interactive mode, so you can pan and zoom. The way to do this depends on your environment and is explained in the docs.

Best to combine them all.

import matplotlib.pyplot as plt
from numpy import random

plt.ion()  # activates interactive mode in most environments
plt.scatter(random.random_sample(10), random.random_sample(10))
ax = plt.gca()
ax.axline((0, 0), slope=1)
ax.set_aspect('equal', adjustable='datalim')  # force equal aspect
Joooeey
  • 3,394
  • 1
  • 35
  • 49