要求 : 随着数据的更新 , 图表也能更新 ,并且可以自动在图表上添加标注 ,
当前已实现的程度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import sys import matplotlib.pyplot as plt import matplotlib.animation as animation import pymysql from scipy import stats import numpy as np import datetime import time x = datetime.datetime(2019,5,8,9,30) firstTop =0 doubletop=0 FuPanIndexBegin = 0 sellingPrice = 0 lowPrice = 0 lastStopWinPrice = 0 oporTime = 0 CurrentState = 0 # 0 mean observer , 1 selling , 2 buying Todaymin=0 Todaymax =0 conn=pymysql.connect(host='localhost',user='root',password='MYSQLTB',db='shfuture') a=conn.cursor() t=[] s=[] annotateIndexX=[] annotateIndexY=[] while True: sql = 'select happentime,lastprice from ' + sys.argv[1] + ' where happentime<=%s and hour(happentime)>=9 order by happentime desc limit 2;' a.execute(sql,x) data=a.fetchall() for result in data: t.append(result[0]) s.append(result[1]) x= x + datetime.timedelta(seconds=1) print(s) plt.plot(t, s) #print(t[-1]) #print(s[-1]) if s[-1]>3050: # 保存备注的坐标 #print(t.index(t[-1])) #print(s.index(s[-1])) #plt.annotate('Something', xy=(t[-1], s[-1])) annotateIndexX.append(t[-1]) annotateIndexY.append(s[-1]) #print(annotateIndex) for i in range(len(annotateIndexX)): # 试图加上备注 #print('i is ',i) plt.annotate('Something', xy=(annotateIndexX[i],annotateIndexY[i])) #plt.annotate('Something', xy=(t[-1],s[-1])) #plt.draw() #plt.pause(0.0001) plt.pause(3) plt.clf() |
功能是可以动态更新 ,也可以加标注, 保存之前的标注坐标在数组里
- 用到的函数
matplotlib.pyplot.
annotate
(s, xy, *args, **kwargs) : Annotate the point xy with text s.
示例代码
a) 在指定坐标加标注 https://jakevdp.github.io/PythonDataScienceHandbook/04.09-text-and-annotation.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x = np.linspace(0, 20, 1000) ax.plot(x, np.cos(x)) ax.axis('equal') ax.annotate('local maximum', xy=(6.28, 1), xytext=(10, 4), arrowprops=dict(facecolor='black', shrink=0.05)) ax.annotate('local minimum', xy=(5 * np.pi, -1), xytext=(2, -6), arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90")) plt.show() |
b)
2021-04-11 最新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import sys import matplotlib.pyplot as plt import matplotlib.animation as animation import pymysql from scipy import stats import numpy as np import datetime as dt import time from datetime import datetime, timedelta import matplotlib.dates as mdates # 设置x轴坐标用 plt.ion() class DynamicUpdate(): def on_launch(self): #Set up plot self.figure, self.ax = plt.subplots() # self.lines, = self.ax.plot([],[], 'o') # o 意思是用圆点画 self.lines, = self.ax.plot([],[]) # plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%M:%S')) #Autoscale on unknown axis and known lims on the other self.ax.set_autoscaley_on(True) def on_running(self, xdata, ydata): #Update data (with the new _and_ the old points) self.lines.set_xdata(xdata) self.lines.set_ydata(ydata) #Need both of these in order to rescale self.ax.relim() self.ax.autoscale_view() #We need to draw *and* flush self.figure.canvas.draw() self.figure.canvas.flush_events() #Example def __call__(self): import numpy as np import time self.on_launch() xdata = [] ydata = [] x = dt.datetime(2021,4,9,21,0) conn=pymysql.connect(host='localhost',user='root',password='MYSQLTB',db='shfuture') while True: a=conn.cursor() strdatetime = x.strftime('%Y-%m-%d %H:%M:%S') sql = 'select happentime,lastprice from oi2109_20210412 where happentime<="' + strdatetime + '" and hour(happentime)>=9 order by happentime desc limit 2;' a.execute(sql) data=a.fetchall() for result in data: xdata.append(result[0]) ydata.append(result[1]) x= x + dt.timedelta(seconds=1) self.on_running(xdata, ydata) # time.sleep(1) return xdata, ydata d = DynamicUpdate() d() |