首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >侧面为sns.jointplot "kde"-style的彩色条

侧面为sns.jointplot "kde"-style的彩色条
EN

Stack Overflow用户
提问于 2020-03-25 09:09:59
回答 1查看 4.3K关注 0票数 5

我试着用边缘轴在密度图旁边画一个色条。它确实画出了彩色条,但不幸的是,它没有画在一边。到目前为止,这是一个尝试过的:

代码语言:javascript
复制
sns.jointplot(x,y, data=df3, kind="kde", color="skyblue", legend=True, cbar=True,
              xlim=[-10,40], ylim=[900,1040])

看起来是这样的:

我也试过这个:

代码语言:javascript
复制
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).

有谁知道如何解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-03-25 12:39:55

当有效地创建色条时,似乎只有颜色条的信息。因此,一种方法是将这两种方法结合起来:通过kdeplot添加一个颜色条,然后将其移动到所需的位置。这将使主联合地块的宽度不足,因此其宽度也应加以调整:

代码语言:javascript
复制
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()

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60845764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档