Skip to content

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

Closed

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.

Activity

  1. tacaswell commented on Apr 2, 2021

    @tacaswell
    Member

    I suspect that 78121f5 is the change that broke this (which is to make sure that we take in scalars).

  2. added this to the v3.4.2 milestone on Apr 2, 2021
  3. tacaswell commented on Apr 2, 2021

    @tacaswell
    Member

    We probably should accept data with trailing 1s , not sure the best way to do this though.

  4. QuLogic commented on Apr 6, 2021

    @QuLogic
    Member

    I have a vague recollection of us explicitly not allowing extraneous singular dimension, but I don't recall exactly where that was.

  5. QuLogic commented on Apr 6, 2021

    @QuLogic
    Member

    Maybe I was thinking of #15090, where after discussion, we eventually did accept it. But on the other hand, #13354 removed support for non-1D where it was not acceptable.

  6. modified the milestones: v3.4.2, v3.4.3 on May 6, 2021
  7. anntzer commented on Jul 22, 2021

    @anntzer
    Contributor

    I personally don't think we should support trailing 1's (consistently with numpy broadcasting), except in very specific cases where there is strong precedent for them (e.g. (M, N, 1) images as in #15090).
    (IOW I'm voting to close this as wontfix.)

  8. modified the milestones: v3.4.3, v3.4.4 on Aug 11, 2021
  9. anntzer commented on Sep 19, 2021

    @anntzer
    Contributor

    Let's close this as expected change/wontfix.

  10. modified the milestones: v3.4.4, on Sep 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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