0

I am dealing a CDF4 data using xarray to open it. I want to plot a line and a bar in just one figure, so I used twinx(). The problem is the xticks of the line and bar plot are not matched to each other. The xticks of the line plot is shifted one unit (one month) to the right when compared to those of the bar plot.

ERA5_land = xr.open('Era5landmon2001_2021nc.sec')

prec = ERA5_land.tp.groupby('time.month').sum().sel(longitude = 8.4, latitude = 49) #calculate the sum of precipitation at (49, 8.4)
temp = ERA5_land.t2m.groupby('time.month').mean().sel(longitude = 8.4, latitude = 49) #calculate the mean of temperature at (49, 8.4)

f,ax1 = plt.subplots()
prec.to_series().plot.bar(ax = ax1,)

ax2 = ax1.twinx()
temp.plot( ax = ax2)

The data can be downloaded from here: ERA5-Land hourly data from 1950 to present. The variables are '2m temperature' and 'total precipitation', measured from 2001 to 2021. All the 12 months in a year were taken into account.

My result: Time shift of the line plot

I tried to set ax2.set_xticks(ticks = ax1.get_xticks() and ax2.set_xticks(ticks = range(1,13) but nothing was success.

f,ax1 = plt.subplots()
prec.to_series().plot.bar(ax = ax1)

ax2 = ax1.twinx()
a2.set_xticks(ticks = ax1.get_xticks())
temp.plot( ax = ax2)

I also know that the xticks of the two plots are different, but I don't know to do match them.

0 Answers0