-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
scatter not showing valid x/y points with invalid color #4354
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
Comments
As a work around something like import matplotlib.pyplot as plt
from matplotlib.cm import spring
import numpy as np
spring.set_bad("k", 1)
spring.set_under('k', 1)
colors = np.array([1, np.nan, 9, np.nan])
x = [0, 1, 2, 3]
plt.figure()
plt.imshow(colors.reshape((2, 2)), interpolation="nearest", cmap=spring)
plt.figure()
sc = plt.scatter(x, x, s=1e4,c=[1, 1, 10, 10], cmap=spring)
sc.set_array(colors)
plt.show() Will work (but I have no idea why the collection is treating nan as under, not as invalid. |
I think the OP is reporting a bug, or API inconsistency, that has always been present and is easily fixed. I will try that now. |
Sorry, I didn't mean it was caused by that code, just that it was related. |
The present behavior is explicitly documented in the scatter docstring, so it was deliberate and has been this way for a very long time. The situation is similar to that with pcolor, which also presently does not plot anything for invalid points, while pcolormesh uses the result of set_bad. |
Where is this documented? I see the comment in One plus side of the (some what) crazy scheme to strip all of the plotting methods off of |
@tacaswell, regarding your example with nans in an array to be colormapped: this simply has never been supported, but it would be easy to do so, with some performance cost. Long ago, there was very little support for nans in Numeric, numarray, and early numpy. Therefore I pushed for matplotlib to work with masked arrays, and I still rely on them. More recently, there seems to be a trend toward using nans internally at some points, but it is inconsistent. From the external API standpoint, in many (most?) places we use ma.masked_invalid on incoming arrays so that we can handle any combination of nans and masked arrays; but your example illustrates a case where nothing catches the nans, and the color mapping logic ends up treating them as "under". I haven't looked to see why, because the solution is to add one more call to masked_invalid to ensure the right result. |
You are right; the docstring is explicit about masked arrays, and the delete_masked_points utility is not calling masked_invalid first. The OP's basic point remains, however: it would be more consistent to let set_bad control masked (or invalid, if we add the check for nans) color values. |
Correction to my earlier comment: |
Correction to another of my comments: the more recent assumption actually was that for the most part we could transparently handle masked array input and arrays with nans identically, but it was OK to leave the documentation consistently talking about masked arrays. |
punted as not a blocker for 1.5. |
Works around a bug in matplotlib when coloring with NaN values in a scatter plot matplotlib/matplotlib#4354
Closing, as being fixed by #12422. I.e. from matplotlib 3.1 on, you can use |
scatter
plots are removing points with valid x/y coordinates but invalid color value. As colormaps have a special setting for what color should be used in case of invalid data points (used e.g. inimshow
array plots) maybe these points should indeed be plotted and plotted with the specified "bad value" color.Consider this example:
The right-hand side
scatter
plot is not showing thex=3
,y=3
,color=np.nan
data point:The text was updated successfully, but these errors were encountered: