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

前端 未结 3 1561
孤街浪徒
孤街浪徒 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:38

    You're talking about histograms, but this doesn't quite make sense. Histograms and bar charts are different things. An histogram would be a bar chart representing the sum of values per year, for example. Here, you just seem to be after bars.

    Here is a complete example from your data that shows a bar of for each required value at each date:

    import pylab as pl
    import datetime
    
    data = """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
    87 25-06-1993
    118 16-02-1995
    119 10-02-1981
    145 03-05-2014"""
    
    values = []
    dates = []
    
    for line in data.split("\n"):
        x, y = line.split()
        values.append(int(x))
        dates.append(datetime.datetime.strptime(y, "%d-%m-%Y").date())
    
    fig = pl.figure()
    ax = pl.subplot(111)
    ax.bar(dates, values, width=100)
    ax.xaxis_date()
    

    You need to parse the date with strptime and set the x-axis to use dates (as described in this answer).

    If you're not interested in having the x-axis show a linear time scale, but just want bars with labels, you can do this instead:

    fig = pl.figure()
    ax = pl.subplot(111)
    ax.bar(range(len(dates)), values)
    

    EDIT: Following comments, for all the ticks, and for them to be centred, pass the range to set_ticks (and move them by half the bar width):

    fig = pl.figure()
    ax = pl.subplot(111)
    width=0.8
    ax.bar(range(len(dates)), values, width=width)
    ax.set_xticks(np.arange(len(dates)) + width/2)
    ax.set_xticklabels(dates, rotation=90)
    

提交回复
热议问题