Аннотирующие подзаговоры в matplotlib масштабируют фигуру до самых больших осей - PullRequest
3 голосов
/ 07 апреля 2020

Когда я делаю фигуру с 5 субплотами и комментирую столбцы в каждом субплоте, появляется matplotlib, чтобы масштабировать фигуру так, чтобы максимум от самой большой оси y масштабировался до самой маленькой оси y.

Я могу Не слишком хорошо описывайте проблему, но посмотрите на это изображение:

image

where there's tons of white-space above where the figure should begin.

However, the figure would ideally look like this

this

Когда я установил 4 наименьших оси таким же верхним пределом y, что и наибольшая ось, тогда фигура масштабируется правильно, но для целей визуализации я бы предпочел не делать этого.

Почему это происходит? Есть ли способ контролировать фигуру, чтобы она не масштабировалась автоматически, как на первом изображении? Или иначе, более подходящий способ построить то, что я надеюсь достичь?

Код, который я использую для генерации рисунка:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Patch
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Arial']
department = ["100", "1,000", "10,000", \
              "100,000", "1,000,000"]
quarter = ["Serial", "MPI", "CUDA", "Hybrid"]
budgets = np.array([[0.049979, 0.43584,  2.787366, 19.75062, 201.6935],\
                    [2.184624, 0.175213, 0.677837, 5.265575, 46.33678],\
                    [0.050294, 0.068537, 0.23739,  1.93778,  18.55734],\
                    [3.714284, 3.9917,   4.977599, 6.174967, 37.732232]])

budgets = np.transpose(budgets)
em = np.zeros((len(department), len(quarter)))

# set up barchart
x = np.arange(len(department)) # label locations
width = 0.8    # width of all the bars

# set up figure
fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5)
axes = [ax1, ax2, ax3, ax4, ax5]

# generate bars
rects = []
color = ["tomato", "royalblue", "limegreen", "orange"]
n = len(quarter)
for i in range(n):
    bar_x = x - width/2.0 + i/float(n)*width + width/(n*2)

    m = len(budgets[:,i])
    for j in range(m):
        bar_x = x[j] - width/2.0 + i/float(n)*width + width/(n*2)
        e = budgets[j,i]
        #bar_x = x - width/2.0 + i/float(n)*width + width/(n*2)
        rects.append(axes[j].bar(bar_x, e, width=width/float(n), \
                label=quarter[i], color=color[i]))

# set figure properties
fig.set_size_inches(12, 2.5)
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
nAx = len(axes)
for i in range(nAx):
    #axes[i].set_aspect("auto")
    axes[i].tick_params(axis='x', which='both', bottom=False, top=False, 
                        labelbottom=False)

ax1.set_ylabel("Time (ms)")
for i in range(nAx):
    axes[i].yaxis.grid(which="major", color="white", lw=0.75)
ax1.set_ylim([0, 4])

fig.suptitle("Time per iteration for differing dataset sizes")   # title

for i in range(nAx):
    axes[i].set_xlabel(department[i])

# annotate bars
for i in range(nAx):
    for rect in rects:
        j = 0;
        for bar in rect:
            y_bottom, y_top = axes[i].get_ylim() # axis limits

            height = bar.get_height() # bar's height

            va = 'bottom'
            offset = 3
            color = 'k'
            fg = 'w'

            # keep label within plot
            if (y_top < 1.1 * height):
                offset = -3
                va = 'top'
                color='w'
                fg = 'k'

            # annotate the bar
            axes[i].annotate('{:.2f}'.format(height),
                              xy=(bar.get_x() + bar.get_width()/2, height),
                              xytext=(0,offset),
                              textcoords="offset points",
                              ha='center', va=va, color=color)


# set custom legend
legend_elements = [Patch(facecolor='tomato', label='Serial'),
                   Patch(facecolor='royalblue', label='MPI'),
                   Patch(facecolor='limegreen', label='CUDA'),
                   Patch(facecolor='orange', label='Hybrid')]
plt.legend(handles=legend_elements, loc="upper center", fancybox=False, 
           edgecolor='k', ncol=4, bbox_to_anchor=(-2, -0.1))

plt.show()

Ответы [ 2 ]

1 голос
/ 07 апреля 2020

Это частичный ответ.

Это может быть ошибкой, поскольку я не мог воспроизвести проблему, пока не переключился на ноутбук Jupyter в системе Debian (также на другом оборудовании). Ваша фигура правильно отображается в моей записной книжке macOS Jupyter и в Debian при отображении из сценария .py.

Проблема, по-видимому, связана с вашими аннотациями. Если вы сделаете вызов tight_layout после аннотации, вы можете получить следующее предупреждение:

