I am not sure if this is a bug or an optimization, but in most cases it is fine to plot bars of arrays containing NaN (only the NaN values are not displayed, the rest is ok).
Here I found that when the x array or the bottom array have NaNs at the extremities, no bar is displayed.
(barh has the same behavior).
Code for reproduction
importnumpyasnp, matplotlibasmpl, matplotlib.pyplotaspltbarx=np.arange(3, dtype=float)
barheights=np.array([0.5, 1.5, 2.0])
barstarts=np.array([0.77]*3)
barx[[0,2]] =np.NaN# alternatively:#barstarts[[0,2]] = np.NaNfig, ax=plt.subplots()
ax.bar(barx, barheights, bottom=barstarts)
# Not ok, nothing shown!# Same behavior with ax.barh:ax.barh(barx, barheights, left=barstarts)
# However, the behavior is correct when NaN only affect the 'heights' argument:fig, ax=plt.subplots()
ax.bar(np.arange(3), np.array([np.NaN, 1, np.NaN]), bottom=np.array([0.2, 0.3, 0.4]))
# OK, one bar is displayed.
In 7c4b000 / #12903 we added some logic to make sure that width would work with interesting units, notable time deltas and and datetime data.
To do this we effective add the width to the first element of the x data, run it through the conversion machinery, and then subtract off the already converted value of the first data point to convert the "width" in (possibly unitful) user data into our internal version of it. However, if the first point in nan, then the width is poisoned to also be nan which means that when we make the Rectangles the x positions are nan which then (correctly) does not render anything.
Activity
tacaswell commented on Aug 26, 2022
I can reproduce this, but do not understand it yet.
tacaswell commented on Aug 26, 2022
In 7c4b000 / #12903 we added some logic to make sure that width would work with interesting units, notable time deltas and and datetime data.
To do this we effective add the width to the first element of the x data, run it through the conversion machinery, and then subtract off the already converted value of the first data point to convert the "width" in (possibly unitful) user data into our internal version of it. However, if the first point in nan, then the width is poisoned to also be nan which means that when we make the Rectangles the x positions are nan which then (correctly) does not render anything.
jklymak commented on Aug 26, 2022
I guess we should be using the new safe_first_element excluding NaNs?
tacaswell commented on Aug 26, 2022
The new one only drops None, but I just finished adapting it to work with nan as well. PR shortly....
FIX: show bars when the first location is nan
FIX: show bars when the first location is nan
FIX: show bars when the first location is nan
FIX: show bars when the first location is nan