Defining bin width/x-axis scale in Matplotlib histogram

我与影子孤独终老i 提交于 2019-11-29 15:47:17
imsc

You can use bar and there is no need to split the axis. Here is an example,

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

In case you really want to split the axis have a look here.

physicalattraction

I had a similar question here, and the answer was to use a dirty hack. Matplotlib histogram with collection bin for high values

So with the following code, you get the ugly histogram you already have.

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

In order to make the bins equal width, you indeed have to make them equal width! This means manipulating your data such that they all fall in bins with equal width, and then play around with the 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()

TVC
import pandas as pd
import numpy as np

df= data

bins = np.arange(0,0.1,0.001)
df.hist(bins=bins,color='grey')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!