Spacing between histogram bars with thousands of bins
Asked Answered
V

2

31

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:

import matplotlib.pyplot as plt
import random

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

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

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 each other with no whitespace in-between.

Verbena answered 20/8, 2014 at 0:11 Comment(1)
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?Blim
S
56

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

Shoestring answered 25/6, 2015 at 10:12 Comment(0)
E
0

plt.hist ultimately uses plt.bar plot the bars so another way to make a gap between bars is through width= parameter.

fig, ax1 = plt.subplots()
ax1.hist(coordinate_counts, bins=range(1, 10000), width=0.5)

One thing to note is that unlike rwidth where the width of a bar depends on how large its bin is (and is a value between 0 and 1), width is an absolute value that determines how wide the bar should be (and can be larger than 1). An example may illustrate better. The following code

coordinate_counts = list(range(10))*10
plt.hist(coordinate_counts, bins=[0, 3, 8, 10], width=2);

plots the following chart

first histogram

while the following code plots

plt.hist(coordinate_counts, bins=[0, 3, 8, 10], rwidth=0.5);

second hist

Enticement answered 31/5, 2023 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.