简体   繁体   中英

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

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 .

>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].

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.

在此输入图像描述

update #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 widths and still has more than one column of graphs :

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

在此输入图像描述

update #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.

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

在此输入图像描述

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))

在此输入图像描述

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.


Edit:

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

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))

在此输入图像描述

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

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))

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM