Home > Blockchain >  how to plot two functions as a same function with same x axis?
how to plot two functions as a same function with same x axis?

Time:10-03

I have written a code that produces standing waves. I am getting the standing waves but the forward wave and backward wave that I have created are two different functions that's why different colors are appearing. now I want to write the forward and backward waves in a single function?

the code is

import numpy as np
import matplotlib.pyplot as plt
x=linspace(0,4*np.pi,10001)
y=np.sin(x)
yn=(-y)
plt.plot(x,y)
plt.plot(x,-y)

how to modify it and plot it as a single function.

CodePudding user response:

I guess on of the efficient ways to achieve it

import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,4*np.pi,10001)
y=np.sin(x)
plt.plot(x,y,'b-', x, -y, 'b-')

Hopefully, it helps

CodePudding user response:

Welcome to SO :)

A function that contains two functions means f(x)=>(y, -y), and this is illegal according to plot function documentation and would cause x and y must have same first dimension error and moreover it is against the theoretical definition of the function itself! But if you are into unifying the graph colors why don't you set it manually?

import numpy as np
from numpy import *
import matplotlib.pyplot as plt
x=linspace(0,4*np.pi,10001)
y=np.sin(x)
yn=(-y)
plt.plot(x,y, color='red')
plt.plot(x,-y, color='red')

enter image description here

  • Related