0

I have an array of numbers and i have an array which has the value of how many time element of numbers is used. I have to create histogram plot which show how many times element from numbers list is used. I know that histogram plot shows distribution but i didnt get the logic of bining.

import sys, tkinter
import matplotlib.pyplot as plt

numbers=['23.7', '23.718', '23.73', '23.7225', '23.754', '23.76', '23.736', '23.745', '23.6','23.64','23.65','23.625','23.6166666666667','23.6666666666667', '23.575', '23.54', '23.525', '23.5', '23.445', '23.39', '23.37', '23.3733333333333', '23.3233333333333', '23.365', '23.29', '23.3066666666667', '23.272', '23.2', '23.23', '23.215', '23.14', '23.15', '23.18', '23.1166666666667', '23.1', '23.06', '23.02', '23.01', '23', '22.9816666666667', '22.945', '22.956', '22.9725', '22.912', '22.89', '22.865', '22.8733333333333', '22.815', '22.84', '22.79', '22.772', '22.76'']
how_many_times=[2, 1, 2, 2, 1, 3, 1, 2, 1, 1, 3, 1, 2, 1, 1, 1, 3, 1, 4, 2, 3, 1, 5, 1, 1, 1, 1, 10, 1, 1, 2, 1, 2, 1, 1, 19, 4, 2, 2, 2, 3, 1, 32, 2, 2, 6, 1, 2, 1, 1, 17, 3, 4, 6, 2, 3, 7, 44, 1, 5, 3,
 1, 6, 3, 2, 3, 3, 2, 56, 1, 7, 6, 1, 16, 6, 18, 3, 4, 75, 1, 7, 6, 10, 1, 3,]

numbers=[float(x) for x in numbers]
plt.hist(numbers,bins, histtype='bar',rwidth=0.08)
plt.xlabel('Numbers')
plt.legend()
plt.show()

This code doesn't show the correct bins and numbers.How can I fix this?

Beyza
  • 53
  • 8
  • If you already know how often a value occurs, you do not want a `hist` plot at all. You may use a bar plot. Now it is not clear from the question whether you want one bar for each number in `numbers` or whether you want to bin the numbers again to plot them to scale on an axis. – ImportanceOfBeingErnest Nov 28 '17 at 18:53
  • @ImportanceOfBeingErnest I understand what you mean, but if i didnt have the how_may_time array . how does the python know the distribution? – Beyza Nov 28 '17 at 18:57
  • It will look how often a value in the input list occurs inside a bin. It is the same as when you compute a histogram by hand. You have a list of bins and look for each number in which of those bins it would fall. – ImportanceOfBeingErnest Nov 28 '17 at 19:02
  • @ImportanceOfBeingErnest for example if i have a list a=[a,a,a,a,b,b,b,c,c] would it know there is 4 of a, 3 of b, 2 of c? – Beyza Nov 28 '17 at 19:12
  • If a,b and c are numbers, it would know how many of them are in some bins. If they are discreate objects, you would not use a histogram at all, but possibly a bincount on the indices. – ImportanceOfBeingErnest Nov 28 '17 at 19:19
  • @ImportanceOfBeingErnest so there should be floats or integers in numbers list and in bins as well? not string – Beyza Nov 28 '17 at 19:22
  • It really depends on what you want to achieve. Usually you do not have the choice of selecting your data. So given some data, what do you want to show? – ImportanceOfBeingErnest Nov 28 '17 at 19:25
  • @ImportanceOfBeingErnest temperature which is given as string and how many times same temperature occured. – Beyza Nov 28 '17 at 19:28
  • So would 23.987987423°C be the same as 23.987987623°C? – ImportanceOfBeingErnest Nov 28 '17 at 19:29
  • first of all, is that your actual code? because as is, you have an extra string quote that would throw an error. also your x in histogram should be "how_many_times" and your bins should be "numbers" that will give you what you want. – jimh Nov 28 '17 at 20:41

0 Answers0