Есть ли лучший способ настроить фигуру с разными столбцами в строке, которые имеют разную высоту? - PullRequest
0 голосов
/ 21 мая 2018

Как показано ниже, gridspec можно использовать для создания сетки строк и столбцов с различной высотой (используя аргумент height_ratios).Чтобы затем получить различное количество столбцов, необходимое для данной строки (иначе, чтобы установить экстент подплота), индексы столбцов в существующей сетке предоставляются в plt.subplot().Этот код работает нормально, но мне интересно, есть ли более лаконичный / менее "клуджий" способ использования gridspec или другого модуля в целом?

# Get modules in use
import seaborn as sns; sns.set(color_codes=True)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# Setup some data to use
x = range(40)
y = np.random.rand(len(x))
# Set heights of rows of subpolts
height_ratios = [1.5, 1, 1.5, 1.25]
# Initialise a figure
fig = plt.figure( )
# Set number of "fake" columns & actual rows
ncols = 1000
nrows = 4
ncols4row = [1,2,2,3]
# Set the # of columns to spread subplots across (just set here for Q brevity)
cols2use = {
2: ( (0, 450), (550, -1) ),
3: ( (0, 283), (383, 616), (716, -1)  ),
}
# Use grid specs to setup plot with nrows with set "height ratios"
G = gridspec.GridSpec(nrows, ncols, height_ratios=height_ratios )
# Now loop rows
for nrow in range( nrows ):
    # Number of columns in row?
    ncol = ncols4row[nrow]
    # Get axes for row based on number of rows
    if ncol == 1 :
         axes = [ plt.subplot(G[nrow, :]) ]
    else:
         axes = [ plt.subplot(G[nrow, i[0]:i[-1]]) for i in cols2use[ncol] ]
    # Loop axes and plot up
    for ax in axes:
        ax.plot(x,y)

Что дает:

enter image description here

...