Python: display data from txt file

How to build a histogram of this type of data,

10 apples 3 oranges 6 tomatoes 10 pears 

from a text file?

thanks

+4
source share
3 answers

Here you can assign different colors to the bars. It works even with 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

+6
source

As others say, Matplotlib is your friend. Sort of

 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() 

you will start. Loading data from a text file is straightforward.

+2
source

Felix is ​​right.

Matplotlib is one of the available paid locations. Take a look, he has many examples. If you cannot draw a histogram, then you can ask another question, and I'm sure there will be many people to help.

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

+1
source

All Articles