<ipython-input-80-f9f592f5efc5>:88: UserWarning: Tight layout not applied. The bottom and top margins cannot be made large enough to accommodate all axes decorations. 
  fig.tight_layout(rect=[0, 0.03, 1, 0.95])

Кажется, что функция annotate вычисляет некоторые совершенно дурацкие координаты для ваших аннотаций, хотя текст заканчивается в правильном месте. Если вы удалите их, пустое пространство исчезнет. Вы можете попытаться вычислить xy координаты a для ваших аннотаций другим способом . Это может помочь вам начать:

        axes[i].annotate('{:.2f}'.format(height),
                          xy=(bar.get_x() + bar.get_width()/2, height),
                          xytext=(0,offset),
                          textcoords="offset points",
                          xycoords="axes points", # change
                          ha='center', va=va, color=color)

Вывод:

enter image description here

Чтобы правильно рассчитать баллы, вы можете попробовать использовать соответствующие ось преобразование , хотя, опять же, я не смог заставить его работать, и это может быть связано с ошибкой.

0 голосов
/ 07 апреля 2020

попробуйте поставить fig.tight_layout(rect=[0, 0.03, 1, 0.95]) после всех команд построения, как показано ниже.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Patch
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Arial']
department = ["100", "1,000", "10,000", \
              "100,000", "1,000,000"]
quarter = ["Serial", "MPI", "CUDA", "Hybrid"]
budgets = np.array([[0.049979, 0.43584,  2.787366, 19.75062, 201.6935],\
                    [2.184624, 0.175213, 0.677837, 5.265575, 46.33678],\
                    [0.050294, 0.068537, 0.23739,  1.93778,  18.55734],\
                    [3.714284, 3.9917,   4.977599, 6.174967, 37.732232]])

budgets = np.transpose(budgets)
em = np.zeros((len(department), len(quarter)))

# set up barchart
x = np.arange(len(department)) # label locations
width = 0.8    # width of all the bars

# set up figure
fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5)
axes = [ax1, ax2, ax3, ax4, ax5]

# generate bars
rects = []
color = ["tomato", "royalblue", "limegreen", "orange"]
n = len(quarter)
for i in range(n):
    bar_x = x - width/2.0 + i/float(n)*width + width/(n*2)

    m = len(budgets[:,i])
    for j in range(m):
        bar_x = x[j] - width/2.0 + i/float(n)*width + width/(n*2)
        e = budgets[j,i]
        #bar_x = x - width/2.0 + i/float(n)*width + width/(n*2)
        rects.append(axes[j].bar(bar_x, e, width=width/float(n), \
                label=quarter[i], color=color[i]))

# set figure properties
fig.set_size_inches(12, 2.5)
#fig.tight_layout(rect=[0, 0.03, 1, 0.95])
nAx = len(axes)
for i in range(nAx):
    #axes[i].set_aspect("auto")
    axes[i].tick_params(axis='x', which='both', bottom=False, top=False, 
                        labelbottom=False)

ax1.set_ylabel("Time (ms)")
for i in range(nAx):
    axes[i].yaxis.grid(which="major", color="white", lw=0.75)
ax1.set_ylim([0, 4])

fig.suptitle("Time per iteration for differing dataset sizes")   # title

for i in range(nAx):
    axes[i].set_xlabel(department[i])

# annotate bars
for i in range(nAx):
    for rect in rects:
        j = 0;
        for bar in rect:
            y_bottom, y_top = axes[i].get_ylim() # axis limits

            height = bar.get_height() # bar's height

            va = 'bottom'
            offset = 3
            color = 'k'
            fg = 'w'

            # keep label within plot
            if (y_top < 1.1 * height):
                offset = -3
                va = 'top'
                color='w'
                fg = 'k'

            # annotate the bar
            axes[i].annotate('{:.2f}'.format(height),
                              xy=(bar.get_x() + bar.get_width()/2, height),
                              xytext=(0,offset),
                              textcoords="offset points",
                              ha='center', va=va, color=color)


# set custom legend
legend_elements = [Patch(facecolor='tomato', label='Serial'),
                   Patch(facecolor='royalblue', label='MPI'),
                   Patch(facecolor='limegreen', label='CUDA'),
                   Patch(facecolor='orange', label='Hybrid')]
plt.legend(handles=legend_elements, loc="upper center", fancybox=False, 
           edgecolor='k', ncol=4, bbox_to_anchor=(-2, -0.1))

fig.tight_layout(rect=[0, 0.03, 1, 0.95])

plt.show()
...