Skip to content

[Bug]: contour on 3d plot fails if x and y are 1d and different lengths #21310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DWesl opened this issue Oct 7, 2021 · 6 comments · Fixed by #21326
Closed

[Bug]: contour on 3d plot fails if x and y are 1d and different lengths #21310

DWesl opened this issue Oct 7, 2021 · 6 comments · Fixed by #21326
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! topic: mplot3d
Milestone

Comments

@DWesl
Copy link
Contributor

DWesl commented Oct 7, 2021

Bug summary

Calling ax.contour(x, y, z) fails if ax was created with projection="3d" and x and y are both 1d and different lengths.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

NX = 30
NY = 20
x = np.linspace(-10, 10, NX)
y = np.linspace(-10, 10, NY)
z = np.random.randint(0, 2, [NY, NX])

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

ax.contour(x, y, z, [0.5], zdir="z")

plt.show()

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 12, in <module>
    ax.contour(x, y, z, [0.5], zdir="z")
  File "/usr/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 2076, in contour
    self.auto_scale_xyz(X, Y, Z, had_data)
  File "/usr/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 650, in auto_scale_xyz
    np.column_stack([X, Y]), not had_data)
  File "<__array_function__ internals>", line 5, in column_stack
  File "/usr/lib/python3.8/site-packages/numpy/lib/shape_base.py", line 656, in column_stack
    return _nx.concatenate(arrays, 1)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 30 and the array at index 1 has size 20

If I call plt.pause(1) after the traceback, a plot shows up showing the plot with x and y limits [0, 1], which doesn't show much that is comprehensible. If I call ax.set_xlim(-10, 10) and ax.set_ylim(-10, 10), the plot shows up fine.

Expected outcome

Changing NX to 20 produces a 3d plot with a set of contours hovering around z=0.5

Operating system

Cygwin, Windows

Matplotlib Version

3.3.3, 3.4.3

Matplotlib Backend

TkAgg, matplotlib_inline.backend_inline

Python version

3.8.10; 3.9.7

Jupyter version

No response

Other libraries

matplotlib_inline==0.1.3

Installation

conda

Conda channel

conda-forge

@DWesl
Copy link
Contributor Author

DWesl commented Oct 7, 2021

I suspect calling np.meshgrid(X, Y) before self.auto_scale_xyz(X, Y, Z, had_data) in contour would help, given that the problem seems to be auto_scale_xyz calls np.column_stack([X.reshape(-1), Y.reshape(-1)])

@dstansby
Copy link
Member

dstansby commented Oct 8, 2021

Yes, if you add x, y = np.meshgrid(x, y) it works fine. The action here is to fix the documentation for Axes3d.contour, which currently just says:

Input data.

Compare to the documentation for Axes.contour, which has the much more helpful:

The coordinates of the values in Z.

X and Y must both be 2D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == M is the number of columns in Z and len(Y) == N is the number of rows in Z.

If not given, they are assumed to be integer indices, i.e. X = range(M), Y = range(N).

@dstansby dstansby added Documentation Good first issue Open a pull request against these issues if there are no active ones! labels Oct 8, 2021
@Kislovskiy
Copy link
Contributor

@dstansby @DWesl I've opened: #21321 Could you take a look?

@DWesl
Copy link
Contributor Author

DWesl commented Oct 8, 2021

#21321 looks like it fixes the terse documentation. I made #21322 to fix the crash in Axes3D.contour.

@jklymak
Copy link
Member

jklymak commented Oct 11, 2021

I don't think this is closed though? ax.contour(x, y, Z) works if x and y are vectors?

@jklymak jklymak reopened this Oct 11, 2021
dstansby added a commit that referenced this issue Oct 11, 2021
…321-on-v3.5.x

Backport PR #21321 on branch v3.5.x (DOC Update description of ax.contour method, resolves #21310)
tacaswell pushed a commit to tacaswell/matplotlib that referenced this issue Oct 12, 2021
DOC Update description of ax.contour method, resolves matplotlib#21310
@QuLogic
Copy link
Member

QuLogic commented Oct 20, 2021

This was accidentally re-closed by the same commit getting pushed to main again.

@QuLogic QuLogic reopened this Oct 20, 2021
@QuLogic QuLogic added this to the v3.5.0 milestone Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! topic: mplot3d
Projects
None yet
5 participants