-1

I am attempting to draw a histogram in Python using the marplot.lib library; however, I keep getting this error: "AttributeError: bins must increase monotonically."

This is my code at the moment:

def draw_viz(info):
    stars = [tup[0] for tup in info]
    scores = [tup[1] for tup in info]

    plt.hist(range, scores)
    plt.show()

The parameter being passed in is info. Info is a list of tuples that could look like this:

[(4, 0.7984031936127745), (5, 0.5988023952095809), (5, 0.8739076154806492), (5, 0.7364544976328248), (3, 0.9980039920159681), (1, 0.8455034588777863), (4, 0.6659267480577137), (5, 0.9950248756218907), (5, 0.9991673605328892), (3, 0.5828476269775188), (1, 0.5226084980686208), (1, 0.5291005291005291), (4, 0.7984031936127745), (4, 0.5380476556495004), (5, 0.6357856494096277), (2, 0.9975062344139651), (4, 0.6644518272425249)]

My range is restricted to 1,2,3,4,or 5. The scores are between 0 and 1.

I'd like to draw a histogram that can handle what I am passing in, but I'm really unsure of how to do so or even initialize bins for this one...

Any help would be appreciated!

ajshort
  • 3,684
  • 5
  • 29
  • 43
natalien
  • 81
  • 3
  • 13

2 Answers2

2

You forgot to pass a value to the range argument (which is not really necessary). The data itself is perfectly valid. The following code should give you a rough graph:

plt.hist(stars, bins=5)
plt.show()

For something a little better looking, maybe:

plt.hist(stars, bins=np.arange(6)+0.5)
plt.xticks(range(1,6))
plt.xlim([0.5,5.5])
plt.show()

For further reference:

Community
  • 1
  • 1
Gustavo Bezerra
  • 9,984
  • 4
  • 40
  • 48
0

You are given the arguments in the wrong order hist needs first the data, and then optionally a number of bins, or a list of bin edges. In your case I'd do

plt.hist(stars,[0.5,1.5,2.5,3.5,4.5,5.5])
plt.show()

plt.hist(scores)
plt.show
Noel Segura Meraz
  • 2,265
  • 1
  • 12
  • 17