0

I'm using a java program to split an array among histogram bins. Now, I want to manually label the histogram bins. So - I want to convert some thing like the sequence: {-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.5,1,1.5,2,2.5,4} into the following image -

enter image description here

Is there a way to do this using any software? I'm on Windows and some thing based on R, python, java or matlab would be awesome. I currently do it manually using mspaint.

Rohit Pandey
  • 2,443
  • 7
  • 31
  • 54
  • Surely if you label your x axis correctly, and make sure the bars are in the right place (i.e. thinner bars for narrower bins), you won't need such an image? Also, use a graphing package, this is crazy by hand. Histograms in R: http://www.statmethods.net/graphs/density.html – Phil H Mar 04 '14 at 09:38

3 Answers3

1

Well, the simplest approach is (Python):

import matplotlib.pyplot as plt

d = [-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.5,1,1.5,2,2.5,4]
hist(d)

plt.show()

As for putting special labels on the histogram, that's covered in the question: Matplotlib - label each bin.

I'm guessing you want to keep it simple, so you can do this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
d = [-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.5,1,1.5,2,2.5,4]
counts, bins, patches = ax.hist(d)
ax.set_xticks(bins)

plt.show()
Community
  • 1
  • 1
ZSG
  • 1,329
  • 14
  • 18
1

To arrange data in bins then display them with equal width bars you can use matplotlib in Python

The previous Python answer doesn't bin the values, set them at equal distance nor handle (-infinity, 0.9) bin

The code here hopefully does:

import matplotlib.pyplot as plt
import numpy as np

# data and bins
d = [-0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -
     0.2, -0.1, 0, 0.1, 0.5, 1, 1.5, 2, 2.5, 4]
bin_boundaries = [-0.9, -0.8, -0.7, -0.6, -0.5, -0.4,
                  -0.3, -0.2, -0.1, 0, 0.1, 0.5, 1, 1.5, 2, 2.5, 4]
# Account for (-inf, min(d)) bin
added_inf = False
min_d = min(d)
if min_d < bin_boundaries[0]:
    bin_boundaries.insert(0, min_d)
    added_inf = True

# Bin data
fig, ax = plt.subplots()
counts, bins, _ = ax.hist(d, bins=bin_boundaries)
plt.close()

# Generate tick labels and handle (-inf, min(d)) bin
bins_str = [f"{bins[i]} to {bins[i+1]}" for i in range(len(bins) - 1)]
if added_inf:
    bins_str[0] = f"-inf to {bins[1]}"
else:
    bins = np.insert(bins, 0, [bins[0] - 1])
    counts = np.insert(counts, 0, [0])
    bins_str.insert(0, f"-inf to {bins[1]}")
    

fig, ax = plt.subplots()
# Ajdust size of plot
fig.set_dpi(150)
# Draw graph with bins_str as labels
ax.bar(bins_str, counts, width=1.0)
# Make labels vertical
plt.xticks(rotation=90, ha='center')

plt.show()
plt.close()

freshpasta
  • 556
  • 2
  • 11