在matplot Python中格式化日期时间

问题描述 投票:0回答:2

我有问题调整日期时间以更好的方式在我的图表中可视化。这是我的代码:

fig = plt.figure()
new.plot(title='(Graph)',figsize=(10,7), legend=None)

plt.tick_params(axis="both", which="both", bottom="off", top="off",    
                            labelbottom="on", left="off", right="off", labelleft="on")   
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
ax = plt.subplot(111)   
ax.spines["top"].set_visible(False)    
ax.spines["bottom"].set_visible(True)    
ax.spines["right"].set_visible(False)    
ax.spines["left"].set_visible(False)    
ax.get_xaxis().tick_bottom()    
ax.get_yaxis().tick_left()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
pp.savefig(bbox_inches='tight')
pp.close()

我试图添加这个库并在ax提名中:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

enter image description here

python pandas matplotlib graph
2个回答
1
投票

您可以尝试旋转标签添加参数qazxsw poi qazxsw poi:

labelrotation

0
投票

我刚刚添加了以下行,它自动完成

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html
© www.soinside.com 2019 - 2024. All rights reserved.