简体   繁体   English

如何绘制非方形 Seaborn Jointplot 或 JointGrid

[英]How to plot non-square Seaborn jointplot or JointGrid

I am trying to plot my non-symmetric data using Seaborn's JointGrid.我正在尝试使用 Seaborn 的 JointGrid 绘制我的非对称数据。 I can get it to use an equal aspect ratio, but then I have unwanted whitespace:我可以让它使用相等的纵横比,但是我有不需要的空格:

带有额外空白的 Seaborn 2D 热图联合图

How do you remove the padding?你如何去除填充物? The documentation for both jointplot and JointGrid simply say JointplotJointGrid的文档都简单地说

size : numeric, optional大小:数字,可选

Size of the figure (it will be square).图形的大小(它将是正方形)。

I also tried going into feeding the extent kwarg to both jointplot and JointGrid, as well as ylim with no luck.我还尝试将extent kwarg 提供给 Jointplot 和 JointGrid,以及没有运气的ylim

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x = np.random.normal(0.0, 10.0, 1000)
y = np.random.normal(0.0, 1.0, 1000)
joint = sns.jointplot(x, y)
joint.plot_marginals(sns.distplot, kde=False)
joint.ax_joint.set_aspect('equal')  # equal aspect ratio
plt.show() 

Stumbled upon this question looking for the answer myself.偶然发现了这个问题,正在寻找自己的答案。 Having figured it out I thought I'd post the solution.想通了之后,我想我会发布解决方案。 As the jointplot code seems quite insistent on having the figure square I don't know if this is considered bad practice, but anyhow...由于jointplot代码似乎非常坚持使用图形方块,我不知道这是否被认为是不好的做法,但无论如何......

If we look through the jointplot code and follow it into JointGrid , the size parameter to jointplot (and equally JointGrid ) is used in the following expression:如果我们查看jointplot代码并按照它进入JointGrid ,则在以下表达式中使用jointplot (以及同样JointGrid )的size参数:

f = plt.figure(figsize=(size, size))
# ... later on
self.fig = f

So to get a non-square JointGrid plot, simply run:因此,要获得非方形JointGrid图,只需运行:

grid = sns.jointplot(...)
grid.fig.set_figwidth(6)
grid.fig.set_figheight(4)
grid.savefig("filename.png", dpi=300)

for a 6x4 figure.对于 6x4 数字。

For those who use Seaborn into a Jupyter Notebook, I suggest calling set_figwidht() and set_figheight() just after the sns.jointplot() command.对于那些在 Jupyter Notebook 中使用 Seaborn 的人,我建议在sns.jointplot()命令之后调用set_figwidht()set_figheight()

my_plot=sns.jointplot(x="K",y="errori",data=risultati , kind="scatter")
my_plot.fig.set_figwidth(13)

Jupyter Example Jupyter 示例

You will need to set the ylim and xlim parameters, which will limit the x and y axis to the tuple ranges you specify:您需要设置ylimxlim参数,这会将 x 和 y 轴限制为您指定的元组范围:

eg例如

sns.jointplot(x="n_estimators", y="learning_rate", data=data,
              color="#172235", height=8, ratio=10, space=0,
              ylim=(0, 1.1), xlim=(-20, 310))  # <-- this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM