0

I'm fairly new to scatter plots and python in general. I am trying to plot a third variable against an x and a y, however, I'm not quite sure how to about specifying that argument? So I would have X values which are ints, y values which are also ints and then on the graph itself I want the model scores to show. Is there any way to do this sort of thing?

Thank you.

2 Answers 2

2

You can use color to plot a third value. Here is a very minimal example :

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
plt.scatter(x,y, c=z, s=5, cmap=cm.hsv)
cbar= plt.colorbar()
plt.show()

enter image description here


Edit

You could also use the size of markers, their transparency, hue or rgb values to depict even more information. Here is an example with marker size, alpha level and color on a perceptually uniform colormap.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
import matplotlib.cm as cmx

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
t = np.random.rand(100)
w = np.random.rand(100)
fig, ax = plt.subplots(1, 1)
cmap = plt.get_cmap('plasma')
cNorm  = colors.Normalize(vmin=0, vmax=max(z))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmap)
for i in range(100):
    ax.scatter(x[i],y[i], c=scalarMap.to_rgba(z[i]), s=t[i]*100, cmap=cmx.plasma, alpha=w[i], edgecolor='none')
scalarMap.set_array([])
fig.colorbar(scalarMap,ax=ax)
for a in [0.1, 0.5, 0.9]:
    ax.scatter([], [], c='k', alpha=0.5, s=a*100, label=str(a), edgecolors='none')
l1 = ax.legend(scatterpoints=1, frameon=True, loc='lower left' ,markerscale=1)
for b in [0.25, 0.5, 0.75]:
    ax.scatter([], [], c='k', alpha=b, s=50, label=str(b), edgecolors='none')
ax.legend(scatterpoints=1, frameon=True, loc='lower right' ,markerscale=1)
fig.show()

enter image description here

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

6 Comments

I keep getting for some reason index tuple out range error, do you know why this may happen?
The three lists x,y and z need to be of the same length, check if it is the case by printing len(x),len(y),len(z)
In this case they're all ints? So I'm trying to do a while loop (with n as an int counter) inside a for loop (with i as an int counter) and train a model and plot the n and the i on the axis and have the score in the background, however I keep getting index tuple out of range error. Any ideas? Thank you
Can you udpate your question with the current relevant code ? It's not clear what your problem is from this comment alone
Any ideas as to why I keep getting index tuple out of range?
|
1

At face value, that question doesn't really make sense because a conventional scatterplot has only two axes, and of course you can't plot points with three dimensions (x, y and accuracy).

However, there are alternative ways to do so.

  1. Use colours
import numpy as np
from matplotlib import pyplot as plt

x = np.random.rand(200)
y = np.random.rand(200)

fig, ax = plt.subplots(figsize=(5, 5))
ax.scatter(x, y, c=(x + y), cmap='RdPu')

scatter takes a c argument, which can be a numeric value, as well as a cmap argument, which can be a string referencing a colormap.

The colormap object translates the numbers provided in c into points along a colour mapping, which you can think of as a gradient bar.

  1. Use 3D axes
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(5, 5))
ax = Axes3D(fig)

ax.scatter(x, y, (x + y))

This turns your 3rd dimension, accuracy, into an ordinary spatial dimension.

  1. Use size of the markers

Very similar to the color option in the first part, you can change the size of the scatter markers (given you have some idea about the scale of the values). So based on the first example, you can also do;

import numpy as np
from matplotlib import pyplot as plt

x = np.random.rand(200)
y = np.random.rand(200)

fig, ax = plt.subplots(figsize=(5, 5))
ax.scatter(x, y, c='k', s=5*(x + y), cmap='RdPu')

scatter takes also the s argument, that changes the size of the markers.

1 Comment

Do you know why I keep getting index tuple out of range error whenever I try to run the code above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.