mardi 14 juin 2016

2 y-axis graph with x-axis as date and time . x value and y value data not showing on mouse point

I created 2 y-axises (y, y1) graph with x-axis as date and time. While I am pointing the graph y and x its showing me data of x and y1. Here is the code with proof:

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

Aucun commentaire:

Enregistrer un commentaire