jeudi 19 août 2021

How to fix "not enough values to unpack" when trying to plot 3D data as colormesh?

With matplotlib I am trying to plot 3D data as a 2D colormap. Each point has a x and a y coordinate, and a 'height' z. This height should determine the color a certain x/y region is colored in.

Here is the code I have been trying:

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

x = []
y = []
z = []
for index in range(100):
    a = random.random()
    b = random.random()
    c = np.exp(-a*a - b*b)
    x.append(a)
    y.append(b)
    z.append(c)

cmap = plt.get_cmap('PiYG')
fig, ax = plt.subplots()
ax.pcolormesh(x, y, z, cmap=cmap)

But it gives an error

ValueError: not enough values to unpack (expected 2, got 1)

Maybe I am trying the wrong thing?

Remark: The three lists x,y,z and calculated for the example above, but in reality I have just three lists with "random" numbers in it I want to vizualize. I cannot calculate z given x and y.

I could also use imshow to create the plot I want, but I have to convert my original data into a matrix first. Maybe there is a function I can use?




Aucun commentaire:

Enregistrer un commentaire