Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Functions intervals

I have 3 functions, how can I plot them using differents intervals ?

This is my code:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,100)
y = 2*x+1
k = 3*x+2
i = 2*x+2
plt.plot(x, y, '-r', label='y=2x+1')
plt.plot(x, k, '-r', label='k =3x+2')
plt.plot(x, i, '-r', label='i =2x+2')
plt.title('3 functions on 3 intervals')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

This is the style I need :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

This is what I get :
enter image description here

My wish is to draw the first function 2*x+1 on the following interval [x:2->x:8].

The second function 3*x+2 has to be plotted on the interval [x:8->x:12]

The third function 2*x+2 has to be plotted on the interval [x:12->x:20]

Is it possible ?

>Solution :

Is it this that you want?

enter image description here

In [33]: import matplotlib.pyplot as plt
    ...: import numpy as np
    ...: 
    ...: xs = [np.linspace(0,4), np.linspace(-3, 5), np.linspace(4, 10)]
    ...: fs = [np.cos, np.sin, lambda x:np.sin(x)-2*np.cos(x)]
    ...: for x, f in zip(xs, fs):
    ...:     plt.plot(x, f(x), label=f.__name__)
    ...: plt.legend()
    ...: plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading