Python 颜色作为seaborn JointPlot中的第三个轴
我有三个量,Python 颜色作为seaborn JointPlot中的第三个轴,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有三个量,x,y,z,我希望看到其中两个的分布,并将第三个的值作为每个点的颜色。我使用seaborn.jointplot绘制x和y的分布,并使用z作为jointplot中的颜色参数。但是,我仍然需要更改调色板,并在绘图之外的某处添加一个颜色栏。这是我做过的最简单的例子: import matplotlib.pyplot as plt import seaborn as sns import numpy as np x = np.random.normal(loc=0, scale=1, si
x,y,z
,我希望看到其中两个的分布,并将第三个的值作为每个点的颜色。我使用seaborn.jointplot
绘制x
和y
的分布,并使用z
作为jointplot
中的颜色参数。但是,我仍然需要更改调色板,并在绘图之外的某处添加一个颜色栏。这是我做过的最简单的例子:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
x = np.random.normal(loc=0, scale=1, size=1000)
y = np.random.normal(loc=0, scale=1, size=1000)
z = x**2 + y**2
plt.figure(num=1, figsize=(8, 5))
sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None})
plt.show()
提前感谢您的评论。所谓“更改调色板”,是指更改颜色贴图吗?如果是这样,只需将相关信息传递给joint_kws
:
g=sns.jointplot(x=x,y=y,c=z,joint_-kws={“color”:无,'cmap':'viridis'})
对于颜色条,这完全取决于您想要它的位置。这里有一个解决方案,可以自动调整轴的大小以腾出空间。请参阅以根据需要调整参数:
g.fig.colorbar(g.ax_joint.collections[0],ax=[g.ax_joint,g.ax_marg_y,g.ax_marg_x],使用_gridspec=True,方向为水平)
完整代码:
plt.figure(num=1, figsize=(8, 5))
g = sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None, 'cmap':'viridis'})
g.fig.colorbar(g.ax_joint.collections[0], ax=[g.ax_joint, g.ax_marg_y, g.ax_marg_x], use_gridspec=True, orientation='horizontal')
plt.show()
所谓“更改调色板”,是指更改颜色贴图吗?如果是这样,只需将相关信息传递给joint_kws
:
g=sns.jointplot(x=x,y=y,c=z,joint_-kws={“color”:无,'cmap':'viridis'})
对于颜色条,这完全取决于您想要它的位置。这里有一个解决方案,可以自动调整轴的大小以腾出空间。请参阅以根据需要调整参数:
g.fig.colorbar(g.ax_joint.collections[0],ax=[g.ax_joint,g.ax_marg_y,g.ax_marg_x],使用_gridspec=True,方向为水平)
完整代码:
plt.figure(num=1, figsize=(8, 5))
g = sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None, 'cmap':'viridis'})
g.fig.colorbar(g.ax_joint.collections[0], ax=[g.ax_joint, g.ax_marg_y, g.ax_marg_x], use_gridspec=True, orientation='horizontal')
plt.show()