0

I've got the following code:

x = range(1, 100) # pixel x coordinates
y = range(21, 120) # pixel y coordinates
z = [-2, 5, 1, ..., 1] # should be mapped to colors

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize

cmap = cm.autumn
norm = Normalize(vmin=min(z), vmax=max(z))
colormap = cmap(norm(5))
plt.plot(x, y, cm=colormap)

And now I want to plot these points [x, y] such that:

  1. Each point should be a pixel (since len(x) may be about 35'000), not just a marker (I'm afraid they'll overlap otherways).
  2. Using this question, I'd like to take a colormap and map min(z) to let's say white color and max(z) to a black color (and display a legend).

How can I do it using matplotlib?

1 Answers1

0

Since you are talking about pixels instead of markers, and I don't know whether you got data for all pixel coordinates or just for some, I'm gonna show you something for both. I'll include the numpy package, because it makes everything with arrays easier.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize

x = range(1, 100) # pixel x coordinates
y = range(21, 120) # pixel y coordinates

Assuming you have z-data for all coordinates spanned by [x, y]:

In [1]: z = np.random.rand(len(x), len(y)) # should be mapped to colors
plt.imshow(z)
plt.colorbar()
Out [1]:

enter image description here

Assuming every z value maps to a [x, y] coordinate pair, but not all coordinates are covered:

In [2]:
z = np.random.rand(len(x))

arr = np.zeros([max(x) + 1, max(y) + 1])
arr[x, y] = z

plt.imshow(arr)
plt.colorbar()

Out [2]:

enter image description here

flurble
  • 1,086
  • 7
  • 21