2

I have two lists containing the x and y coordinates of some points. There is also a list with some values assigned to each of those points. Now my question is, I can always plot the points (x,y) using markers in python. Also I can select colour of the marker manually (as in this code).

import matplotlib.pyplot as plt

x=[0,0,1,1,2,2,3,3]
y=[-1,3,2,-2,0,2,3,1]
colour=['blue','green','red','orange','cyan','black','pink','magenta']
values=[2,6,10,8,0,9,3,6]

for i in range(len(x)):
    plt.plot(x[i], y[i], linestyle='none', color=colour[i], marker='o')

plt.axis([-1,4,-3,4])
plt.show()

But is it possible to choose a colour for the marker marking a particular point according to the value assigned to that point (using cm.jet, cm.gray or similar other color schemes) and provide a colorbar with the plot ?

For example, this is the kind of plot I am looking for

enter image description here

where the red dots denote high temperature points and the blue dots denote low temperature ones and others are for temperatures in between.

kanayamalakar
  • 564
  • 1
  • 11
  • 27

1 Answers1

4

You are most likely looking for matplotlib.pyplot.scatter. Example:

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

# Generate data:
N = 10
x = np.linspace(0, 1, N)
y = np.linspace(0, 1, N)
x, y = np.meshgrid(x, y)
colors = np.random.rand(N, N) # colors for each x,y

# Plot
circle_size = 200
cmap = matplotlib.cm.viridis # replace with your favourite colormap 
fig, ax = plt.subplots(figsize=(4, 4))
s = ax.scatter(x, y, s=circle_size, c=colors, cmap=cmap)

# Prettify
ax.axis("tight")
fig.colorbar(s)

plt.show()

Note: viridis may fail on older version of matplotlib. Resulting image:

Circles coloured by colormap

Edit

scatter does not require your input data to be 2-D, here are 4 alternatives that generate the same image:

import matplotlib
import matplotlib.pyplot as plt

x = [0,0,1,1,2,2,3,3]
y = [-1,3,2,-2,0,2,3,1]
values = [2,6,10,8,0,9,3,6]
# Let the colormap extend between:
vmin = min(values)
vmax = max(values)

cmap = matplotlib.cm.viridis
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)

fig, ax = plt.subplots(4, sharex=True, sharey=True)
# Alternative 1: using plot:
for i in range(len(x)):
    color = cmap(norm(values[i]))
    ax[0].plot(x[i], y[i], linestyle='none', color=color, marker='o')

# Alternative 2: using scatter without specifying norm
ax[1].scatter(x, y, c=values, cmap=cmap)

# Alternative 3: using scatter with normalized values:
ax[2].scatter(x, y, c=cmap(norm(values)))

# Alternative 4: using scatter with vmin, vmax and cmap keyword-arguments
ax[3].scatter(x, y, c=values, vmin=vmin, vmax=vmax, cmap=cmap)

plt.show()
oystein
  • 1,507
  • 1
  • 11
  • 13
  • But it gives `AttributeError: 'module' object has no attribute 'viridis'`. – kanayamalakar Apr 23 '16 at 11:09
  • 'viridis' is one of the new matplotlib colormaps; you need to download an additional module to use it. You can replace by 'jet' which is the (ugly) matplotlib default – Reblochon Masque Apr 23 '16 at 11:34
  • Okay. And another thing, I dont want to do a meshgrid I have discrete points with random coordinates that are given in the lists x and y. How do I plot them with this scheme? – kanayamalakar Apr 23 '16 at 11:38
  • Just omit the meshgrid, scatter should be able to figure it out :) – oystein Apr 23 '16 at 11:44
  • And along with that `colors = np.random.rand(N)` in place of `colors = np.random.rand(N, N)` – kanayamalakar Apr 23 '16 at 11:49
  • Thank you @oystein. Can you tell me what module is needed to be able to use `viridis`? – kanayamalakar Apr 23 '16 at 12:34
  • It is included in the latest matplotlib version 1.5, see here if you do not want to update: [How to use viridis in matplotlib 1.4](http://stackoverflow.com/questions/32484453/how-to-use-viridis-in-matplotlib-1-4) – oystein Apr 23 '16 at 12:50