I am plotting a curve that has 3 different functions, y, for 3 intervals of x. The code is below and plot also attached. I want to know how to colour the 3 parts in 3 different colours, i.e. for intervals xval<-36, -36 <= xval <= -34 and xval>-34?
import matplotlib.pyplot as plt
x = np.linspace(-50,23,500)
y = []
for xval in x:
if xval < -36:
y.append(m.log10(((10**xval)/(10**-36))**(1/2)))
elif -36 <= xval <= -34:
y.append(m.log10(np.exp((10**36)*((10**xval)-(10**-36)))))
else:
y.append(m.log10((np.exp((10**36)*((10**-34) - (10**-36)))*(((10**xval)/(10**-36))**(1/2)))))
plt.plot(x,y)
pl.xlim([-44.,-30.])
#pl.ylim([-10.,20.])
pl.xlabel('log x')
pl.ylabel('log y')
plt.show()
Thank you!
CodePudding user response:
Draw three lines
y = np.array(y)
i1 = np.where(x <= -36)
i2 = np.where((x > -36) & (x <= -34))
i3 = np.where(x > -34)
_, ax = plt.subplots(figsize=[12, 8])
ax.plot(x[np.append(i1, i2[0])], y[np.append(i1, i2[0])], color="green")
ax.plot(x[np.append(i2, i3[0])], y[np.append(i2, i3[0])], color="red")
ax.plot(x[i3], y[i3], color="blue")
ax.set_xlim([-44.,-30.])
ax.set_xlabel('log x')
ax.set_ylabel('log y')
CodePudding user response:
from turtle import color
import matplotlib.pyplot as plt
x = np.linspace(-50,23,500)
y = []
for xval in x:
if xval < -36:
y.append(m.log10(((10**xval)/(10**-36))**(1/2)))
plt.plot(color = "red")
elif -36 <= xval <= -34:
y.append(m.log10(np.exp((10**36)*((10**xval)-(10**-36)))))
plt.plot(color = "green")
else:
y.append(m.log10((np.exp((10**36)*((10**-34) - (10**-36)))*(((10**xval)/(10**-36))**(1/2)))))
plt.plot(color = "blue")
plt.plot(x,y)
plt.xlim([-44.,-30.])
#pl.ylim([-10.,20.])
plt.xlabel('log x')
plt.ylabel('log y')
plt.show()
This should work, i have added the plt.plot() functions and given an attribute to the color parameter. I am not fully sure if this is what you meant though, but i hope it helps.