I have a curve of some data that I am plotting using matplotlib. The small value x-range of the data consists entirely of NaN values, so that my curve starts abruptly at some value of x>>0 (which is not necessarily the same value for different data sets I have). I would like to place a vertical dashed line where the curve begins, extending from the curve, to the x axis. Can anyone advise how I could do this? Thanks
3 Answers
Assuming you know where the curve begins, you can just use:
plt.plot((x1, x2), (y1, y2), 'r-') to draw the line from the point (x1, y1) to the point (x2, y2)
Here in your case, x1 and x2 will be same, only y1 and y2 should change, as it is a straight vertical line that you want.
2 Comments
for loop to find the exact position of the last NaN in your array. Then assuming your array is called x, you can use x[12] assuming that the last NaN value was found at the 12th position in your arrayAs an alternative, you can also use plt.vlines to draw a vertical line and provided that your data is in a numpy array and not a list, you can skip the for loop to determine the first non-NaN value and make use of np.isfinite instead. Something like:
x_value = data_array[np.where(np.isfinite(data_array))[0]]
Depending on how many values you have to loop through to get to the first finite value each time, this may be a faster option.