Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

python - How to plot non-square Seaborn jointplot or JointGrid

I am trying to plot my non-symmetric data using Seaborn's JointGrid. I can get it to use an equal aspect ratio, but then I have unwanted whitespace:

Seaborn 2D heatmap jointplot with extra white space

How do you remove the padding? The documentation for both jointplot and JointGrid simply say

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.

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() 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...

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:

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

So to get a non-square JointGrid plot, simply run:

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

for a 6x4 figure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...