Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matplotlib直方图中定义仓位宽度/x轴比例_Matplotlib_Axis - Fatal编程技术网

在Matplotlib直方图中定义仓位宽度/x轴比例

在Matplotlib直方图中定义仓位宽度/x轴比例,matplotlib,axis,Matplotlib,Axis,我正在用matplotlib生成直方图 我需要的箱子是不平等的宽度,因为我最感兴趣的是最低的箱子。 现在我正在这样做: plt.hist(hits_array, bins = (range(0,50,10) + range(50,550,50))) 这创建了我想要的(前5个箱子的宽度为10,其余为50),但前5个箱子当然比后5个箱子窄,因为所有箱子都显示在同一个轴上 是否有办法影响x轴或直方图本身,以便我可以在前5个箱子之后打破比例,以便所有箱子都显示为同等宽 (我意识到这将创建一个扭曲的视图

我正在用matplotlib生成直方图

我需要的箱子是不平等的宽度,因为我最感兴趣的是最低的箱子。 现在我正在这样做:

plt.hist(hits_array, bins = (range(0,50,10) + range(50,550,50)))
这创建了我想要的(前5个箱子的宽度为10,其余为50),但前5个箱子当然比后5个箱子窄,因为所有箱子都显示在同一个轴上

是否有办法影响x轴或直方图本身,以便我可以在前5个箱子之后打破比例,以便所有箱子都显示为同等宽

(我意识到这将创建一个扭曲的视图,对此我没有意见,尽管我不介意轴的两个不同比例部分之间有一点空间。)

任何帮助都将不胜感激。
谢谢

您可以使用
,无需拆分轴。举个例子,

import matplotlib.pylab as plt
import numpy as np

data = np.hstack((np.random.rand(1000)*50,np.random.rand(100)*500))
binwidth1,binwidth2=10,50
bins=range(0,50,binwidth1)+range(50,550,binwidth2)

fig,(ax) = plt.subplots(1, 1)

y,binEdges=np.histogram(data,bins=bins)

ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[:5], y[:5],width=.8*binwidth1,align='center')
ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[5:], y[5:],width=.8*binwidth1,align='center')
plt.show()


如果你真的想拆分轴,请看一看。

我这里有一个类似的问题,答案是使用肮脏的黑客。

因此,使用下面的代码,您将得到您已经拥有的丑陋的直方图

def plot_histogram_04():
    limit1, limit2 = 50, 550
    binwidth1, binwidth2 = 10, 50    
    data = np.hstack((np.random.rand(1000) * limit1, np.random.rand(100) * limit2))

    bins = range(0, limit1, binwidth1) + range(limit1, limit2, binwidth2)

    plt.subplots(1, 1)
    plt.hist(data, bins=bins)
    plt.savefig('my_plot_04.png')
    plt.close()

为了使垃圾箱的宽度相等,你必须使它们的宽度相等!这意味着操纵数据,使它们都落在宽度相等的容器中,然后使用xlabel

def plot_histogram_05():
    limit1, limit2 = 50, 550
    binwidth1, binwidth2 = 10, 50

    data = np.hstack((np.random.rand(1000) * limit1, np.random.rand(100) * limit2))

    orig_bins = range(0, limit1, binwidth1) + range(limit1, limit2 + binwidth2, binwidth2)
    data = [(i - limit1) / (binwidth2 / binwidth1) + limit1 
            if i >= limit1 else i for i in data]
    bins = range(0, limit2 / (binwidth2 / binwidth1) + limit1, binwidth1)

    _, ax = plt.subplots(1, 1)
    plt.hist(data, bins=bins)

    xlabels = np.array(orig_bins, dtype='|S3')
    N_labels = len(xlabels)
    print xlabels
    print bins
    plt.xlim([0, bins[-1]])
    plt.xticks(binwidth1 * np.arange(N_labels))
    ax.set_xticklabels(xlabels)

    plt.savefig('my_plot_05.png')
    plt.close()

虽然此代码可以回答问题,但提供有关此代码为什么和/或如何回答问题的附加上下文可以提高其长期价值。我倾向于同意此评论,但此答案非常好,完全符合要求,如果您使用过bins参数,也非常容易理解。
import pandas as pd
import numpy as np

df= data

bins = np.arange(0,0.1,0.001)
df.hist(bins=bins,color='grey')