1

How to plot a x versus y line? By x versus y, I mean how to plot x vs y line if the x and y axes have already fixed, as if the axes are reversed for this line.

Update: Some one asked me why not just reverse the arguments and axes labels. Here is my reason: this x vs y line is only a part of a 2D plot (the main plot) and the main axes are for the 2D plot. What's more, there are also y vs x lines in the same 2D plot. I do this because I want to show certain line clearly.

Update: Here is a example what I want:

example results

I want to plot the black line in the figure which I draw manually (actually I want to draw Gaussian curve). It is time vs voltage. I still want to keep the existed blue line and I should not reverse the time/voltage labels.

7
  • 3
    You mean you want to reverse the axes? Why don't you just reverse the arguments? ax.plot(s, t) and reverse the time/voltage labels. Commented Apr 4, 2018 at 7:13
  • @Djib2011 Uh, because this x vs y line is only a part of a 2D plot and the axes is for the 2D plot. What's more, there is also y vs x lines in the same 2D plot. I do this because I want to show certain line clearly. Commented Apr 4, 2018 at 7:59
  • I ask it because I really need it. In the past I make it using Photoshop, but now I think it is too tricky. I believe it is not only me who need a way to do this. Commented Apr 4, 2018 at 8:00
  • Your updated figure shows a flipped x-y axis. This is what Djib2011 suggested. Commented Apr 4, 2018 at 8:59
  • @MrT I feel a little confused. I still want to keep the existed blue line and I should not reverse the time/voltage labels. Commented Apr 4, 2018 at 9:01

1 Answer 1

2

You can easily plot multiple curves in the same subplot in matplotlib. As an example see this annotated code:

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

# Note that using plt.subplots below is equivalent to using
# fig = plt.figure() and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
#plot sine wave
ax.plot(t, s, label = "sine wave")

#now create y values for the second plot
y = np.linspace(0, 2, 1000)
#calculate the values for the Gaussian curve
x = 2 * np.exp(-0.5 * np.square(-4 * (y - 1)))
#plot the Gaussian curve
ax.plot(x, y, label = "Gaussian curve")

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

#show the legend
plt.legend()
plt.show()

Output:

enter image description here

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

2 Comments

I can't believe there is such an elegant way. Thank you! I was confused why people voted down, now I know it.
The main reason for the down-votes was the confusing question. I was not sure that this is, what you try to achieve. Glad we found a solution. Good luck with your project.

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.