4

I'm using matplotlib to make a histogram.

Basically, I'm wondering if there is any way to manually set the bins as well as the their values and still get the result as if the graph is made my matplotlin histogram. Below are my bins and their corresponding value.

0-7     0.9375
7-13    0.9490740741
13-18   0.8285714286 
18-28   0.880952381
28-37   0.92164903
37-48   0.9345357019
48-112  0.9400368773

This is the bar code i have implemented

import matplotlib.pyplot as plt
plt.bar(maxlist, mean)
plt.show()

maxlist = [7.0, 13.0, 18.0, 28.0, 37.0, 48.0, 112.0]
mean = [0.9375, 0.94907407407777766, 0.82857142858571442, 0.88095238094999995, 0.92164902996666676, 0.93453570191250002, 0.94003687729000018]

This is what the bar plot of the above code looks like. histwithoutwidth

I also tried playing with width parameter but the bar seems to overlap in that case, which doesn't happen in histogram.

import matplotlib.pyplot as plt
plt.bar(maxlist, mean,width = 6)
plt.show()

enter image description here

What i want is the bar plot to look like histogram as shown below, without any overlaps. Anyone with any ideas how to do this in python /ipython notebook. enter image description here

3
  • so you want the bars to be equal width, but the base of them not equal width? or do you some of the bars to be wider to fill in the gaps? Commented Mar 24, 2016 at 2:16
  • Bins allow you to give very specific bins, by passing a list. The display is not affected. For that look to histtype , align and rwidth in the excellent documentation: matplotlib.org/api/pyplot_api.html. Commented Mar 24, 2016 at 2:21
  • @maxymoo I want bars of equal length, without having them to overlap with each other. Base cannot be of equal length since the range of bin differ everytime. Hope this clarifies your doubt. Commented Mar 24, 2016 at 2:23

1 Answer 1

5

From the documentation linked by @roadrunner66,

matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)

Make a bar plot with rectangles bounded by:

left, left + width, bottom, bottom + height

(left, right, bottom and top edges)

The first parameter passed to bar is actually the left edge of your "bin." It seems that maxlist, which you're passing as left is actually the right edge of your bins which is throwing everything off and causing your widths to be weird.

import matplotlib.pyplot as plt
import numpy as np
x1 = [0, 7, 13, 18, 28, 37, 48] #left edge
x2 = x1[1::] + [112] #right edge
y = [0.9375, 0.94907407407777766, 0.82857142858571442, 0.88095238094999995, 0.92164902996666676, 0.93453570191250002, 0.94003687729000018]
w = np.array(x2) - np.array(x1) #variable width, can set this as a scalar also
plt.bar(x1, y, width=w, align='edge')
plt.show()

Bar chart with variable bin widths

The bar widths are representative of the distances between the left and right edges, x1 and x2. If you wanted constant widths, just pass width=w_scalar to plt.bar(), where w_scalar is your constant bin width. If you want the bars farther apart, just make width smaller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.