Light_B
Light_B

Reputation: 1800

Removing the shared colorbar in Xarray subplot

I'm trying to fine-tune my subplot by making a shared color bar using the high-level plotting functions of Xarrays. At the moment my plot looks enter image description here and I want to make a common color bar and fix the yaxis so that only 1st subplot y-axis has ticks and labels. The code I used to plot this looks like this:

fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, figsize=(14,10))
Var1_monthly_clim_N.plot(ax=ax1,vmin=6, vmax=14, cmap='jet');

Var2_monthly_clim_S.plot(ax=ax2, vmin=6, vmax=14, cmap='jet', yticks=[]);
## why is yticks removing ticks from both axis
## Var1 and Var 2 have similiar dimensions
Var1
>><xarray.DataArray 'R_metric' (month: 12, lat: 1, lon: 720)>
array([[[ 9.899495,  9.942897, ...,  9.814826,  9.856807]],

   [[ 9.578215,  9.610594, ...,  9.514423,  9.546191]],

   ...,

   [[11.974784, 12.019565, ..., 11.886591, 11.930419]],

   [[10.237672, 10.285891, ..., 10.142638, 10.189836]]])
Coordinates:
* lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 ... 178.5 179.0 179.5
* month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: lat

I'm not the most proficient with python plotting and I tried to use this idea of plotting using imshow to try

img1 = Var1.data
img2 = Var2.data 
plt.subplot(1,2,1)
plt.imshow(img1, vmin=6, vmax=14, cmap='jet', aspect='auto')
plt.subplot(1, 2, 2)
plt.imshow(img2, vmin=6, vmax=14, cmap='jet', aspect='auto')
plt.colorbar()
>>TypeError: Invalid dimensions for image data

How can I do this quickly using the highlevel plotting functions of xarray, that is, not extracting the data into numpy arrays and making meshgrid and all for the plot?

Upvotes: 5

Views: 11498

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40737

I've never used xarray, so I may be wrong, but, from looking at the documentation, it looks like you could pass add_colorbar=False to you first plot() call to suppress the creation of one of the colorbar.

As for your ylabel problem, you have requested to have a shared y-axis, so if you remove the labels from one, you also remove them from the other. Interestingly, plt.subplots(..., sharey=True) should already have set up things so that only the leftmost graph(s) have y-labels, and their hidden in the other Axes. If xarray's plot() is messing with that, then your best bet is to remove sharey=True and ensure that the limits are the same by hand (using ax2.set_ylim(ax1.get_ylim() for instance.)

Upvotes: 24

Related Questions