xarray icon indicating copy to clipboard operation
xarray copied to clipboard

plt.imshow() vs xarray_dataset.plot.imshow() not rendering correctly | Potential Bug

Open melioristic opened this issue 1 year ago • 1 comments

What is your issue?

I have 2d data which I want to visualise. The visuals look completely different if I use plt.imshow() vs xarray_dataset.plot.imshow() There are mainly two issues

  • First, the array is flipped. (I think this is manageable but inconsistent)
  • Secondly, the plots don't look correct. This can be best illustrated by the figures themselves.

For example this is the xarray code I am using.

day_data.plot.imshow(cmap= "Blues", vmin =1, vmax = 100)
plt.show()

And this is the image that I get. xarray

Secondly, when I use the matplotlib to plot the values.

plt.imshow(day_data.values, vmin = 1, vmax = 100, cmap = 'Blues')
plt.show()

I get this plot. matplotlib

Since it is a discharge data I would expect to see the second plot. Can someone tell me what is the issue here?

P.S.

This is how day_data looks like.

xarray.DataArray'dis06'y: 950x: 1000
array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]], dtype=float32)
Coordinates:
time
()
datetime64[ns]
2019-10-24T06:00:00
step
()
timedelta64[ns]
06:00:00
surface
()
float64
0.0
latitude
(y, x)
float64
...
longitude
(y, x)
float64
...
valid_time
()
datetime64[ns]
2019-10-24T12:00:00
Attributes:
GRIB_paramId :
240023
GRIB_dataType :
sfo
GRIB_numberOfPoints :
950000
GRIB_typeOfLevel :
surface
GRIB_stepUnits :
1
GRIB_stepType :
avg
GRIB_gridType :
lambert_azimuthal_equal_area
GRIB_NV :
0
GRIB_cfName :
unknown
GRIB_cfVarName :
dis06
GRIB_gridDefinitionDescription :
Lambert azimuthal equal area projection
GRIB_missingValue :
9999
GRIB_name :
Mean discharge in the last 6 hours
GRIB_shortName :
dis06
GRIB_units :
m**3 s**-1
long_name :
Mean discharge in the last 6 hours
units :
m**3 s**-1
standard_name :
unknown

melioristic avatar Aug 14 '22 08:08 melioristic

I think that the reverted y-axis is intentional.

Imshow is intended to display images which have the origin at the top. xarray tries to keep the origin at the bottom for all plots, especially when attaching coordinates to the label it might be confusing that they are reverse.

headtr1ck avatar Oct 13 '22 08:10 headtr1ck

Reverting the axis is understandable, but there is a lot of difference in the way the figures are rendered. The major issue is seeing the dots instead of the continuous lines.

melioristic avatar Oct 23 '22 08:10 melioristic

The two relevant kwargs to set to reproduce the matplotlib plotting are: yincrease=False and interpolation=None

I don't know why the default of xarray is interpolation="nearest"...

headtr1ck avatar Oct 26 '22 22:10 headtr1ck

@melioristic could you reproduce the matplotlib results by setting interpolation?

headtr1ck avatar Mar 13 '23 20:03 headtr1ck