Skip to content

ValueError: operands could not be broadcast together with remapped shapes. Different beahviour between package versions. #18394

@DanielOram

Description

@DanielOram

Bug report

Bug summary

upgrading matplotlib to 3.3.1 throws error when plotting 3d graph (using code for reproduction) but works completely fine for package versions <=3.2.2.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

num_data = 10

xs=np.random.uniform(-50,50,(num_data,1))
ys=np.random.uniform(-50,50,(num_data,1))

noise = np.random.uniform(-1,1,(num_data,1))

outputs = -4*xs + 7*ys + 11 + noise

outputs = outputs.reshape(num_data,)


fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(xs,ys,outputs)

ax.set_xlabel('xs')
ax.set_ylabel('ys')
ax.set_zlabel('Outputs')

ax.view_init(azim=100)
plt.show()

Actual outcome

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-26db9c11f4d1> in <module>
     20 ax = fig.add_subplot(111, projection='3d')
     21 
---> 22 ax.plot(xs,ys,outputs)
     23 
     24 ax.set_xlabel('xs')

P:\anaconda3\envs\py3-TF2\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in plot(self, xs, ys, zdir, *args, **kwargs)
   1467 
   1468         # Match length
-> 1469         zs = np.broadcast_to(zs, np.shape(xs))
   1470 
   1471         lines = super().plot(xs, ys, *args, **kwargs)

<__array_function__ internals> in broadcast_to(*args, **kwargs)

P:\anaconda3\envs\py3-TF2\lib\site-packages\numpy\lib\stride_tricks.py in broadcast_to(array, shape, subok)
    178            [1, 2, 3]])
    179     """
--> 180     return _broadcast_to(array, shape, subok=subok, readonly=True)
    181 
    182 

P:\anaconda3\envs\py3-TF2\lib\site-packages\numpy\lib\stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
    123     it = np.nditer(
    124         (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
--> 125         op_flags=['readonly'], itershape=shape, order='C')
    126     with it:
    127         # never really has writebackifcopy semantics

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (10,) and requested shape (10,1)

Expected outcome

A simple 3d plot should be drawn with 10 datapoints.

I have run the code using matplotlib version 3.2.2 and 3.1.3 and the code runs fine. I can't find anything in the docs that suggests there have been changes to how the matplotlib.pyplot.plot function works (the docs look identical between versions). I might be wrong.

Additional

If I add the following code

xs = xs.reshape(num_data,)
ys = ys.reshape(num_data,)

so that the code now looks like this

import numpy as np
import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

num_data = 10

xs=np.random.uniform(-50,50,(num_data,1))
ys=np.random.uniform(-50,50,(num_data,1))

noise = np.random.uniform(-1,1,(num_data,1))

outputs = -4*xs + 7*ys + 11 + noise

outputs = outputs.reshape(num_data,)
xs = xs.reshape(num_data,)
ys = ys.reshape(num_data,)


fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(xs,ys,outputs)

ax.set_xlabel('xs')
ax.set_ylabel('ys')
ax.set_zlabel('Outputs')

ax.view_init(azim=100)
plt.show()

This now works using 3.3.1. Seems that some automatic reshaping function that occurred in versions pre 3.3.1 isn't there anymore? Maybe not a bug?

Matplotlib version

  • Operating system: Windows 10 Pro
  • Matplotlib version: 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.7
  • Jupyter version (if applicable): 6.1.1
  • Other libraries:

matplotlib installed using conda through the default channel.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions