Colorbar для sns.jointplot "kde" -стиль на стороне - PullRequest
1 голос
/ 25 марта 2020

Я пытаюсь построить цветную полосу рядом с моим графиком плотности с краевыми осями. Это действительно наносит цветную полосу, но, к сожалению, не на стороне. Вот что попробовал до сих пор:

sns.jointplot(x,y, data=df3, kind="kde", color="skyblue", legend=True, cbar=True,
              xlim=[-10,40], ylim=[900,1040])

Это выглядит так:

seaborn jointplot

Я тоже пробовал это:

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np

kdeplot = sns.jointplot(x=tumg, y=pumg, kind="kde")
plt.subplots_adjust(left=0.2, right=0.8, top=0.8, bottom=0.2)
cbar_ax = kdeplot.fig.add_axes([.85, .25, .05, .4])
plt.colorbar(cax=cbar_ax)
plt.show()

Но со вторым вариантом я получаю ошибку во время выполнения:

No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

У кого-нибудь есть идеи, как решить проблему?

1 Ответ

0 голосов
/ 25 марта 2020

Кажется, что информация только для цветовой панели существует только при эффективном создании цветовой панели. Итак, идея состоит в том, чтобы объединить оба подхода: добавить цветовую панель с помощью kdeplot, а затем переместить ее в нужное место. Это оставит основной объединенный участок с недостаточной шириной, поэтому его ширина также должна быть адаптирована:

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np

# create some dummy data: gaussian multivariate with 10 centers with each 1000 points
tumg = np.random.normal(np.tile(np.random.uniform(10, 20, 10), 1000), 2)
pumg = np.random.normal(np.tile(np.random.uniform(10, 20, 10), 1000), 2)

kdeplot = sns.jointplot(x=tumg, y=pumg, kind="kde", cbar=True)
plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)
# get the current positions of the joint ax and the ax for the marginal x
pos_joint_ax = kdeplot.ax_joint.get_position()
pos_marg_x_ax = kdeplot.ax_marg_x.get_position()
# reposition the joint ax so it has the same width as the marginal x ax
kdeplot.ax_joint.set_position([pos_joint_ax.x0, pos_joint_ax.y0, pos_marg_x_ax.width, pos_joint_ax.height])
# reposition the colorbar using new x positions and y positions of the joint ax
kdeplot.fig.axes[-1].set_position([.83, pos_joint_ax.y0, .07, pos_joint_ax.height])
plt.show()

resulting plot

...