简体   繁体   中英

Matplotlib share x-axis between imshow and plot

I am trying to plot two imshow and one plot above each other sharing their x-axis. The figure layout is set up using gridspec. Here is a MWE:

    import matplotlib as mpl
    from matplotlib import pyplot as plt
    import numpy as np
    fig = plt.figure(figsize=(10,8))
    gs = fig.add_gridspec(3,2,width_ratios=(1,2),height_ratios=(1,2,2), left=0.1,right=0.9,bottom=0.1,top=0.99, wspace=0.1, hspace=0.1)
    ax=fig.add_subplot(gs[2,1])
    ax2=fig.add_subplot(gs[2,0], sharey=ax)
    ax3=fig.add_subplot(gs[1,0])
    ax4=fig.add_subplot(gs[1,1], sharex=ax, sharey=ax3)
    ax5=fig.add_subplot(gs[0,1], sharex=ax)
    
    dates = pd.date_range("2020-01-01","2020-01-10 23:00", freq="H")
    xs = mpl.dates.date2num(dates)
    ys = np.random.random(xs.size)
    N = 10
    arr = np.random.random((N, N))
    arr2 = np.random.random((N, N))
    
    norm=mpl.colors.Normalize(0, arr.max()) # change the min to stretch the color spectrum
    pcm = ax.imshow(arr, extent=[xs[0],xs[-1],10,0],norm=norm,aspect='auto')
    cax = fig.colorbar(pcm, ax=ax, extend='max') # , location='left'
    ax.set_xlabel('date')
    cax.set_label('fraction [-]')
    # ax.xaxis_date()
    myFmt = mpl.dates.DateFormatter('%d.%m')
    ax.xaxis.set_major_formatter(myFmt)
    
    norm=mpl.colors.Normalize(0, arr2.max()) # change the min to stretch the color spectrum
    pcm = ax4.imshow(arr2, extent=[xs[0],xs[-1],1,0],norm=norm,aspect='auto')
    cax4 = fig.colorbar(pcm, ax=ax4, extend='max')
    cax4.set_label('fraction [-]')
    
    ax5.plot(xs,ys)

    con1 = ConnectionPatch(xyA=(ax2.get_xlim()[0],1), xyB=(ax2.get_xlim()[0],1), 
                       coordsA="data", coordsB="data", connectionstyle=mpl.patches.ConnectionStyle("Bar", fraction=-0.05),
                       axesA=ax2, axesB=ax3, arrowstyle="-", color='r')
    
    con2 = ConnectionPatch(xyA=(ax2.get_xlim()[0],0), xyB=(ax2.get_xlim()[0],0), 
                       coordsA="data", coordsB="data", connectionstyle=mpl.patches.ConnectionStyle("Bar", fraction=-0.02),
                       axesA=ax2, axesB=ax3, arrowstyle="-", color='r')
    
    fig.add_artist(con1)
    fig.add_artist(con2)

The plot ends up like this: 阴谋

While the axes seem to be linked (date format applied to all of them), they do not have the same extent.

NOTE: The two left axes must not share the same x-axis.

EDIT: Added ConnectionPatch connections which break when using constrained_layout.

Constrained_layout was specifically designed with this case in mind. It will work with your gridspec solution above, but more idiomatically:

import datetime as dt
import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

fig, axs = plt.subplot_mosaic([['.', 'plot'], ['empty1', 'imtop'],
                               ['empty2', 'imbottom']],
                              constrained_layout=True,
                              gridspec_kw={'width_ratios':(1,2),'height_ratios':(1,2,2)})

axs['imtop'].sharex(axs['imbottom'])
axs['plot'].sharex(axs['imtop'])

dates = pd.date_range("2020-01-01","2020-01-10 23:00", freq="H")
xs = mpl.dates.date2num(dates)
ys = np.random.random(xs.size)
N = 10
arr = np.random.random((N, N))
arr2 = np.random.random((N, N))

norm=mpl.colors.Normalize(0, arr.max()) # change the min to stretch the color spectrum
pcm = axs['imtop'].imshow(arr, extent=[xs[0],xs[-1],10,0],norm=norm,aspect='auto')
cax = fig.colorbar(pcm, ax=axs['imtop'], extend='max')

norm=mpl.colors.Normalize(0, arr2.max()) # change the min to stretch the color spectrum
pcm = axs['imbottom'].imshow(arr2, extent=[xs[0],xs[-1],1,0],norm=norm,aspect='auto')
cax4 = fig.colorbar(pcm, ax=axs['imbottom'], extend='max')

axs['plot'].plot(xs,ys)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM