简体   繁体   English

如何调整 matplotlib.pyplot 直方图的 x 轴?

[英]How can I adjust the x-axis of a matplotlib.pyplot histogram?

total noob here.总菜鸟在这里。 I need to create a histogram, and it is coming out terribly.我需要创建一个直方图,结果非常糟糕。

可怕的直方图

As you can see it generates one large bar for all 150,000 data points.如您所见,它为所有 150,000 个数据点生成一个大条。 The range is a percent, so almost all of the values should between 0 and 1. I want to see what is happening between 0 and 1, not that most all of the values are between 0 and 1.范围是一个百分比,所以几乎所有的值都应该在 0 和 1 之间。我想看看 0 和 1 之间发生了什么,而不是大多数值都在 0 和 1 之间。

Here is the code I used:这是我使用的代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/data.csv')

df['DebtRatio'].hist();
plt.xticks(np.arange(0, 2, 0.1))

How can I get this histogram to actually be a histogram and show me the distribution across the lowest and most prominent end of the range?我怎样才能让这个直方图真正成为一个直方图,并向我展示该范围最低端和最突出端的分布?

Thanks谢谢

As said by @ImportanceOfBeingErnest your actual values range from 0 to 329664, which explain why your histogram looks like that.正如@ImportanceOfBeingErnest 所说,您的实际值范围从 0 到 329664,这解释了为什么您的直方图看起来如此。

If you're sure that it must be a ratio (between 0 and 1), then you can filter your rows:如果您确定它必须是一个比率(介于 0 和 1 之间),那么您可以过滤您的行:

import pandas as pd
import matplotlib.pyplot as plt

if __name__ == '__main__':
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/data.csv')

    print(df['DebtRatio'].describe())  # Look a the max value
    df['DebtRatio'].loc[df['DebtRatio'] <= 1].hist()  # Plot an histogram of filtered values
    plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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