Python 如何在一个包含多个 matplotlib 直方图的图形中设置 x 轴的边界并仅创建一列图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23983642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:46:36  来源:igfitidea点击:

how to set bounds for the x-axis in one figure containing multiple matplotlib histograms and create just one column of graphs?

pythonmatplotlibpandashistogram

提问by blehman

I am struggling to set xlim for each histogram and create 1 column of graphs so the x-axis ticks are aligned. Being new pandas, I am unsure of how to apply answer applies: Overlaying multiple histograms using pandas.

我正在努力为每个直方图设置 xlim 并创建 1 列图形,以便 x 轴刻度对齐。作为新熊猫,我不确定如何应用答案适用:使用 pandas 叠加多个直方图

>import from pandas import DataFrame, read_csv
>import matplotlib.pyplot as plt
>import pandas as pd

>df=DataFrame({'score0':[0.047771,0.044174,0.044169,0.042892,0.036862,0.036684,0.036451,0.035530,0.034657,0.033666],
              'score1':[0.061010,0.054999,0.048395,0.048327,0.047784,0.047387,0.045950,0.045707,0.043294,0.042243]})

>print df
     score0    score1
0  0.047771  0.061010
1  0.044174  0.054999
2  0.044169  0.048395
3  0.042892  0.048327
4  0.036862  0.047784
5  0.036684  0.047387
6  0.036451  0.045950
7  0.035530  0.045707
8  0.034657  0.043294
9  0.033666  0.042243

>df.hist()
>plt.xlim(-1.0,1.0)

The result sets only one of the bounds on the x-axis to be [-1,1].

结果仅将 x 轴上的边界之一设置为 [-1,1]。

I'm very familiar ggplot in R and just trying out pandas/matplotlib in python. I'm open to suggestions for better plotting ideas. Any help would be greatly appreciated.

我非常熟悉 R 中的 ggplot,只是在 python 中尝试了 pandas/matplotlib。我愿意接受更好的策划想法的建议。任何帮助将不胜感激。

enter image description here

在此处输入图片说明

update #1 (@ct-zhu):

更新 #1 (@ct-zhu):

I have tried the following, but the xlim edit on the subplot does not seem to translate the bin widths across the new x-axis values. As a result, the graph now has odd bin widthsand still has more than one column of graphs:

我尝试了以下操作,但是子图上的 xlim 编辑似乎没有将 bin 宽度转换为新的 x 轴值。结果,该图现在具有奇数 bin 宽度,并且仍然有不止一列图

for array in df.hist(bins=10):
    for subplot in array:
        subplot.set_xlim((-1,1))

enter image description here

在此处输入图片说明

update #2:

更新#2:

Getting closer with the use of layout, but the width of bins does not equal the interval length divided by bin count. In the example below, I set bins=10. Hence, the width of each bin over the interval from [-1,1] should be 2/10=0.20; however, the graph does not have any bins with a width of 0.20.

使用 接近layout,但 bin 的宽度不等于间隔长度除以 bin 计数。在下面的示例中,我设置了bins=10. 因此,从 [-1,1] 开始的区间内每个 bin 的宽度应该是2/10=0.20; 但是,该图没有任何宽度为 0.20 的 bin。

for array in df.hist(layout=(2,1),bins=10):
    for subplot in array:
        subplot.set_xlim((-1,1))

enter image description here

在此处输入图片说明

采纳答案by CT Zhu

There are two subplots, and you can access each of them and modify them seperately:

有两个子图,您可以访问每个子图并分别修改它们:

ax_list=df.hist()
ax_list[0][0].set_xlim((0,1))
ax_list[0][1].set_xlim((0.01, 0.07))

enter image description here

在此处输入图片说明

What you are doing, by plt.xlim, changes the limit of the current working axis only. In this case, it is the second plot which is the most recently generated.

您正在做什么,通过plt.xlim,仅更改当前工作轴的限制。在这种情况下,它是最近生成的第二个图。



Edit:

编辑:

To make the plots into 2 rows 1 column, use layoutargument. To make the bin edges aligns, use binsargument. Set the x limit to (-1, 1)is probably not a good idea, you numbers are all smallish.

要将图分成 2 行 1 列,请使用layout参数。要使 bin 边缘对齐,请使用bins参数。将 x 限制设置(-1, 1)为可能不是一个好主意,您的数字都很小。

ax_list=df.hist(layout=(2,1),bins=np.histogram(df.values.ravel())[1])
ax_list[0][0].set_xlim((0.01, 0.07))
ax_list[1][0].set_xlim((0.01, 0.07))

enter image description here

在此处输入图片说明

Or specify exactly 10 bins between (-1,1):

或者在 (-1,1) 之间精确指定 10 个 bin:

ax_list=df.hist(layout=(2,1),bins=np.linspace(-1,1,10))
ax_list[0][0].set_xlim((-1,1))
ax_list[1][0].set_xlim((-1,1))

enter image description here

在此处输入图片说明