0

I want to draw parallel diagonal lines. The second diagonal line should be 5% below the first one and the third one should be 5% above the first one. I do not want to use the axis limits because my data is not stationary, it is volatile. So, I want to use actual limits. My code works fine and produces the output. Whether this is the correct approach or not I have no idea. I mean, 5% below 1200 is 1200*0.95 but how to get 5% below at close to the origin. In my code, I have used 50 by trial and error.

My code and output:

plt.plot([0,1200],[50,1200*1.05],'k--',linewidth=2)
plt.plot([0,1200],[0,1200],'k--',linewidth=2)
plt.plot([50,1200],[0,1200*0.95],'k--',linewidth=2)

enter image description here

3
  • 1
    You can't recalculate every y value as a percentage as this will just change the gradient of the line. You need to ADD the same amount to EVERY y value (or minus) to shift it up or down while keeping the same gradient Commented Sep 11, 2020 at 14:04
  • @GhandiFloss Thanks a ton. Yes! I figured out that suppose 5% of 1000 is 50. I should use this number to the plot below and above lines. Your comment reassured me. THanks Commented Sep 11, 2020 at 14:07
  • print(1200+(1200*(5/100))) print(1200-(1200*(5/100))) for 5+% and 5-% .Hence consider x as your number i.e. 1200 Commented Sep 11, 2020 at 14:10

1 Answer 1

1

To draw diagonal lines, regardless of the current x / y limits, the most natural approach is to:

  • specify ending poins of lines in axes coordinates (0 to 1),
  • pass transform=ax.transAxes.

Running the following example code:

fig = plt.figure()
ax = plt.subplot(xlim=(0, 1250), ylim=(0, 1200))
ax.text(0.05, 0.95, 'Diagonal lines', transform=ax.transAxes, fontsize=12, va='top')
# Diagonal
plt.plot([0,    1],    [0,    1],    'k--', linewidth=1, transform=ax.transAxes)
# 5 % down
plt.plot([0.05, 1],    [0,    0.95], 'r--', linewidth=1, transform=ax.transAxes)
# 5 % up
plt.plot([0,    0.95], [0.05, 1],    'g--', linewidth=1, transform=ax.transAxes)
plt.show()

I got the following plot:

enter image description here

I deliberately used different colors of lines, to ease their identification.

Experiment with this code, changing xlim and ylim, and each time all 3 lines should be at just the same positions.

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

1 Comment

I am interested on your answer. How can one define the limits, x and y of the dashed lines?

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.