ble*_*man 3 python matplotlib histogram pandas
我正在努力为每个直方图设置xlim并创建1列图形,以便x轴刻度对齐.作为新的大熊猫,我不确定如何应用答案适用:使用熊猫覆盖多个直方图.
>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)
Run Code Online (Sandbox Code Playgroud)
结果只将x轴上的一个边界设置为[-1,1].
我非常熟悉R中的ggplot,只是在python中尝试pandas/matplotlib.我愿意接受更好的策划思路的建议.任何帮助将不胜感激.

更新#1(@ ct-zhu):
我尝试了以下内容,但子图上的xlim编辑似乎并未在新的x轴值上转换bin宽度.因此,图形现在具有奇数的bin宽度,并且仍然具有多列图形:
for array in df.hist(bins=10):
for subplot in array:
subplot.set_xlim((-1,1))
Run Code Online (Sandbox Code Playgroud)

更新#2:
越接近使用layout,但箱的宽度不等于间隔长度除以箱数.在下面的例子中,我设置了bins=10.因此,每个箱子在[-1,1]间隔的宽度应为2/10=0.20; 但是,图表没有任何宽度为0.20的箱柜.
for array in df.hist(layout=(2,1),bins=10):
for subplot in array:
subplot.set_xlim((-1,1))
Run Code Online (Sandbox Code Playgroud)

有两个子图,您可以访问它们并单独修改它们:
ax_list=df.hist()
ax_list[0][0].set_xlim((0,1))
ax_list[0][1].set_xlim((0.01, 0.07))
Run Code Online (Sandbox Code Playgroud)

您正在执行的操作plt.xlim仅更改当前工作轴的限制.在这种情况下,它是最近生成的第二个图.
要将绘图分为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))
Run Code Online (Sandbox Code Playgroud)

或者在(-1,1)之间精确指定10个区间:
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))
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
17767 次 |
| 最近记录: |