5

I've the following code that I'm using to plot a numpy array as a histogram. All I end up getting is a box.

from sys import argv as a
import numpy as np
import matplotlib.pyplot as plt

r = list(map(int, (a[1], a[2], a[3], a[4], a[5])))
s = np.array([int((x - min(r))/(max(r) - min(r)) * 10) for x in r])
plt.hist(s, normed=True, bins=5)
plt.show()

The program is run with the following inputs 22 43 11 34 26

How can I get a histogram with the y axis representing the heights of list elements.

1
  • The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead. Commented Oct 25, 2019 at 7:12

3 Answers 3

12

You can use plt.bar:

plt.bar(np.arange(len(s)),s)
plt.show()

Which turns into the below. Is that your expected output?

enter image description here

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

Comments

4

You cannot get a histogram with the y axis representing the values of list elements.

Per definition a histogram gives the number of elements, which fall into certain bins, or the probability to find the element in a certain bin. plt.hist is plotting function that draws a bar chart from such a histogram.

So what happens when you call plt.hist(s, normed=True, bins=5) is that the normalized input array s = [ 3, 10, 0, 7, 4] is divided into 5 bins between 0 and 10. There is exactly one number from s falling into each bin, so all bars in the hisogram plot have the same height.

enter image description here

Since in this case you don't actually want to have a histogram at all, but only a bar chart of the values, you should use plt.bar with the array s as height argument and some index as position.

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

a = ["some file", "22", "43", "11","34", "26"]

r = list(map(int, (a[1], a[2], a[3], a[4], a[5])))
s = np.array([int((x - min(r))/(max(r) - min(r)) * 10) for x in r])

plt.bar(np.arange(len(s)), s)
plt.show()

enter image description here

2 Comments

Is it possible to do this without using matplotlib and numpy. Just plain python.
Is this a question? If so, I would say, yes theoretically it is possible, but it will cost you 4 days to write the code to do it. The good thing about libraries such as matplotlib and numpy is that you don't need to care about some of the basics and thus the code is very short. There are of course also other libraries for statistics and plotting around, which you may use instead.
0
your_values = [1, 2, 0.25, 15]
x = np.arange(len(your_values))

plt.bar(
    x, 
    your_values
)

Here is the example of the resulting bar plot

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

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.