import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = pd.date_range(start='2020/01/01', end='2020/01/03', freq='D')
y = [1, 2, 3]
fig = plt.figure()
ax = fig.add_subplot(111)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gcf().autofmt_xdate() # 自动旋转日期
ax.plot(x, y)
# # ymin = -40
# # ymax = 60
# # ax1.set_ylim([ymin, ymax]) 设置y轴坐标范围
# # xmin = 0
# # xmax = 96
# # ax1.set_xlim([xmin, xmax]) 设置x轴坐标范围
# # ax1.xaxis.set_major_locator(MultipleLocator(12)) # 设置横坐标间隔
plt.show()
【记录】matplotlib.pyplot画图并设置横坐标为日期格式
最新推荐文章于 2023-04-21 17:05:07 发布