Как центрировать общие y-метки для двух горизонтальных столбцов? - PullRequest
1 голос
/ 06 апреля 2020

У меня небольшая проблема, и Google не может мне помочь. Вот мой код:

from matplotlib import pyplot as plt
import numpy as np

job_r = list(ct_t.JobRole.unique())
att_y = ct_t[ct_t['Attrition']=='Yes']['Percentage'].values
att_n = ct_t[ct_t['Attrition']=='No']['Percentage'].values

# Sort by number of sales staff
idx = att_n.argsort()
job_r, att_y, att_n = [np.take(x, idx) for x in [job_r, att_y, att_n]]
y = np.arange(att_y.size)

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=[8,8])
axes[0].barh(y, att_n, align='center', color='#43e653', zorder=10)
axes[0].set(title='NO')

axes[1].barh(y, att_y, align='center', color='#ed1c3c', zorder=10)
axes[1].set(title='YES')

axes[0].invert_xaxis()
axes[0].set(yticks=y, yticklabels=job_r)
axes[0].yaxis.tick_right()

for ax in axes.flat:
    ax.margins(0.03)
    ax.grid(True)

fig.tight_layout()
fig.subplots_adjust(wspace=0.7)
plt.show()

Мой текущий вывод:

enter image description here

  1. Есть ли способ центрирования общие метки y посередине между этими двумя подсюжетами?

  2. Можно ли увеличить ось х до 1,0? Каждый раз, когда я делаю что-то вроде: axes[1].set_xlabels(1.0) весь мой сюжет переворачивается вверх ногами.

1 Ответ

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

Вот способ достижения желаемого графика.

Увеличение пределов x до 1 проще, если это сделать до инверсии оси x. Код для центрирования и изменения положения меток взят из этого поста .

import matplotlib.pyplot as plt
import matplotlib.transforms
import numpy as np

# first create some test data compatible with the question's data
job_r = ["".join(np.repeat(letter, np.random.randint(4, 15))) for letter in 'ABCDEFG']
att_y = np.random.uniform(0.5, 0.9, len(job_r))
att_n = 1 - att_y

# Sort by number of sales staff
idx = att_n.argsort()
job_r, att_y, att_n = [np.take(x, idx) for x in [job_r, att_y, att_n]]
y = np.arange(att_y.size)

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=[8, 8])
axes[0].barh(y, att_n, align='center', color='#43e653', zorder=10)
axes[0].set(title='NO')

axes[1].barh(y, att_y, align='center', color='#ed1c3c', zorder=10)
axes[1].set(title='YES')
axes[1].set_xlim(xmax=1)

axes[0].set(yticks=y, yticklabels=job_r)
axes[0].yaxis.tick_right()
axes[0].set_xlim(xmax=1)
axes[0].invert_xaxis()

for ax in axes:
    ax.margins(0.03)
    ax.grid(True)

fig.tight_layout()
fig.subplots_adjust(wspace=0.7)

plt.setp(axes[0].yaxis.get_majorticklabels(), ha='center')

# Create offset transform by some points in x direction
dx = 60 / 72.
dy = 0 / 72.
offset = matplotlib.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)
# apply offset transform to all y ticklabels.
for label in axes[0].yaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)

plt.show()

resulting plot

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...