1

Say I have three sets of data x, y and z. I want to plot a scatter of x and y, but I want to assign colours to different points depending on what their corresponding z values are.

So for example for every point where z is in the range 0 to 1 I want the data points to be red, and when z in the range 1 to 3 I want the points to be blue etc.

How do I do this?

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
ljanex
  • 23
  • 1
  • 3
  • 2
    Hi ljanex, I see you are new to stackoverflow. Welcome! I would like to help you ask better questions and thus get more direct answers (and not get downvoted). The issue with this question is that it simply states your goal, which is far too broad. Instead, discuss what you've tried, which tools and libraries you are using, and why they do or do not produce the results you expect. Good luck! – Wheezil Oct 26 '18 at 21:20
  • Which plotting library are you using? – dwjbosman Oct 26 '18 at 23:23

1 Answers1

2

Try this. adopted from ImportanceOfBeingErnest answer here

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

x = np.linspace(0, 10, 100)
y = np.random.randint(0, 50, size=100)
z = np.random.rand(100)*10

bounds = [0,1,3,10]
colors = ["r", "b", "g"]

plt.figure(figsize=(8,6))

cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, len(colors))

rect = plt.scatter(x, y, s = 100, c=z, cmap=cmap, norm=norm)

cbar = plt.colorbar(rect, spacing="proportional")
cbar.set_label('Color', rotation=270, labelpad=10)

for i, txt in enumerate(z):
    plt.annotate(np.around(txt, 1), (x[i], y[i]))
plt.show()

enter image description here

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40