可以自动重新调整seaborn(distplot)yaxis吗?

时间:2017-11-17 14:27:28

标签: python matplotlib plot seaborn

是否可以将Seaborn distplot设置为自动重新缩放y轴以捕获多个绘制数据集的最大y范围?

当使用Seaborn进行批量绘图时,有时不可避免地提供数据而不增加最大频率值。当发生这种情况时,创建的绘图yaxis会切断数据。但是,如果数据的最大值增加,sns.distplot()就可以了。

这可以通过Matplotlib.patches修正,或者只需拨打ax.autoscale()(感谢@ImportanceOfBeingErnest以获取后续建议)即可修复,但要么是“kludgey”......

简单的例子:

# Import modules
import numpy as np
from scipy.stats import truncnorm
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)

# Make some random data (credit: [@bakkal's Answer][3])
scale = 3.
range = 10
size = 100000
X = truncnorm(a=-range/scale, b=+range/scale, scale=scale).rvs(size=size)

# --- first time to show issue
# Now plot up 1st set of data (with first dataset having high Y values)
ax= sns.distplot( X*4 )
# Now plot up two more
for i in np.arange(1, 3):
    sns.distplot( X*i, ax=ax )
plt.show()

 (Issue!)

# --- Second time with a "kludgey" fix
ax = sns.distplot( X*4 )
# Now plot up two more
for i in np.arange(1, 3):
    sns.distplot( X*i, ax=ax )
# Now force y axis extent to be correct
ax.autoscale()
plt.show()

(Success!)

# --- Third time with increasing max in data provided 
ax= sns.distplot( X )
# Now plot up two more
for i in np.arange(2, 4 ):
    sns.distplot( X*i, ax=ax )
plt.show()

(success too)

1 个答案:

答案 0 :(得分:2)

这是Seaborn(0.8.0)中的一个问题,修正已在github上提交(感谢@mwaskom)。

如果您还看到此问题,请更新Seaborn(> = version 0.8.1)。