How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?

前端 未结 3 1562
孤街浪徒
孤街浪徒 2020-12-08 15:04

I use Python 2.7 and matplotlib. I have a *.txt data file :

0 14-11-2003
1 15-03-1999
12 04-12-2012
33 09-05-2007
44 16-08-1998
55 25-07-2001
76 31-12-2011
8         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 15:27

    First, what you are looking for is a column or bar diagram, not really a histogram. A histogram is made from a frequency distribution of a continuous variable that is separated into bins. Here you have a column against separate labels.

    To make a bar diagram with matplotlib, use the matplotlib.pyplot.bar() method. Have a look at this page of the matplotlib documentation that explains very well with examples and source code how to do it.

    If it is possible though, I would just suggest that for a simple task like this if you could avoid writing code that would be better. If you have any spreadsheet program this should be a piece of cake because that's exactly what they are for, and you won't have to 'reinvent the wheel'. The following is the plot of your data in Excel:

    Bar diagram plot in Excel

    I just copied your data from the question, used the text import wizard to put it in two columns, then I inserted a column diagram.

提交回复
热议问题