Creating histograms in pandas with columns with equidistant base, not proportional to the range
<p>I am creating an histogram in pandas simply using:</p>

<pre><code>train_data.hist("MY_VARIABLE", bins=[0,5, 10,50,100,500,1000,5000,10000,50000,100000])
</code></pre>

<p>(train_data is a pandas df).</p>

<p>The problem is that, since the range <code>[50000,100000]</code> is so large, I can barely see the small ranges <code>[0,5]</code> or <code>[5,10]</code> etc. I would like the histogram to have equidistant bars on the x-axis, not proportional to the range. Is this possible?</p>

<p>You can do it this way:</p>

<pre><code>bins = [0, 5, 10,50,100,500,1000,5000,10000,50000,100000]
df.groupby(pd.cut(df.a, bins=bins, labels=bins[1:])).size().plot.bar(rot=0)
</code></pre>

<p>Demo:</p>

<pre><code>df = pd.DataFrame(np.random.randint(0,10**5,(10**4,2)),columns=list('ab'))
bins = [0, 5, 10,50,100,500,1000,5000,10000,50000,100000]
df.groupby(pd.cut(df.a, bins=bins, labels=bins[1:])).size().plot.bar(rot=0)
</code></pre>

<p><a href="https://i.stack.imgur.com/63Awq.png" rel="nofollow"><img src="https://i.stack.imgur.com/63Awq.png" alt="enter image description here"></a></p>

<p>filtering results:</p>

<pre><code>threshold = 100
(df.groupby(pd.cut(df.a,
                   bins=bins, 
                   labels=bins[1:]))
   .size()
   .to_frame('count')
   .query('count &gt; @threshold')
)

Out[84]:
        count
a
5000      396
10000     492
50000    4044
100000   4961
</code></pre>

<p>plotting filtered:</p>

<pre><code>(df.groupby(pd.cut(df.a,
                   bins=bins, 
                   labels=bins[1:]))
   .size()
   .to_frame('count')
   .query('count &gt; @threshold')
   .plot.bar(rot=0, width=1.0)
)
</code></pre>

<p><a href="https://i.stack.imgur.com/dL0yM.png" rel="nofollow"><img src="https://i.stack.imgur.com/dL0yM.png" alt="enter image description here"></a></p>

python pandas histogram