Question

I'm trying to add a subplot onto the bottom of a figure of subplots. The issue I've got is that all of the first set of subplots want to share their x axis, but not the bottom one.

channels are the subplots that want to share the x axis.

So how can I add a subplot that does not share the x axis? This is my code:

def plot(reader, tdata):
    '''function to plot the channels'''

channels=[]
        for i in reader:
        channels.append(i)

    fig, ax = plt.subplots(len(channels)+1, sharex=False, figsize=(30,16), squeeze=False)
    plot=0
    #where j is the channel name
    for i, j in enumerate(reader): 

        y=reader["%s" % j]
        ylim=np.ceil(np.nanmax(y))
        x=range(len((reader["%s" % j])))
        ax[plot,0].plot(y, lw=1, color='b')
        ax[plot,0].set_title("%s" % j)
        ax[plot,0].set_xlabel('Time / s')
        ax[plot,0].set_ylabel('%s' % units[i])
        ax[plot,0].set_ylim([np.nanmin(y), ylim+(ylim/100)*10])
        plot=plot+1

    ###here is the new subplot that doesn't want to share the x axis###
    ax[plot, 0].plot()
    plt.tight_layout()
    plt.show()

This code does NOT work because they are shareing the x-axis of the last subplot. The length of channels changes dependant onn what I specify earlier in the code.

Is it a valid option to use add_subplot somehow, ever though I don't have a constant number of channels?

thanks so much for any help

EDIT Image for Joe:

enter image description here

Was it helpful?

Solution

It's easiest to use fig.add_subplot directly in this case.

As a quick example:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(6, 8))

# Axes that share the x-axis
ax = fig.add_subplot(4, 1, 1)
axes = [ax] + [fig.add_subplot(4, 1, i, sharex=ax) for i in range(2, 4)]

# The bottom independent axes
axes.append(fig.add_subplot(4, 1, 4))

# Let's hide the tick labels for all but the last shared-x axes
for ax in axes[:2]:
    plt.setp(ax.get_xticklabels(), visible=False)

# And plot on  the first subplot just to demonstrate that the axes are shared
axes[0].plot(range(21), color='lightblue', lw=3)

plt.show()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top