Search code examples
pythonnumpymatplotlibtorch

ValueError: object __array__ method not producing an array : it occor when I run my code


ValueError: object array method not producing an array and TypeError: Invalid shape (1, 1, 6, 6) for image data

When attempting to run the following code, the following errors occur.

Code

import torch
import matplotlib.pyplot as plt

data = torch.tensor([[0, 0, 1, 1, 0, 0],
                     [0, 1, 0, 0, 1, 0],
                     [0, 0, 0, 0, 1, 0],
                     [0, 0, 1, 1, 0, 0],
                     [0, 1, 0, 0, 0, 0],
                     [0, 1, 1, 1, 1, 0]]).view(-1, 1, 6, 6).float()

# Convert data to a 2D array
data_np = data[0, 0].cpu().detach().numpy()

plt.imshow(data_np, cmap='gray')
plt.axis('off')
plt.show()

Error

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\\Lib\\tkinter\__init_\_.py", line 1968, in __call__
return self.func(\*args)
^^^^^^^^^^^^^^^^
File "C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\\Lib\\tkinter\__init_\_.py", line 862, in callit
func(\*args)
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\backends_backend_tk.py", line 271, in idle_draw
self.draw()
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\backends\\backend_tkagg.py", line 10, in draw
super().draw()
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\backends\\backend_agg.py", line 387, in draw
self.figure.draw(self.renderer)
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\artist.py", line 95, in draw_wrapper
result = draw(artist, renderer, \*args, \*\*kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\figure.py", line 3154, in draw
self.patch.draw(renderer)
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\patches.py", line 632, in draw
self.\_draw_paths_with_artist_properties(
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\patches.py", line 617, in \_draw_paths_with_artist_properties
renderer.draw_path(gc, \*draw_path_args)
File "C:\\Users\\mori\\Desktop\\ML_learning\\venv\\Lib\\site-packages\\matplotlib\\backends\\backend_agg.py", line 131, in draw_path
self.\_renderer.draw_path(gc, path, transform, rgbFace)
ValueError: object __array__ method not producing an array

My environments

  • torch==2.3.1+cu121
  • numpy==1.26.4
  • matplotlib==3.9.0
  • python==3.12.4

How can I solve these issues?


Solution

  • I am not able to reproduce your issue, though I have matched the versions of your library (except for torch which is 2.3.1 without CUDA support on my system).

    Nevertheless, the below snippet causes the same error

    import numpy as np
    import matplotlib.pyplot as plt
    
    data_np = np.ones((1, 1, 6, 6))
    
    plt.imshow(data_np, cmap='gray')
    plt.axis('off')
    plt.show()
    

    The solution is to use numpy.squeeze to get rid of axes of length 1. The data you give to plt.imshow will then be of shape (6,6) and they will be plotted without issues.

    import numpy as np
    import matplotlib.pyplot as plt
    
    data_np = np.ones((1, 1, 6, 6))
    
    plt.imshow(np.squeeze(data_np), cmap='gray')
    plt.axis('off')
    plt.show()