0
votes

I am using the Pylab (pl) subplots function as follows:

f, (ax1, ax2, ax3, ax4) = pl.subplots(4, sharex=True, sharey=False)

Let's say that t0, t1, t2, and t3 are four timestamps in chronological order. Axes objects ax1 and ax2 generate subplots of precipitation and air temperature data between timestamps t0 and t2 with a total of 100 data points. Axes objects ax3 and ax4 generate subplots of relative humidity and wind data between the timestamps t1 and t3 with a total of 500 data points.

Both time arrays are converted to decimal numbers using:

import matplotlib.dates as dates
time_numbers1 = dates.datestr2num(time_array1)
time_numbers2 = dates.datestr2num(time_array2)

The issue that I am encountering is no surprise: When I run my code, the generated figure displays ax1 and ax2 data that are distorted and not in line with the data in ax3 and ax4.

I know that my issue is tied to my oversimplified use of this subplots setting:

sharex=True

Does anyone have recommendations/ideas for handling this plotting issue? To be clear, this is not a do or die situation, for I could always fiddle with the data (e.g., zero padding, interpolation, averaging) to get the data to line up. It is a lot of data, however, so I am curious if any clever plotting solutions might be out there.

1

1 Answers

1
votes

I'm afraid plt.subplots is your doing too many things that you do not know. It works great if you want simple figures, but if you want to do something slightly advanced it is usually better to create your own figure and add the subplots manually. I.e.

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(4, 1, 1)
ax2 = fig.add_subplot(4, 1, 2, sharex=ax1)
ax3 = fig.add_subplot(4, 1, 3)
ax4 = fig.add_subplot(4, 1, 4, sharex=ax3)

This should force that only two x-axes are shared, but in two pairs. In this case I would say that instead of stacking them it might be better with a grid, as

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3, sharex=ax1)
ax4 = fig.add_subplot(2, 2, 4, sharex=ax3)

Hope it helps!