2个y轴图表,x轴为日期和时间。 x值和y值数据未显示在鼠标点上

时间:2016-06-14 11:11:32

标签: python python-3.x matplotlib

我创建了2个y轴(y, y1)图形,x轴为日期和时间。当我指向图表yx时,它会显示xy1的数据。这是带证明的代码:

import sqlite3,time,datetime,os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
import matplotlib.dates as mdates
from matplotlib import style
style.use('ggplot')

def bytedate2num(fmt):
    def converter(b):
        return mdates.strpdate2num(fmt)(b.decode('ascii'))
    return converter
conn = sqlite3.connect("Datas.db")
c = conn.cursor()
sql= "SELECT timestap,date,val,num FROM table"
graphArray = []
temp=0
for row in c.execute(sql):
    if temp > int(row[0]):
        continue
    else:
        temp=int(row[0])
    graphArray.append(row[1]+','+row[2]+','+row[3])

date_converter = bytedate2num("%Y-%m-%d %H:%M:%S")
date_Formatter = dates.DateFormatter('%m/%d %H:%M')
datestime, val, num= np.loadtxt(graphArray,delimiter=',', unpack=True,converters={ 0: date_converter})
fig = plt.figure()
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 15
fig_size[1] = 12
fig, ax1 = plt.subplots()
ax1.plot_date(datestime, val, 'g-')
ax1.xaxis.set_major_formatter(date_Formatter)
plt.xticks(rotation=45)
for tl in ax1.get_yticklabels():
    tl.set_color('g')
ax2 = ax1.twinx()
ax2.plot_date(datestime, num, 'r-')
ax2.yaxis.set_label_position("right")
for tl in ax2.get_yticklabels():
    tl.set_color('r')
fig.autofmt_xdate()
plt.show() 

enter image description here enter image description here

  

样本数据

     

timestamp date num val

     

1461685551626 2016-04-26 15:45:51 20.884 5

     

1461685572550 2016-04-26 15:46:12 1.009 5

     

1461685573560 2016-04-26 15:46:13 0.09 5

     

1461685551140 2016-04-26 15:45:51 22.24 5

     

1461685578914 2016-04-26 15:46:18 1.061 7

     

1461685573914 2016-04-26 15:46:13 1.061 7

     

1461685560734 2016-04-26 15:46:00 20.849 7

     

1461685581587 2016-04-26 15:46:21 0.736 7

     

1461685582334 2016-04-26 15:46:22 0.058 7

     

1461685582393 2016-04-26 15:46:22 0.057 7

0 个答案:

没有答案