Python 不在轴上写出所有日期,Matplotlib
看看这个例子:Python 不在轴上写出所有日期,Matplotlib,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,看看这个例子: import datetime as dt from matplotlib import pyplot as plt import matplotlib.dates as mdates x = [] d = dt.datetime(2013, 7, 4) for i in range(30): d = d+dt.timedelta(days=1) x.append(d) y = range(len(x)) plt.gca().xaxis.set
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
x = []
d = dt.datetime(2013, 7, 4)
for i in range(30):
d = d+dt.timedelta(days=1)
x.append(d)
y = range(len(x))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gcf().autofmt_xdate()
plt.bar(x,y)
plt.show()
代码在绘图的x轴上写出日期,见下图。问题是日期被阻塞了,如图所示。如何使matplotlib只写出五分之一或十分之一坐标
您可以为
日期定位器指定间隔
参数,如下所示。例如,interval=5
定位器在每5个日期放置刻度。另外,将autofmtxdate()
放在bar
方法之后,以获得所需的输出
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
x = []
d = dt.datetime(2013, 7, 4)
for i in range(30):
d = d+dt.timedelta(days=1)
x.append(d)
y = range(len(x))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5))
plt.bar(x, y, align='center') # center the bars on their x-values
plt.title('DateLocator with interval=5')
plt.gcf().autofmt_xdate()
plt.show()
使用interval=3
时,您每第三次约会就会得到一个勾号:
将日期标签旋转90度怎么样plt.xticks(rotation=90)
。我们能定义开始日期和结束日期吗?对我有用!花了这么多时间想弄清楚。谢谢!