我是这个领域的新手,并且我正在与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()
答案 0 :(得分:0)
这是一个廉价的技巧。我只显示修改后的代码。
for hh in range(0, 58): # Increased the range
dates= dt.datetime.fromtimestamp(int(d+ (hh*1800)))
date1.append(dates)
closep1.append(randint(1,9))
# Plotting the complete range in white color to get the major ticks
a1.plot(date1, closep1, color='white')
a1.plot(date1[0:48], closep1[0:48]) # Plotting the wanted data only up to 48 in color
输出