沿多个matplotlib子地块添加外部轴

2024-10-02 16:32:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用matplotlib生成了子地块的网格。每个子批次都有变量“a”和“B”的不同值组合。它们的排列方式是,沿栅格的x轴移动时,A的值会增加,y轴和B的值也会增加。 我想添加包含整个网格的外部轴,并可视化这两个数量的增加。 以下是我当前使用的代码的摘要版本:

#--OS
import sys,os
import copy
import glob

#--math and datasets
import numpy as np
import pandas as pd

#--matplotlib
%matplotlib inline
import matplotlib
from matplotlib.ticker import MultipleLocator
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.lines import Line2D
from matplotlib import ticker
from matplotlib import cm
import colorcet as cc
import holoviews as hv
from colorcet.plotting import swatch, swatches

ABinBounds= pd.DataFrame.from_dict({'max' : [2.3713730200033902,3.1622776601683795,4.216965733794858,5.623415332340302,7.4989399250827455, 
                          10.0,13.335216533675034,17.782800679308078, 23.713730200033908,31.622776601683796,100.0], 
                 'min': [1.7782800679308082, 2.3713730200033902, 3.1622776601683795, 4.216965733794858, 5.623415332340302, 7.4989399250827455,
                         10.0, 13.335216533675034, 17.782800679308078, 23.713730200033908, 31.622776601683796] } )

BBinBounds = pd.DataFrame.from_dict({'max': [0.001, 0.00158489, 0.00251189, 0.00398107, 0.00630957, 0.01, 0.0158489, 
                          0.025118900000000003, 0.0398107, 0.0630957, 0.1, 0.15848900000000002, 0.251189, 0.398107, 0.630957, 1.0],
                  'min': [0.000630957, 0.001, 0.00158489, 0.00251189, 0.00398107, 0.00630957, 0.01, 0.0158489, 0.025118900000000003,
                          0.0398107, 0.0630957, 0.1, 0.15848900000000002, 0.251189, 0.398107, 0.630957] })
cmaprai=cc.cm.rainbow

nQ = len(BBinBounds)
nx = len(ABinBounds)
fig, axs = plt.subplots(nQ-1,nx,figsize = (40*1.6,40), sharex=True,sharey=True )
for i in range(len(ABinBounds)):
    for j in range(len(BBinBounds) - 1 ):   
        jp = -(j+1); ip = i
        ax = axs[jp,ip] #axs[(nQ-1-1)-j,i]

        ax.text(0.1,0.75, '<B> = {:.2e}'.format((BBinBounds['max'][j]-BBinBounds['min'][j])/2),transform=ax.transAxes,fontsize=32 )
        ax.text(0.1,0.4, '<A> = {:.4f}'.format((ABinBounds['max'][i]-ABinBounds['min'][i])/2),transform=ax.transAxes,fontsize=32 )

fig.subplots_adjust(right=0.9)
cbar_ax = fig.add_axes([0.95, 0.15, 0.02, 0.4])
norm = matplotlib.colors.Normalize(vmin=0, vmax=1)
cb = fig.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=cmaprai),cax=cbar_ax)
cb.ax.tick_params(labelsize=25)  # set your label size here

plt.subplots_adjust(wspace=0, hspace=0)

plt.show()

这将生成以下图像:

enter image description here

我想对其进行修改,使其看起来如下(y轴和x轴上的每个间隔应对应于子地块网格的一行或一列):

enter image description here

目前,我已经尝试使用inset来实现这一点,但没有多大成功


Tags: fromimport网格lenmatplotlibasfigplt