我需要标记图表的最小值和最大值。我的代码只在其中一个图上标记它们。
import matplotlib.pyplot as my_graph
import matplotlib.dates as mdates
def several_tag(tags):
mmin=[]
mmax=[]
current_db=r.hget('db_id', 'value')
data=db_arch[current_db]
for counter, ctag in enumerate(tags):
vals=[]
times=[]
tag_values=data.find({'tagname':ctag},{'value':1,'time':1,'_id':0})
for tag in tag_values:
try:
vals.append(round(float(tag['value']),2))
except:
vals.append(0)
times.append(datetime.fromtimestamp(tag['time']))
mmin.append(min(vals))
mmax.append(max(vals))
my_graph.ylim(min(mmin)-5,max(mmax)+5)
my_graph.plot(times, vals)
def paint_x(vals):
my_graph.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
locator=mdates.ticker.FixedLocator([mdates.date2num(times[vals.index(min(vals))]),mdates.date2num(times[vals.index(max(vals))])])
my_graph.gca().xaxis.set_major_locator(locator)
paint_x(vals)
my_graph.xticks(size="xx-small", color="gray")
my_graph.annotate('max: %s' % max(vals), size="x-small", color="red", xy=(mdates.date2num(times[vals.index(max(vals))]), max(vals)), xycoords='data', xytext=(10, 10), textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))
my_graph.annotate('min: %s' % min(vals), size="x-small", color=(0.4, 0.5, 0.3), xy=(mdates.date2num(times[vals.index(min(vals))]), min(vals)), xycoords='data', xytext=(15, -15), textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))
my_graph.grid(True)
my_graph.savefig(r'1.png', transparent=True)
several_tag(['MKA50GE004ZQ_V', 'MKA50GE003ZQ_V'])
结果:http://rghost.net/48775269.view
如何在所有图表的时间轴上标记极值?