1

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 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I don't know where the curve begins, is there a way I can find out the position in an array of the first non-NaN values?
Yes can be done.. just use a 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 array
3

As 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.

Comments

2

To plot vertical dashed line you have to set the same x value and make code like this (use '--' for dashed line or ':'):

x = 100  # line at this x position
y0 = 0   # y limits of a line
y1 = 100
plt.plot((x,x),(y0,y1),'k--')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.