简体   繁体   中英

show date on axis of a plot in matplotlib

I have a question. I want to plot an graph in matplotlib. The x-axis contains dates and the y-axis heights in meters above see level. From my readin method i get.

minlist = [368.23000000000002, 368.12, 368.20999999999998, 368.35000000000002, 368.16000000000003, 367.98000000000002, 367.98000000000002, 367.97000000000003, 367.83999999999997, 367.63999999999999, 367.85000000000002, 367.56, 367.87, 367.99000000000001, 367.75, 367.88, 367.92000000000002, 367.81, 367.86000000000001, 367.95999999999998, 367.79000000000002, 367.67000000000002, 367.81999999999999, 367.91000000000003, 367.81999999999999, 367.81999999999999, 367.74000000000001, 367.80000000000001, 367.76999999999998, 367.85000000000002, 367.88999999999999, 367.76999999999998, 367.79000000000002, 367.86000000000001, 367.88999999999999, 367.86000000000001, 367.94999999999999, 367.94999999999999, 367.87, 367.91000000000003]

this list should have two digits after the comma.

Now i used a method to convert strings like '23.05.2012' to date.

def stringtodate(array):

i = 1
end = len(array)

date_format = []

while i <= end:

    string_date = str(array[i-1])

    time_tuple = datetime.strptime(string_date, "%d.%m.%Y")

    date_format.append(time_tuple)

    i = i+1

return date_format

that returns a list like that:

yearlist = [datetime.datetime(1975, 7, 22, 0, 0), datetime.datetime(1976, 11, 9, 0, 0), datetime.datetime(1977, 9, 23, 0, 0), datetime.datetime(1978, 7, 31, 0, 0), datetime.datetime(1979, 7, 31, 0, 0), datetime.datetime(1980, 9, 4, 0, 0), datetime.datetime(1981, 8, 27, 0, 0), datetime.datetime(1982, 7, 22, 0, 0), datetime.datetime(1983, 9, 16, 0, 0), datetime.datetime(1984, 8, 31, 0, 0), datetime.datetime(1985, 9, 27, 0, 0), datetime.datetime(1986, 9, 8, 0, 0), datetime.datetime(1987, 8, 4, 0, 0), datetime.datetime(1988, 8, 4, 0, 0), datetime.datetime(1989, 7, 18, 0, 0), datetime.datetime(1990, 7, 18, 0, 0), datetime.datetime(1991, 7, 31, 0, 0), datetime.datetime(1992, 7, 22, 0, 0), datetime.datetime(1993, 8, 25, 0, 0), datetime.datetime(1994, 8, 17, 0, 0), datetime.datetime(1995, 7, 25, 0, 0), datetime.datetime(1996, 8, 8, 0, 0), datetime.datetime(1997, 8, 7, 0, 0), datetime.datetime(1998, 8, 24, 0, 0), datetime.datetime(2000, 5, 31, 0, 0), datetime.datetime(2001, 8, 29, 0, 0), dateti me.datetime(2002, 8, 30, 0, 0), datetime.datetime(2003, 9, 18, 0, 0), datetime.datetime(2004, 10, 19, 0, 0), datetime.datetime(2005, 7, 7, 0, 0), datetime.datetime(2006, 5, 16, 0, 0), datetime.datetime(2007, 9, 19, 0, 0), datetime.datetime(2008, 5, 13, 0, 0), datetime.datetime(2009, 11, 24, 0, 0), datetime.datetime(2010, 10, 18, 0, 0), datetime.datetime(2011, 5, 3, 0, 0), datetime.datetime(2013, 1, 8, 0, 0), datetime.datetime(2013, 11, 19, 0, 0), datetime.datetime(2014, 8, 27, 0, 0), datetime.datetime(2014, 12, 1, 0, 0)]

now i want to plot these two lists with matplotlib. My code is:

def plotminimum(minList,yearList):

print(minList)
print(yearList)

try:

    plt.xlabel('Datum')
    plt.ylabel('minimale Sohle [m.u.A]')


    plt.xticks(yearList)
    plt.xticks(rotation=70)

    plot(yearList,minList, label='blau')

    show()

except:

    print('Profil nicht vorhanden!')

The problem now is the x-axis doesnt show the date. I cant find my mistake alone, please help me.

I hope i provided you with enough information to help me solve the problem.

greetings

Ok so I converted the date within the graphing program. I had a series with the dates pulled straight from a csv file, so the dates originally looked like 10/2/2014. I named that series date. My code looks like this:

date = data['Date'] # data is just the variable I am using to hold the csv file. 
# The above line is just pulling the date column into it's own variable.

x1 = [dt.datetime.strptime(d, '%m/%d/%Y').date() for d in date]

You will have to add a line in your imports to import datetime as dt for this to work. Then, when you use the plot command, set it up like this:

plot(x1, minList, label='blau')

This should properly label the x-axis without the need for the plt.xticks(yearList), but if you can try it with and without.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM