我有一个问题。我想在matplotlib中绘制一个图形。 x轴包含日期和y轴高度(以米为单位)。从我的readin方法我得到。
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]
这个列表在逗号后应该有两位数。
现在我使用了一种方法来转换字符串,如' 23.05.2012'到目前为止。
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
返回类似的列表:
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),dat etime.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)]
现在我想用matplotlib绘制这两个列表。我的代码是:
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!')
现在的问题是x轴没有显示日期。我不能单独找到我的错误,请帮助我。
我希望我能为您提供足够的信息来帮助我解决问题。
问候
答案 0 :(得分:0)
好的,所以我在图表程序中转换了日期。我有一个系列日期从csv文件直接拉出日期,所以日期最初看起来像10/2/2014。我将该系列日期命名为。我的代码如下所示:
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]
您必须在导入中添加一行以将日期时间导入为dt才能生效。然后,当您使用plot命令时,将其设置如下:
plot(x1, minList, label='blau')
这应该正确地标记x轴而不需要plt.xticks(yearList),但是如果你可以使用和不使用它。