Reputation: 41
I've got a code that plots a graph of two functions on Python. I was just wondering that if there was a way to have my x function only on the domain between 0 and 0.8 so it wouldn't carry on after the intersection with h-but I still want h to carry on. Is there any way I can modify this? Thanks. This is my code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerLine2D
t = np.arange(0.0, 1.0, 0.001)
h = 0.1*np.sin(10*t)
x = 4*t-5*t**2
line1, = plt.plot(t, h, label='h(t)')
line2, = plt.plot(t, x, label='x(t)', linestyle='--')
plt.legend(handler_map={line1: HandlerLine2D(numpoints=4)})
plt.xlabel('time')
plt.ylabel('height')
plt.title('Fig 1.')
plt.grid(False)
plt.savefig("Plot.png")
plt.show()
This is how it looks:
Upvotes: 2
Views: 8593
Reputation: 910
Simply slice the two arrays:
line2, = plt.plot(t[t<0.8], x[t<0.8], label='x(t)', linestyle='--')
Upvotes: 2