【发布时间】:2018-09-12 02:08:22
【问题描述】:
我是这个领域的新手,我正在使用 matplotlib 一段时间。
这是我的问题:正如你所看到的,当我有两个或多个主要刻度a1.plot(date2, closep2) 时,完美没问题,但只有一个主要刻度a1.plot(date1, closep1) 我没有小刻度。
有人可以帮忙吗?我将不胜感激。
import matplotlib.pylab as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
import datetime as dt
from random import randint
fig = plt.figure
a1 = plt.subplot(111)
d= 1536596330 #just an starting time for my sample data
date1= []
date2= []
closep1 = []
closep2 = []
# Not the best way but making some lists to plot :
# Date1 with 48 dates and 30 minutes interval , some random prices
for hh in range (0, 48) :
dates= dt.datetime.fromtimestamp(int(d+ (hh*1800)))
date1.append(dates)
closep1.append(randint(1,9))
# Date2 with 72 dates and 30 minutes interval , some random prices
for hh in range (0, 72) :
dates= dt.datetime.fromtimestamp(int(d+ (hh*1800)))
date2.append(dates)
closep2.append(randint(1,9))
# Here comes trouble :
a1.plot(date1, closep1)
# No problem with this one :
# a1.plot(date2, closep2)
a1.xaxis.set_major_locator(mdates.DayLocator())
a1.xaxis.set_major_formatter(mdates.DateFormatter('%b/%d'))
a1.xaxis.set_minor_locator(mticker.AutoMinorLocator(6))
a1.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))
plt.setp(a1.xaxis.get_majorticklabels(),rotation=45, fontsize=10,color='k')
plt.setp(a1.xaxis.get_minorticklabels(),rotation=45, fontsize=7,color='r')
plt.show()
【问题讨论】:
-
6inAutoMinorLocator(6)表示将主要刻度之间的区域划分为 6 个相等的部分,从而产生 5 个次要刻度。当你有一个主要刻度时,我认为它找不到下一个主要刻度来划分区域,因此没有次要刻度 -
@Bazingga,谢谢你的回答。事实上,即使
AutoMinorLocator(24)或任何其他不起作用,但mdates.DayLocator(1)将从第一天作为主要 1 开始情节,并以第二天作为主要 2 结束,还有未成年人出现,但还不够好 -
如果只有一个专业,那么选择 6 或 16 或 24 或 100 都没有关系……我下面的解决方案不适合你吗?
标签: matplotlib