Heres my code:
import matplotlib.pyplot as plt
import pandas as pd
import urllib
API = 'http://api.example.com'
data = urllib.request.urlopen(API)
df = pd.DataFrame(data)
x = df['x'].tolist()
y1 = df['y1'].tolist()
y2 = df['y2'].tolist()
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.plot(x, y1, label='y1')
ax1.plot(x, y2, label='y2')
plt.show()
So basically I've plotted two lines:
ax1.plot(x, y1, label='y1')
ax1.plot(x, y2, label='y2')
Assuming that the chart is live and updating. I want python to print a warning message whenever the lines meet, with another condition that it will only print a warning message for 100 times.
Here's what I got so far:
I defined the warning_message function to print a warning message.
def warning_message():
print("Warning!")
I've also done the other condition wherein it will only print a warning message for 100 times.
count = 100
while count != '0':
warning_message()
count -= 1
What's missing inside this while loop is the condition that it will only print the warning message whenever the lines meet. Does anyone know how do it? :D
Since the lines share the same x data, it's rather easy to compare their y data.
Simply asking whether y2 > y1
is enough.
import numpy as np
y1 = np.array([1,2,3])
y2 = np.array([0,1,3.1])
if np.any(y2>y1):
print("warning")
In order to make this work in your script, don't use .tolist()
but keep the data as numpy arrays, i.e. y1 = df['y1'].values
.
import numpy as np
y1 = np.array([1,2,3])
y2 = np.array([0,1,2.9])+1
c = y2>y1
if (np.any(c) and not np.all(c)) or (np.any(~c) and not np.all(~c)):
print("warning")
if np.all(c) or np.all(~c):
#reset counter here
pass