I have a plot that my code is generating, example below:
I am trying to find a way in which I can freely move the red line across the axis so it slides up and down, but so far I haven't found any guidance on how to do that, any advise would be appreciated, the code that generates the figure is below:
plt.figure()
plt.axhline(y = frame_threshold, color = 'r', linestyle = '-')
plt.plot(framenumber, Avfiltered)
CodePudding user response:
Internally, the horizontal line is represented by x-values 0 and 1 and both corresponding y-values set to the given y-value. A "y axis transform" is used to tell that x should be interpreted as "axis coordinates", and y as "data coordinates". The matplotlib tutorial contains more information about coordinates and transforms.
You can update the y-data of the horizontal line to have it at another position. The following example code uses a click event to demonstrate such an update.
import matplotlib.pyplot as plt
import numpy as np
def on_click(event):
global hor_line
if event.inaxes:
# hor_line.set_data(([0, 1], [event.ydata, event.ydata]))
hor_line.set_ydata([event.ydata] * 2)
plt.draw()
frame_threshold = 5
plt.figure()
hor_line = plt.axhline(y=frame_threshold, color='r', linestyle='-')
plt.plot(np.arange(200), np.random.normal(0.1, 1, 200).cumsum())
plt.connect('button_press_event', on_click)
plt.show()