繁体   English   中英

在Matplot Python中格式化日期时间

[英]Formatting datetime in matplot Python

我在调整日期时间时遇到问题,无法以更好的方式在图表中显示。 这是我的代码:

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()

我试图添加该库并在斧头提名中:

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)

在此处输入图片说明

你可以尝试旋转标签添加paramenter labelrotation https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html

plt.tick_params(axis="both", which="both", bottom="off", top="off",    
                        labelbottom="on", left="off", right="off", 
                        labelleft="on", labelrotation=45)

我只是添加了以下行,它是自动完成的

plt.gcf().autofmt_xdate()   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM