0

I want to plot a histogram with matploblib in Python, but my code doesn't seem to work. I imported data from a textfile with the following code:

vragenlijst_data= np.genfromtxt('antwoorden.txt', delimiter=',', dtype=None, names=('geslacht', 'leeftijd', 'stelling1', 'doorvraag1', 'stelling2', 'stelling3', 'doorvraag3', 'opmerking'))

Now my data looks like this:

[ ("'vrouw'", 43, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 34, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 32, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 32, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 43, " '3'", " 'sport'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 32, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " ''")
 ("'vrouw'", 43, " '2'", " 'onbeantwoord'", " '3'", " '3'", " 'collega'", " 'nee'")
 ("'vrouw'", 27, " '3'", " 'in alle'", " '3'", " '4'", " '3'", " ''")
 ("'vrouw'", 34, " '4'", " 'prive en werk'", " '3'", " '2'", " 'onbeantwoord'", " 'fghfgh'")
 ("'vrouw'", 32, " '2'", " 'onbeantwoord'", " '2'", " '2'", " 'onbeantwoord'", " 'k'")
 ("'vrouw'", 46, " '4'", " 'werk'", " '4'", " '4'", " 'werk'", " 'nee'")]

And I want to plot a histogram with counts of answers in matplotlib. For example, number of vrouw/man and number of times that answer 1, 2, 3, 4, or 5 occurs. However, it seems only possible to plot a histogram with x and y values. I there any way to do this anyway?

user3483203
  • 50,081
  • 9
  • 65
  • 94
ItK
  • 63
  • 1
  • 7
  • 1
    What exactly is the problem? I suppose you created a list of answers and counts and want to plot a bar plot? In how far does this not work? – ImportanceOfBeingErnest Jun 10 '18 at 23:51
  • I tried several things, for example, I used geslacht = vragenlijst_data['geslacht'] plt.hist(geslacht), and it gives an error TypeError: cannot perform reduce with flexible type – ItK Jun 11 '18 at 00:02
  • A `hist` is a frequency representation of numerical data. Here you have categorical data. You cannot use `hist`, but you anyways want to plot a `bar` plot right? – ImportanceOfBeingErnest Jun 11 '18 at 00:09
  • Yes, a bar plot would be perfect – ItK Jun 11 '18 at 00:10
  • So what hinders you in plotting a bar plot? (Note that comments here are meant for you to update your question such that it can be answered.) – ImportanceOfBeingErnest Jun 11 '18 at 00:12
  • When I use geslacht = vragenlijst_data['geslacht'] plt.bar(geslacht) it gives TypeError: bar() takes at least 2 arguments (1 given). I assume it asks for frequencies of the variables, but I don't know how to do that. – ItK Jun 11 '18 at 00:20

0 Answers0