Python Forum
showing only desired part of a plot - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: showing only desired part of a plot (/thread-28235.html)



showing only desired part of a plot - grknkilicaslan - Jul-10-2020

Hi,
I do simply have a plot but I need to show only a specific part of that plot. When I run the code below, it shows all possible results but I need only the part between y=-3 and y=3 also x=-3 and x=3.
If you can help me I will be very appreciated.

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
 
 
def f(t,r):
    theta,x,z=r
    ftheta=(-1/2)*(np.cos(x)*np.cos(z)+np.sin(theta)/100)
    fx=0.5*np.sin(theta)-(1/2)*np.cos(x)*np.sin(z)
    fz=0.5*np.cos(theta)+(1/2)*np.sin(x)*np.cos(z)
    return ftheta,fx, fz
 
sol=integrate.solve_ivp(f,(0,2000),(np.pi/4,-np.pi/2,-np.pi/2), t_eval=np.linspace(0,2000,100000))
theta,x,z=sol.y

#plotting
plt.plot(x,z,color="red")
plt.show()



RE: showing only desired part of a plot - Marbelous - Jul-10-2020

You can use the xlim and ylim plot functions for that.

Add the following before your plt.plot() statement.
plt.xlim(-3, 3)
plt.ylim(-3, 3)
https://www.pythonprogramming.in/how-to-set-axis-limits-in-matplotlib.html