Добавить проценты к гистограммам в JointGrid - PullRequest
0 голосов
/ 27 февраля 2020

Можно ли добавить проценты к гистограммам JointGrid?

Я мог бы жить с процентами ниже, в или выше столбцов. Или с дополнительной осью, показывающей проценты + линии. Я просмотрел исходный код JointGrid, чтобы найти способ вернуть скрытую ось обеих гистограмм, чтобы я мог добавить проценты к ним.

Я также пробовал разные метки для осей гистограммы, но это не так. т работают, как они делятся:

hex.ax_marg_y.set_yticklabels(["test", "label"])

Это моя работа в процессе:

labels = ['Ma 13', 'Di 14', 'Wo 15', 'Do 16', 'Vr 17', 'Za 18', 'Zo 19', 'Ma 20']

hex = sns.JointGrid(x, y, height = 12)
hex.ax_marg_x.hist(x, bins=np.arange(-0.5, 23.5))
hex.ax_marg_y.hist(y, bins=np.arange(-0.5, len(labels) + 0.5), orientation="horizontal")
hex.plot_joint(sns.kdeplot, shade=True, cmap="Blues", bw=.11)

plt.ylim(-0.5, len(labels)-0.5)
plt.xlim(-0.5, 23.5)

hex.ax_joint.set_xticks(range(24))
hex.ax_joint.set_yticks(range(len(labels)))
hex.ax_joint.set_yticklabels(labels)

plt.subplots_adjust(left=0.05, right=0.95, top=0.93, bottom=0)  # Shrink figure so the legende is visible

hex.x = x2
hex.y = y2
hex.plot_joint(plt.scatter, marker = 'x', c = 'r', s = 190)

plt.show()

Изображение JointGrid

1 Ответ

0 голосов
/ 28 февраля 2020

Если вы позаботитесь о сохранении ссылки на артистов, возвращенных ax_marg_{x|y}.hist(), тогда вам просто нужно перебрать каждого из них и сделать пометки в нужном месте:

tips = sns.load_dataset("tips")
x = tips['total_bill']
y = tips['tip']
hex = sns.JointGrid(x, y)
_,_,bars_x = hex.ax_marg_x.hist(x)
_,_,bars_y = hex.ax_marg_y.hist(y, orientation="horizontal")
hex.plot_joint(sns.kdeplot, shade=True, cmap="Blues")

# annotate top hist
total_height = np.sum([bar.get_height() for bar in bars_x])
for bar in bars_x:
    height = bar.get_height()
    hex.ax_marg_x.annotate('{:.1f}%'.format(100*height/total_height),
                    xy=(bar.get_x() + bar.get_width() / 2, height),
                    xytext=(0, 1),  # 1 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom', fontsize=8)

# annotate right hist
total_height = np.sum([bar.get_width() for bar in bars_y])
for bar in bars_y:
    height = bar.get_width()
    hex.ax_marg_y.annotate('{:.1f}%'.format(100*height/total_height),
                    xy=(height, bar.get_y(), ),
                    xytext=(1, 10),  # 1 points vertical offset
                    textcoords="offset points",
                    ha='left', va='center', fontsize=8)

enter image description here

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