matplotlib x轴弄乱了

时间:2019-06-20 21:32:02

标签: python-3.x datetime matplotlib

我正在尝试从CSV文件中解析某些日期,格式为“ 2014-07-01”。然后我想把它放在图的x轴上

下面是我的代码。一切都很好,如第一个图像所示。

import csv
from matplotlib import pyplot as plt
from datetime import datetime

# Get high temperatures from file.
filename = 'sitka_weather_07-2014.csv'

with open(filename) as f:
    reader = csv.reader(f)

    highs = []
    dates = []
    for row in reader:
        high = int(row[1])
        highs.append(high)
        date = datetime.strptime(row[0], '%Y-%m-%d')
        dates.append(date)

#Plot data
fig = plt.figure(dpi=128, figsize = (10,6))
plt.plot(dates, highs, c='red')

#Format plot
plt.title("Daily high temperatures, July 2014", fontsize = 24)
plt.xlabel('', fontsize=16)

#draws the date labels diagonally to prevent them from overlapping
fig.autofmt_xdate()

plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()

但是当我尝试使用strftime()方法重新设置日期格式时,x轴被弄乱了,如第二幅图所示。我所做的唯一更新如下:

date = datetime.strptime(row[0], '%Y-%m-%d')


date = datetime.strptime(row[0], '%Y-%m-%d').strftime('%Y/%m/%d')

任何人都在想哪里出了什么问题?任何帮助表示赞赏!

First image

Second image

0 个答案:

没有答案