我试图找到一个很好的方法来绘制同一轴上的两个distplots(来自seaborn) 。 它不像我想要的那样漂亮,因为直方图条是相互覆盖的。 而且我不想仅仅因为它们看起来不漂亮而使用countplot或barplot 。 当然,如果没有其他方式我会以这种方式做,但distplot看起来非常好。 但是,如上所述,这些酒吧现在相互覆盖(见图)。
因此有没有办法将两个distplot频率条拟合到一个bin上,以便它们不重叠? 或者把计数放在彼此之上? 基本上我想在seaborn中这样做:
任何清理它的想法都是最受欢迎的。 谢谢。
MWE:
sns.set_context("paper",font_scale=2) sns.set_style("white") rc('text', usetex=False) fig, ax = plt.subplots(figsize=(7,7),sharey=True) sns.despine(left=True) mats=dict() mats[0]=[1,1,1,1,1,2,3,3,2,3,3,3,3,3] mats[1]=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5] N=max(max(set(mats[0])),max(set(mats[1]))) binsize = np.arange(0,N+1,1) B=['Thing1','Thing2'] for i in range(len(B)): ax = sns.distplot(mats[i], kde=False, label=B[i], bins=binsize) ax.set_xlabel('My label') ax.get_yaxis().set_visible(False) ax.legend() plt.show()
I am trying to figure a nice way to plot two distplots (from seaborn) on the same axis. It is not coming out as pretty as I want since the histogram bars are covering each other. And I don't want to use countplot or barplot simply because they don't look as pretty. Naturally if there is no other way I shall do it in that fashion, but distplot looks very good. But, as said, the bars are now covering each other (see pic).
Thus is there any way to fit two distplot frequency bars onto one bin so that they do not overlap? Or placing the counts on top of each other? Basically I want to do this in seaborn:
Any ideas to clean it up are most welcome. Thanks.
MWE:
sns.set_context("paper",font_scale=2) sns.set_style("white") rc('text', usetex=False) fig, ax = plt.subplots(figsize=(7,7),sharey=True) sns.despine(left=True) mats=dict() mats[0]=[1,1,1,1,1,2,3,3,2,3,3,3,3,3] mats[1]=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5] N=max(max(set(mats[0])),max(set(mats[1]))) binsize = np.arange(0,N+1,1) B=['Thing1','Thing2'] for i in range(len(B)): ax = sns.distplot(mats[i], kde=False, label=B[i], bins=binsize) ax.set_xlabel('My label') ax.get_yaxis().set_visible(False) ax.legend() plt.show()最满意答案
正如@mwaskom所说,seaborn正在包装matplotlib绘图函数(大部分都是如此),以提供更复杂和更好看的图表。
您正在寻找的是“足够简单”来使用matplotlib完成它:
sns.set_context("paper", font_scale=2) sns.set_style("white") plt.rc('text', usetex=False) fig, ax = plt.subplots(figsize=(4,4)) sns.despine(left=True) # mats=dict() mats0=[1,1,1,1,1,2,3,3,2,3,3,3,3,3] mats1=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5] N=max(mats0 + mats1) # binsize = np.arange(0,N+1,1) binsize = N B=['Thing1','Thing2'] ax.hist([mats0, mats1], binsize, histtype='bar', align='mid', label=B, alpha=0.4)#, rwidth=0.6) ax.set_xlabel('My label') ax.get_yaxis().set_visible(False) # ax.set_xlim(0,N+1) ax.legend() plt.show()产量:
您可以取消注释ax.set_xlim(0,N+1)以在此直方图周围提供更多空间。
As @mwaskom has said seaborn is wrapping matplotlib plotting functions (well to most part) to deliver more complex and nicer looking charts.
What you are looking for is "simple enough" to get it done with matplotlib:
sns.set_context("paper", font_scale=2) sns.set_style("white") plt.rc('text', usetex=False) fig, ax = plt.subplots(figsize=(4,4)) sns.despine(left=True) # mats=dict() mats0=[1,1,1,1,1,2,3,3,2,3,3,3,3,3] mats1=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5] N=max(mats0 + mats1) # binsize = np.arange(0,N+1,1) binsize = N B=['Thing1','Thing2'] ax.hist([mats0, mats1], binsize, histtype='bar', align='mid', label=B, alpha=0.4)#, rwidth=0.6) ax.set_xlabel('My label') ax.get_yaxis().set_visible(False) # ax.set_xlim(0,N+1) ax.legend() plt.show()Which yields:
You can uncomment ax.set_xlim(0,N+1) to give more space around this histogram.
更多推荐
发布评论