如何纠正我对子情节的误解

huangapple go评论63阅读模式
英文:

How to correct my misunderstandings of subplots

问题

I seemed to understand how the subplots and ax options work in Matplotlib, but I just did a graphical test and it doesn't work at all as expected, which shows me that I still don't quite understand the syntax of the statements.

The program is below; I should get 2 separate graphs but there is only 1, which seems to overlay the 2 datasets.

[NOTE: the code must use sns.histplot; the p1 and p2 data do not correspond to the reality of the data to be presented.]

Could you explain to me what my typing errors are?

p1 = [7.86706728, 2.07023424, 8.59099644, 7.07850226, 9.79575806]
p2 = [1.48705512, 0.3142216 , 0.3407479 , 0.32947036, 0.32947036]

fig, ax = plt.subplots(1, 2, figsize=(8, 3), tight_layout=True)

ax[0] = sns.histplot(data=None, x=p1, bins=25, discrete=False, shrink=1.0,
             stat="probability", element="bars", color="green", kde=False)

ax[0].set_title("p1", fontsize='15')
ax[0].set_xlabel("p1", fontsize='15')
ax[0].set_ylabel("Probability", fontsize='15')

ax[1] = sns.histplot(data=None, x=p2, bins=25, discrete=False, shrink=1.0,
             stat="probability", element="bars", color="green", kde=False)

ax[1].set_title("p2", fontsize='15')
ax[1].set_xlabel("p2", fontsize='15')
ax[1].set_ylabel("Probability", fontsize='15')

plt.show()

Wrong output: 如何纠正我对子情节的误解

英文:

I seemed to understand how the subplots and ax options work in Matplotlib, but I just did a graphical test and it doesn't work at all as expected, which shows me that I still don't quite understand the syntax of the statements.

The program is below; I should get 2 separate graphs but there is only 1, which seems to overlay the 2 datasets.

[NOTE: the code must use sns.histplot; the p1 and p2 data do not correspond to the reality of the data to be presented.]

Could you explain to me what my typing errors are?

p1 = [7.86706728, 2.07023424, 8.59099644, 7.07850226, 9.79575806]
p2 = [1.48705512, 0.3142216 , 0.3407479 , 0.32947036, 0.32947036]

fig, ax = plt.subplots(1, 2, figsize = (8,3), tight_layout = True)

ax[0] = sns.histplot(data = None, x = p1, bins = 25, discrete = False, shrink = 1.0,
             stat = "probability", element = "bars", color = "green", kde = False) 

ax[0].set_title("p1", fontsize = '15')
ax[0].set_xlabel("p1" , fontsize = '15')
ax[0].set_ylabel("Probability", fontsize = '15')

ax[1] = sns.histplot(data = None, x = p2, bins = 25, discrete = False, shrink = 1.0,
             stat = "probability", element = "bars", color = "green", kde = False) 

ax[1].set_title("p2", fontsize = '15')
ax[1].set_xlabel("p2" , fontsize = '15')
ax[1].set_ylabel("Probability", fontsize = '15')

plt.show()

Wrong output:
如何纠正我对子情节的误解

答案1

得分: 1

你的问题在于你使用seaborn绘制直方图的方式。根据它的文档(这里),你可以作为参数提供一个预先存在的轴(ax)。如果没有提供,seaborn将调用plt.gca()(获取当前轴)。由于你的代码中没有提供ax作为参数,两次调用sns.histplot()都将调用plt.gca()。但是,当前轴是ax[1],因此它是两次直方图调用所选择的轴。

因此,你的第一次调用应该是:

sns.histplot(ax=ax[0], ...) # 和其他参数一起

而第二次调用应该是:

sns.histplot(ax=ax[1], ...) # 和其他参数一起
英文:

Your problem is in the way you are using the seaborn histogram plotting. From its documentation (here), you can provide, as argument, a pre-existing axes. If not provided, seaborn will call plt.gca() (get current axes). As you are not providing an ax as an argument in your code, both calls of sns.histplot() will call plt.gca(). But, the current axes, is ax[1], so it is the axes selected for both histogram calls.

So, your first call should be:

sns.histplot(ax = ax[0], ...) # and the rest of the arguments

And the second:

sns.histplot(ax = ax[1], ... ) # and the rest of the arguments

huangapple
  • 本文由 发表于 2023年5月17日 17:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定