Search code examples
pythonmatplotlibgoogle-colaboratory

Not enough values to unpack in plt.pcolormesh


I am testing plotting a colored mesh based on two arrays. To do this, I first count the values ​​for the first half of the grid, then for the second. I get two arrays and save them to a file. Also I am storing arrays with coordinates.

k1s1 = np.linspace(-1.0, 1.0, 11)
k2s1 = np.linspace(-1.0, 0.0, 11)
grid1 = np.zeros([len(k1s), len(k2s)])

for i, k1 in enumerate(k1s1):
    for j, k2 in enumerate(k2s1):
        grid1[i][j] = 1

with open('/content/drive/My Drive/Colab Notebooks/arraytest1.txt', 'w') as f:
    np.savetxt(f, grid1)

k2s2 = np.linspace(0.0, 1.0, 11)
grid2 = np.zeros([len(k1s1), len(k2s2)])

for i, k1 in enumerate(k1s1):
    for j, k2 in enumerate(k2s2):
        grid2[i][j] = 2

with open('/content/drive/My Drive/Colab Notebooks/arraytest2.txt', 'w') as f:
    np.savetxt(f, grid2)

with open('/content/drive/My Drive/Colab Notebooks/k1s1.txt', 'w') as f:
    np.savetxt(f, k1s1)
with open('/content/drive/My Drive/Colab Notebooks/k2s1.txt', 'w') as f:
    np.savetxt(f, k2s1)
with open('/content/drive/My Drive/Colab Notebooks/k2s2.txt', 'w') as f:
    np.savetxt(f, k2s2)

Then I extract these arrays from the files and concatenate them.

with open('/content/drive/My Drive/Colab Notebooks/arraytest1.txt', 'r') as f:
    grid1 = np.loadtxt(f)
with open('/content/drive/My Drive/Colab Notebooks/arraytest2.txt', 'r') as f:
    grid2 = np.loadtxt(f)
with open('/content/drive/My Drive/Colab Notebooks/k1s1.txt', 'r') as f:
    k1s1n = np.loadtxt(f)
with open('/content/drive/My Drive/Colab Notebooks/k2s1.txt', 'r') as f:
    k2s1n = np.loadtxt(f)
with open('/content/drive/My Drive/Colab Notebooks/k2s2.txt', 'r') as f:
    k2s2n = np.loadtxt(f)

grid3 = np.append(grid1, grid2)

k1s3 = k1s1n
k2s3 = np.append(k2s1n, k2s2n)

Then I try to build a color grid using the resulting arrays.

plt.pcolormesh(k1s3, k2s3, grid3, cmap=plt.cm.get_cmap('jet'))
plt.axes().set_aspect('equal', adjustable='box')
plt.colorbar()

And on the line

plt.pcolormesh(k1s3, k2s3, grid3, cmap=plt.cm.get_cmap('jet'))

an error like this appears:

    Traceback (most recent call last)
<ipython-input-44-76ec1e216ce3> in <module>()
----> 1 plt.pcolormesh(k1s3, k2s3, grid3, cmap=plt.cm.get_cmap('jet'))
      2 plt.axes().set_aspect('equal', adjustable='box')
      3 plt.colorbar()

3 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py in pcolormesh(alpha, norm, cmap, vmin, vmax, shading, antialiased, data, *args, **kwargs)
   2723         *args, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
   2724         vmax=vmax, shading=shading, antialiased=antialiased,
-> 2725         **({"data": data} if data is not None else {}), **kwargs)
   2726     sci(__ret)
   2727     return __ret

/usr/local/lib/python3.7/dist-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1563     def inner(ax, *args, data=None, **kwargs):
   1564         if data is None:
-> 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1566 
   1567         bound = new_sig.bind(ax, *args, **kwargs)

/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py in pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
   6102         allmatch = (shading == 'gouraud')
   6103 
-> 6104         X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
   6105         Ny, Nx = X.shape
   6106         X = X.ravel()

/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py in _pcolorargs(funcname, allmatch, *args)
   5678                 if isinstance(Y, np.ma.core.MaskedArray):
   5679                     Y = Y.data
-> 5680             nrows, ncols = C.shape
   5681         else:
   5682             raise TypeError(

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

Though I checked all dimensions of all arrays. Please tell me why this can be and how to fix it?


Solution

  • Ok, I found the problem and solved it. When I connected the 2D arrays grid1 and grid2 using the np.append() function, one array would be appended to the end of the other, resulting in a large ONE-DIMENSIONAL array.

    In order to get one TWO DIMENSIONAL array after concatenation, you need to use np.concatenate().