4

How do I plot a histogram of this kind of data,

10 apples
3 oranges
6 tomatoes
10 pears

from a text file?

thanks

1
  • Have a look at matplotlib. Commented Sep 29, 2010 at 12:06

3 Answers 3

6

Here's one way you can assign different colors to the bars. It works with even a variable number of bars.

import numpy as np
import pylab
import matplotlib.cm as cm

arr = np.genfromtxt('data', dtype=None)
n = len(arr)
centers = np.arange(n)
colors = cm.RdYlBu(np.linspace(0, 1, n))
pylab.bar(centers, arr['f0'], color=colors, align='center')
ax = pylab.gca()
ax.set_xticks(centers)
ax.set_xticklabels(arr['f1'], rotation=0)
pylab.show()

bar chart

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

4 Comments

I'm pretty sure that you can do this without having to handle the x-positioning (sep, width, left, centers). In fact, you can simply use range(n) for set_xticks(), and use the align='center' parameter for bar().
Glad you found the remark useful. Here is another remark: it is easier to set dtype=None in genfromtxt(): the data types are correctly inferred; you also get, as a free bonus, a bullet-proof code against fruit names that are longer than 20 characters. :)
@EOL: Wow, I didn't know dtype=None behaved different than omitting dtype. That's nice to know. Thanks again.
Yeah, this surprised me too, when I checked the docs. :) The default is dtype=float (purely numerical arrays).
2

As the others suggest, Matplotlib is your friend. Something like

import numpy as np
import matplotlib.pyplot as plt

plt.figure()
indices = np.arange(4)
width = 0.5
plt.bar(indices, [10, 3, 6, 10], width=width)
plt.xticks(indices + width/2, ('Apples', 'Oranges', 'Tomatoes', 'Pears'))
plt.show()

will get you started. Loading the data from a textfile is straight forward.

Comments

1

Felix is right.

Matplotlib is one of the tolls available. Take a look, it has lot of examples. If you're not able to draw a histogram then you could ask another question and I'm sure there will be lots of people to help.

Here are some examples:
http://matplotlib.sourceforge.net/examples/pylab_examples/histogram_demo_extended.html

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.