我正在尝试使用matplotlib创建一个图形,该图形将日期 - 从文件中拉出 - 作为主要标记,小时作为次要标记。到目前为止,我所拥有的代码即使文本全部成束 - 我现在也不关心它。我的问题是,如果我要缩放或平移,则滴答及其各自的值不会更新。例如,如果我要缩放到一天,所有日子仍然显示在x轴上。我使用Spyder作为我的IDE和Python 2.7。另外,我应该抛出这个:我是Python和编程的新手。
hticks = []
tickcnt = 0
for i in range(len(MBTempvalue)):
mcnt = mcnt + 1
cnt = cnt + 1
if mcnt == 60:
mcnt = 0
harray.append(str(hcnt))
hcnt = hcnt + 1
tickcnt = tickcnt + 1
hticks.append(tickcnt)
if hcnt == 24:
hcnt = 0
if cnt == 1375:
cnt = 0
days = days + 1
hours = days * 24
fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(211)
xmax = float(len(PS0AmbientTempvalue))
dx = hours/xmax
y = PS0AmbientTempvalue
y2 = PS1AmbientTempvalue
x = np.arange(0,hours, dx)
plt.xticks(darray,date)
ax.xaxis.set_major_locator(LinearLocator(len(darray)))
minorLocator = AutoMinorLocator(23)
ax.xaxis.set_minor_locator(minorLocator)
minorformatter = FixedFormatter(harray)
ax.xaxis.set_minor_formatter(minorformatter)
l1, l2, l3, l4 = ax.plot(x,y, 'b', x, PS0AmbientTempavgline, 'r', x, y2, 'y', x, PS1AmbientTempavgline, 'g')
#plt.xlabel('Hours')
plt.ylabel('Temp - C')
plt.title(' PS0/T_AMB and PS1/T_AMB')
plt.axis([0, hours, (minscale1-5), (maxscale1+5)])
plt.grid(True)
fig.legend((l1,l2,l3,l4), ('PS0 Act', 'PS0 Avg', 'PS1 Act', 'PS1 Avg'), 'upper right')
plt.annotate(('PS0 Max = ' + PS0AmbientTempstrmax), (0,0), (0, -70), xycoords = 'axes fraction', textcoords = 'offset points', va ='bottom')
plt.annotate(('PS0 Min = ' + PS0AmbientTempstrmin), (0,0), (0, -90), xycoords = 'axes fraction', textcoords = 'offset points', va = 'bottom')
plt.annotate(('PS0 Avg = ' + PS0AmbientTempstravg), (0,0), (0, -110), xycoords = 'axes fraction', textcoords = 'offset points', va ='bottom')
plt.annotate(('PS0 Stand Dev = ' + PS0AmbientTempstrstandev), (0,0), (0, -130), xycoords = 'axes fraction', textcoords = 'offset points', va ='bottom')
plt.annotate(('PS 1Max = '+ PS1AmbientTempstrmax), (0,0), (8*hours, -70), xycoords = 'axes fraction', textcoords = 'offset points', va = 'bottom')
plt.annotate(('PS1 Min = ' + PS1AmbientTempstrmin), (0,0), (8*hours, -90), xycoords = 'axes fraction', textcoords = 'offset points', va = 'bottom')
plt.annotate(('PS1 Avg = ' + PS1AmbientTempstravg), (0,0), (8*hours, -110), xycoords = 'axes fraction', textcoords = 'offset points', va = 'bottom')
plt.annotate(('PS1 Stand Dev = ' + PS1AmbientTempstrstandev), (0,0), (8*hours, -130), xycoords = 'axes fraction', textcoords = 'offset points', va = 'bottom')
任何帮助都将非常感激。 谢谢! 克里斯
答案 0 :(得分:2)
他们不更新的原因是您正在使用FixedFormatter
,正如其名称所示,已修复。它的主要用途是将文本标签放在刻度线上(例如制作条形图,其中x轴是定性的而不是定量的)。 mpl正在做你告诉它的事情。
matplotlib可以直接绘制datetime
对象,这可能就是你想要做的。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
numdays = 5
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(hours=x) for x in range(0, numdays*24)]
data = np.random.rand(len(date_list))
fig, ax = plt.subplots(1, 1)
ax.plot(date_list, data)
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.xaxis.set_minor_locator(mdates.HourLocator())
fig.autofmt_xdate()
plt.draw()
plt.show()
答案 1 :(得分:0)
非常感谢tcaswell的帮助!没有你的帮助,我无法解决我的问题!以下是我使用的代码:
def kuVpolFunc2(kuvdate, kuvit, kuviti, kuvfp, kuvfpi, faultflag):
import matplotlib.pyplot as plt
import numpy as np
import re
import matplotlib.dates as mdates
import datetime
from datetime import timedelta
kutempvalue = kuvit
kutempinter = kuviti
kufpvalue = kuvfp
kufpinter = kuvfpi
faultflagit = faultflag
date = kuvdate
datelist = []
#I first had to decontruct the filename pulled from the directory to put it in a format that Python could then create a datetime object.
for n in date:
month = n[:3]
datenum = re.sub("[^0-9]","", n)
day = str(datenum[:-4])
year = str(datenum[2:])
print ("Year: " + year)
print ("Month: " + month)
print ("Day: " + day)
alltogether = str(month + ' ' + day + ' ' + year)
dateobject = datetime.datetime.strptime(alltogether, '%b %d %Y')
datelist.append(dateobject)
start = min(datelist)
dlist = [start + timedelta(minutes = x) for x in range(0, len(datelist) *1440)]
#The 'add' variable is used to make sure that the dlist's dimension is the same as the data. It isn't posted here for brevity's sake, but I can post it if someone wants it.
add = max(dlist)
fig, ax = plt.subplots(1,1)
l1, l2, = ax.plot(dlist, y, 'b', dlist, kutempavgline, 'r')
ax.xaxis.set_major_locator(mdates.DayLocator())
plt.title('Ku_Vpol Internal Temp')
plt.ylabel('Temp - C')
fig.legend((l1,l2), ('Internal Temp Actual Temp', 'Internal Temp Average Temp'), 'upper right')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.xaxis.set_minor_locator(mdates.HourLocator())
plt.grid(True)
fig.autofmt_xdate()
plt.draw()
plt.show()