0

I have two data list data value(each 100 numbers) d1 = [0.5, 0.6, 0.45, ........], d2= [0.45,0.65, ........]. I want to plot the two hist with two list data, like the following figure! How to plot it with matplotlib, Thanks! my code is following:

def plot_data(d1, d2):
   fig, ax = plt.subplots()
   ax.hist(d1, 100,  50, ec='red', fc='none', lw=1.5, histtype='step', label='n-gram')
   ax.hist(d2, 100, 50, ec='green', fc='none', lw=1.5, histtype='step', label='ensemble')
   ax.legend(loc='upper left')
  plt.show()

but there is error:

mn, mx = [mi + 0.0 for mi in range]
TypeError: 'int' object is not utterable

two hist with two data lists

2
  • 1
    where is this code that's giving you error. Commented Nov 17, 2016 at 10:46
  • 1
    What @harshil9968 is saying is that there is no evidence in what you showed us that the error has anything to do with plotting such data. Commented Nov 17, 2016 at 10:51

1 Answer 1

3

Your error comes from the third argument to the hist() function. Read the documentation for hist():

Parameters: x : (n,) array or sequence of (n,) array (...)

bins : integer or array_like, optional (...)

range : tuple or None, optional

The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, range is (x.min(), x.max()). Range has no effect if bins is a sequence.**

If bins is a sequence or range is specified, autoscaling is based on the specified bin range instead of the range of x.

Default is None

The third parameter must be either a tuple or None and you've provided 50 (an int).

see the code below, where I simply replaced the 3rd argument by None (and reduced the number of bins)

d1 = np.random.normal(size=(100,))
d2 = 1+np.random.normal(size=(100,))

fig, ax = plt.subplots()
ax.hist(d1, 10, None, ec='red', fc='none', lw=1.5, histtype='step', label='n-gram')
ax.hist(d2, 10, None, ec='green', fc='none', lw=1.5, histtype='step', label='ensemble')
ax.legend(loc='upper left')
plt.show()

enter image description here

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

1 Comment

If my answer was helpful, please consider accepting it by clicking on the check mark on the left

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.