0

I'm creating a plot using these lines of code, pretty standard.

plt.plot(x, It)
plt.plot(x, NIt, dashes=[6,2])
plt.show()

Which gives me the following result: Graph using current code

Which is correct given the code I am using. However, I would like to use a graph that keeps the variable value until the value changes, i.e. an horizontal line until the value changes, at which point the dots are connected by a vertical line. So instead of the above (for the blue), I'd have something like the black line shown here (I did only the start to illustrate it, forgive my terrible paint skills): Wanted graph

Any way of doing this? Thanks in advance.

1
  • You want the drawstyle = 'steps' option to plt.plot(). Writing a full answer below. Commented Dec 16, 2018 at 19:35

1 Answer 1

1

This is what the drawstyle proptery is for. Use the 'steps-pre' style to achieve what you're looking for. See the drawstyle property section of matplotlib.lines.Line2D docs. (steps-pre and steps-post change whether the y-value is the beginning or end of the horizontal line; see Step Functions in Matplotlib for more examples and explanation.)

If you modify your code as follows, it should work as you want:

plt.plot(x, It)
plt.plot(x, NIt, dashes=[6,2], drawstyle='steps-pre')
plt.show()

Cheers!

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

2 Comments

Thanks! Didn't realize it'd be that easy. :p And my mind completely blanked for some reason, I knew there was a word for the "steps" but it just wouldn't come to me, started describing them as vertical/horizontal lines. haha
Glad to help. :-) Modified the answer to link to some examples of steps-pre and steps-post in case it's helpful.

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.