18
votes

I'm making histograms using matplotlib's hist() function or bar(), and I want to use >10,000 bins (one bin to represent the counts at each coordinate of a large entity). Is there any way to create more whitespace between the vertical bars when I create the figure? Currently, there is no whitespace between each bar of the histogram. For example:

# imports
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import random

# Generating dummy data    
coordinate_counts = []
for __ in range(1,100000) :
    coordinate_counts.append(random.randrange(1,10000))

# plotting
fig, ax1 = plt.subplots()
ax1.hist(coordinate_counts,bins=range(1,10000))
fig.savefig('temp.png')

I've tried using rwidth() and varying the value of that, as well as tried using figsize() and simply expanding the size of the plot, but the final result always has each vertical bar next to eachother with no whitespace inbetween.

1
Using the rwidth keyword argument works for me. Granted, I had to zoom in quite a bit before I could actually resolve the individual bars. If you use rwidth with only 10 bins do you still not get any space between the columns?AMacK
I think you might be expecting too much from your display - how do you expect to see space between each of 10000 bins (or even to see all 10000 un-downsampled bins) when your monitor probably only has ~2000 ish pixels across it?Ajean
Thanks for the insight--I was thinking about that same display issue after I posted the question!whymca

1 Answers

42
votes

The parameter rwidth specifies the width of your bar relative to the width of your bin. For example, if your bin width is say 1 and rwidth=0.5, the bar width will be 0.5. On both side of the bar you will have a space of 0.25.

Mind: this gives a space of 0.5 between consecutive bars. With the number of bins you have, you won't see these spaces. But with fewer bins they do show up.

enter image description here