Skip to content
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

Graceful handling of a numpy matrix #1558

Closed
gustavla opened this issue Dec 4, 2012 · 7 comments · Fixed by #13089
Closed

Graceful handling of a numpy matrix #1558

gustavla opened this issue Dec 4, 2012 · 7 comments · Fixed by #13089
Assignees
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Documentation third-party integration
Milestone

Comments

@gustavla
Copy link

gustavla commented Dec 4, 2012

If you give quiver numpy matrices, it will crash. It raises a ValueError for a reshape, which can be hard to realize was caused by the matrices (potentially to great frustration for the user).

The source of the problem boils down to ravel() for np.matrix creating a row matrix of shape (1, N), instead of an ndarray with shape (N,).

I naively tried fixing it (pull request: #1546) by throwing in a call to np.asarray, but it was rightly closed because it would break support for masked arrays. It was also pointed out that it was likely to be a problem beyond just quiver.

As I see it, the issue is either resolved by adding support for numpy.matrix, or better information to the users of the source of the error.

@WeatherGod
Copy link
Member

Wouldn't doing an np.squeeze() on the result of ravel() do the trick? Not
sure about the implications with masked arrays and such, but just a thought.

@gustavla
Copy link
Author

gustavla commented Dec 4, 2012

That handles masked arrays well, but it still retains the shape (1, N) after the squeeze.

@tacaswell
Copy link
Member

From the other thread, we need to make sure the docs are clear that mpl does not support matrix.

@story645 story645 added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues third-party integration labels May 31, 2016
@NelleV NelleV added Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues and removed Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Oct 11, 2016
@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2016

The correct way to fix this is probably to figure out each call site of np.asanyarray (there's like ~25 of them) to see whether they should be replaced by calls to np.asarray. The trick here is that you probably still want masked arrays to pass through, so perhaps you really need a helper function that behaves like asarray, except that masked arrays are passed through.

This helper function should probably(?) then be used in most places where either asarray or asanyarray is used.

@dopplershift
Copy link
Contributor

Instead of only whitelisting masked arrays, it should blacklist matrix. Otherwise, you needlessly preclude other ndarray subclasses.

@NelleV NelleV added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues and removed Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Oct 11, 2016
@efiring
Copy link
Member

efiring commented Oct 11, 2016

On 2016/10/11 9:17 AM, Ryan May wrote:

Instead of only whitelisting masked arrays, it should blacklist matrix.
Otherwise, you needlessly preclude other |ndarray| subclasses.

Right. The matrix subclass is an anomaly that causes more problems than
it is worth; it alters some of the most fundamental ndarray behavior.
In most discussions I have seen, the consensus advice is to discourage
its use.

@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2016

Agreed, just blacklisting matrix and matrix subclasses seems better.

@NelleV NelleV self-assigned this Nov 8, 2016
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@QuLogic QuLogic modified the milestones: needs sorting, v3.1 Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Documentation third-party integration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants