Python 在matplotlib中的绘图轴上显示日期

Python 在matplotlib中的绘图轴上显示日期,python,matplotlib,Python,Matplotlib,我有个问题。我想在matplotlib中绘制一个图形。x轴包含日期,y轴高度以米为单位,高于see level。从我的readin方法中,我得到了 最小列表=[368.23000000000002, 368.12, 368.20999999999998, 368.35000000000002, 368.16000000000003, 367.98000000000002, 367.98000000000002, 367.97000000000003, 367.83999999999997, 36

我有个问题。我想在matplotlib中绘制一个图形。x轴包含日期,y轴高度以米为单位,高于see level。从我的readin方法中,我得到了

最小列表=[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
现在的问题是x轴没有显示日期。我无法单独找到我的错误,请帮助我

我希望我提供了足够的信息来帮助我解决这个问题


问候

好的,因此我在绘图程序中转换了日期。我有一个序列,其中的日期直接从csv文件中提取,因此日期最初看起来像2014年2月10日。我将该序列日期命名为。我的代码如下所示:

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]
plot(x1, minList, label='blau')
您必须在导入中添加一行以将datetime导入为dt,才能使其正常工作。然后,当您使用plot命令时,将其设置为:

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]
plot(x1, minList, label='blau')

这应该正确标记x轴,而不需要plt.xticks(年表),但如果您可以尝试使用和不使用。

好的,因此我在绘图程序中转换了日期。我有一个序列,其日期直接从csv文件中提取,因此日期最初看起来像2014年2月10日。我将该序列日期命名为。我的代码如下所示:

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]
plot(x1, minList, label='blau')
您必须在导入中添加一行以将datetime导入为dt,才能使其正常工作。然后,当您使用plot命令时,将其设置为:

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]
plot(x1, minList, label='blau')

这应该正确标记x轴,而不需要plt.xticks(年表),但如果您可以尝试使用或不使用。

x轴显示的是一个6位数的数字。不幸的是,我无法发布绘图x轴显示的是一个6位数的数字。不幸的是,我无法发布可以正常工作的绘图,是的!但问题是我希望列表的日期显示在x轴上。删除plt.xticks(yearList)使matplotlib自动缩放x轴,但这不是我需要的。嗯……我翻遍了我的绘图程序,看起来我做了类似于你的事情。主要区别在于我没有转换绘图程序之前的日期。我将用我如何编写绘图程序来更新我的答案,你可以看到这是否正确帮助。你的代码和我的方法一样!问题是我在x-achsis上得到了一个打印,看起来像721191。我用谷歌搜索了它,它打印了从公历或类似的东西上计数的数字。这很奇怪。对不起,我不知道为什么会这样做。在格式化日期之前,日期到底是什么样子的?好的,这是最后一件事我可以考虑试试。将行
plt.xticks(yearList)
替换为
plt.xticks(range(len(minList)),yearList)
并将行
plot(yearList,minList,label='blau')
更改为
plot(minList,label='blau'))
那会管用的,是的!但问题是我希望列表的日期显示在x轴上。删除plt.xticks(yearList)使matplotlib自动缩放x轴,但这不是我需要的。嗯……我翻遍了我的绘图程序,看起来我做了类似于你的事情。主要区别在于我没有转换绘图程序之前的日期。我将用我如何编写绘图程序来更新我的答案,你可以看到这是否正确帮助。你的代码和我的方法一样!问题是我在x-achsis上得到了一个打印,看起来像721191。我用谷歌搜索了它,它打印了从公历或类似的东西上计数的数字。这很奇怪。对不起,我不知道为什么会这样做。在格式化日期之前,日期到底是什么样子的?好的,这是最后一件事我可以考虑试试。将行
plt.xticks(yearList)
替换为
plt.xticks(range(len(minList)),yearList)
并将行
plot(yearList,minList,label='blau')
更改为
plot(minList,label='blau')