5

I am trying to draw histogram but nothing appears in the Figure Window. My code is below:

import numpy as np
import matplotlib.pyplot as plt

values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]

plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,11,0,220000])
plt.show()

This is the output: enter image description here

I am trying to achieve this plot enter image description here

Any help would be much appreciated...

2
  • plt.axis([0,11,0,220000]) this makes no sense. Can you say a word or two about why you included this and what it is supposed to do? Commented May 4, 2016 at 4:21
  • Yes, sorry that was wrong, my bad... I added it at start when my plot was displaying negative values... don't know why Commented May 4, 2016 at 4:31

2 Answers 2

7

You are confusing what a histogram is. The histogram that can be produced with the given data is as given below.

A histogram basically counts how many given values fall within a given range.

You have given incorrect arguments to the axis() function. The ending value is 2200000 You missed a single zero. Also you have swapped the arguments. Limits of the x axis comes first and then the limits of the Y axis. This is the modified code:

import numpy as np
import matplotlib.pyplot as plt

values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]

plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,2200000,0,11])
plt.show()

This is the histogram generated:

enter image description here

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

6 Comments

Yes that is my mistake but even changing 220000 with 2200000 didn't fix anything, I am trying to put 0-10 along x-axis and 0-2200000 along y-axis. Thanks anyway
@SohailAshraf You misunderstand what a histogram is. See the updated answer.
No I didn't misunderstand. Please check the output I want to generate in question above... Thanks
@SohailAshraf It is not possible to generate such an output from the given histogram. Atleast that is not what a histogram does. You will have to generate 1000000 values of '1' , and so on and then plot the histogram
Sorry, I get it... I was doing it wrong way, It is not possible... I need to use the bar chart. Anyway thanks for your help...
|
5

I finally achieved it...

Here is the code:

import numpy as np
import matplotlib.pyplot as plt

values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]
strategy = [1,2,3,4,5,6,7,8,9,10]
value = np.array(values)
strategies = np.array(strategy)
plt.bar(strategy, values, .8)
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([1,11,0,2200000])
plt.show()

Output: enter image description here

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.