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

blocks.Pcolormesh seems to have different behavior than plt.pcolormesh #56

Closed
Xin-yang-Liu opened this issue Dec 4, 2020 · 6 comments
Closed

Comments

@Xin-yang-Liu
Copy link

Xin-yang-Liu commented Dec 4, 2020

I am not sure if it is a feature or a bug.
When I was playing with the example code:

import numpy as np
import matplotlib.pyplot as plt
import animatplot as amp


x = np.linspace(-2, 2, 5)
y = np.linspace(-2, 2, 5)
t = np.linspace(0, 2*np.pi, 40)

X, Y, T = np.meshgrid(x, y, t)

Z = np.sin(X*X+Y*Y-T)

block = amp.blocks.Pcolormesh(X[:,:,0], Y[:,:,0], Z, t_axis=2, cmap='RdBu')
plt.colorbar(block.quad)
plt.gca().set_aspect('equal')

anim = amp.Animation([block], amp.Timeline(t))

anim.controls()
plt.show()

I used a very coarse grid 55 instead of the original 5050, so that it is more clear.
The data array Z is of size 55 at a certain time step. But the generated animation shows a grid of shape 44.
It seems blocks.Pcolormesh use the value in Z as the "node" value in animation, which is different in plt.pcolormesh whereZ value is treated as the "mesh cell" value.

@johnomotani
Copy link
Contributor

What versions of animatplot and matplotlib are you using? For me with animatplot-0.4.2 and matplotlib-3.3.3, from the following the pcolormesh plot is identical (apart from tick labels) to the first frame of the animation.

import numpy as np
import matplotlib.pyplot as plt
import animatplot as amp


x = np.linspace(-2, 2, 5)
y = np.linspace(-2, 2, 5)
t = np.linspace(0, 2*np.pi, 40)

X, Y, T = np.meshgrid(x, y, t)

Z = np.sin(X*X+Y*Y-T)

plt.pcolormesh(X[:,:,0], Y[:,:,0], Z[:,:,0], cmap='RdBu')
plt.colorbar()


plt.figure()

block = amp.blocks.Pcolormesh(X[:,:,0], Y[:,:,0], Z, t_axis=2, cmap='RdBu')
plt.colorbar(block.quad)
plt.gca().set_aspect('equal')

anim = amp.Animation([block], amp.Timeline(t))

anim.controls()
plt.show()

We did change Pcolormesh recently though: #53

@Xin-yang-Liu
Copy link
Author

I am using the exact same version of animatplot and matplotlib as you.
plt
pcolormesh
the first figure is generated using plt.pcolormesh at time 0. The grid is 5 by 5.
In the animation, the grid is 4*4

@johnomotani
Copy link
Contributor

johnomotani commented Dec 4, 2020

Have you changed the default value of matplotlib.rcParams["pcolor.shading"]? Your output is what I get with plt.pcolormesh(X[:,:,0], Y[:,:,0], Z[:,:,0], shading="nearest"). shading="auto" (which should be equivalent to shading="nearest" for this case) will become the matplotlib default at some point, but for the moment the default is still shading="flat". We should pick up the default from matplotlib though, here

self. shading = kwargs.get("shading", plt.rcParams.get("pcolor.shading", "flat"))

so I'm confused.

animatplot is behaving as I'd expect, it's your matplotlib plot that seems odd. Maybe you could try creating a clean virtual environment (or conda environment) and make the plt.pcolormesh plot again?

@Xin-yang-Liu
Copy link
Author

Xin-yang-Liu commented Dec 5, 2020

I did not change any default value.
I also tried in a clean conda environment (python 3.7.8, matplotlib 3.3.3, numpy 1.19.4) and got the same figure.

@Xin-yang-Liu
Copy link
Author

Actually I was using plt.pcolormesh(Z[:,:,0]) and got the figure with shading="nearest"(5 * 5 grid) by default
Besides, I tried plt.pcolormesh(X[:,:,0],Y[:,:,0],Z[:,:,0]). It seems the default value becomes shading="flat" (4 * 4 grid)

@johnomotani
Copy link
Contributor

animatplot at the moment doesn't support calling like Pcolormesh(Z) without X and Y arrays. I think a PR adding that functionality would be welcome.

The current behaviour is consistent with matplotlib's pcolormesh when passing X, Y and Z, so this isn't a bug. The matplotlib behaviour without X and Y arguments is like calling (if Z is an array of shape (n,m))

plt.pcolormesh(np.arange(m+1), np.arange(n+1), Z)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants