Plot Markers on Curve where Value of X is known in matplotlib

此生再无相见时 提交于 2021-01-27 21:09:24

问题


I plotted a curve w.r.t time-series from the data which I got from an experiment. Data is collected at 10ms interval. Data is single row array.

I also have calculated an array which contains the time at which a certain device is triggered. I drew axvlines of these triggered locations.

Now I want to show markers where my curve crosses these axvlines. How can I do it?

Time of trigger (X- is known). Curve is drawn but don't have any equation (irregular experiment data). Trigger interval is also not always the same.

Thanks.

p.s - I also use multiple parasite axes on figure too. Not that it really matters but just in case.

Want Markers On Curve Where AXVline Crosses


回答1:


You can use numpy.interp() to interpolate the data.

import numpy as np
import matplotlib.pyplot as plt

trig = np.array([0.4,1.3,2.1])
time = np.linspace(0,3,9)
signal = np.sin(time)+1.3

fig, ax = plt.subplots()
ax.plot(time, signal)
for x in trig:
    ax.axvline(x, color="limegreen")
#interpolate:
y = np.interp(trig, time, signal)
ax.plot(trig, y, ls="", marker="*", ms=15,  color="crimson")

plt.show()



来源:https://stackoverflow.com/questions/43930954/plot-markers-on-curve-where-value-of-x-is-known-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!