I have a 1000 x 3 numpy array of coordinates that consist of an (x, y, theta in radians) pose for a moving robot at various times (from time = 0 to time = 1000). Is it possible to graph this position and orientation information using python so that at each point (x,y) there is a small arrow that points in the theta direction? Perhaps a matplotlib type graph would be possible for this?
Have you tried the arrow function in matplotlib (documentation)?
Assuming that theta is angle in radians from the x axis, perhaps something like the following for each point will do it.
arrow(x, y, cos(theta), sin(theta))
The above code will draw an arrow from (x,y) to (x+dx, y+dy).
Another option is matplotlib.pyplot.quiverdocumentation. The quiver function allows us to control the length of the arrow in many ways.
Related
I'm trying to create a traffic simulator using python (+ pygame and math libs).
I'm stuck at car movements, I've managed to create a fixed horizontal and vertical path using a list of coordinates, using nested for loops.
Now, since I'm in a crossroads I now have to make the 1/4 of a circle to make a left or right turn.
The car has to make a straight path for n pixels (and it's ok) then have to make the 1/4 following a circular path and then proceed towards a straight line again.
I have a list of coordinates, however, I don't know how to make it turn around the center of the circumference (which I know the coordinates of) while also turning the image relative to the center!
I'll give you an idea of the problem using an image
The red paths "1" are done, the problem is the blue tract "2", "O" coordinates are known, there are 4 "O" in total (one for each path).
I would suggest to use the equation of a circle which is (x – h)^2 + (y – k)^2 = r^2 where (h,k) are the coordinates of the cercle's center and r the radius.
The idea in your case could be to draw all discrete points (x,y) that verify this equation,
here is an example (for points where x and y are positive regarding the origin, if we follow your example):
import numpy as np
import matplotlib.pyplot as plt
center = (10,10)
radius = 5
th = 0.01
for x in np.arange(center[0],center[0]+radius+th, th):
for y in np.arange(center[1], center[1]+radius+th, th):
if abs((x-center[0])**2 + (y-center[1])**2 - radius**2) < th:
plt.scatter(x,y)
plt.show()
You can then control the number of points with the th, and even draw lines instead of just points to speed up the process
r = np.linspace(0.1,1,11)
theta = np.linspace(-alpha,alpha,11)
radius_matrix, theta_matrix = np.meshgrid(r,theta)
u_radial = -q*(1/radius_matrix)*u_sol[0]
u_theta = theta_matrix*[0 for x in range(len(u_sol[0]))]
ax = plt.subplot(111, polar=True)
ax.plot(theta_matrix, radius_matrix, u_radial, u_theta) #color='r',
ls='none', marker='.'
plt.show()
I am trying to make a plot of a velocity field (same as vector field) using numpys quiver function. The velocity field is written
where q is just an arbitrary constant and r is the distance to the origin. Now, to plot this in a polar coordinate system I create two meshgrids radius_matrix and theta_matrix, as seen in my code (line three). Together these meshgrids form a polar coordinate plane, with r on the horizontal axis and theta on the vertical axis (at least I think) and each point should have a vector arrow corresponding to the equation above.
So for that to happen I define u_radial and u_theta, which are the vector components in radial and angluar direction, resp.. The variable u_sol[0] contains f(theta) (as seen in the equation) for 11 different theta points, and I thought that this would give the correct vectorcomponent, but it doesnt. Why not?
I am expecting something like this, that the arrow shrinks when I get close to the edge for a single value of r. I just want this but for more values of r. This is the data of my u_sol[0] vector:
u_sol[0] = [4.68520269e-26 1.54380741e+00 2.74550730e+00 3.60503630e+00
4.12217780e+00 4.29651250e+00 4.12741184e+00 3.61407419e+00
2.75560427e+00 1.55113610e+00 3.84028608e-18]
When I plot this, I get something worse, see the figure below. What happend to the arrows? And why are there colors all of a sudden?
Best regards SimpleP.
Right now I am plotting a streographic projection with a center at the z axis by getting a P vector (Px, Py, Pz) from different directional indices in a unit sphere. The following process converts these to 2d coordinates.
(Python)
x2,y2,z2=P
theta2=np.arccos(abs(z2)/(x2**2+y2**2+z2**2)**.5)
phi2=np.arctan((y2/x2))
xp=theta2*np.cos((phi2))
yp=theta2*np.sin((phi2))
Plot xp,yp
What transformations do I apply in order to change the P vector (or a different aspect of the code) so that the center is on a different direction (x-axis for example)?
Thanks.
I have some surface data given as x_mesh, y_mesh, z_mesh.
x_mesh and y_mesh were generated from steps by longitude and latitude on a geotiff (so, their shapes are equal and regular but steps by x and y are not).
z_mesh is the height from tangential plane to Earth ellipsoid at the center of map.
I can easily plot the surface with matplotlib.pyplot.pcolormesh(x_mesh, y_mesh, z_mesh). It works.
Now I want to set a line by mouse and somehow take a Z profile under this line. I need some interpolator to make xy --> z, but don't know which one to use.
I tried to do this:
scipy.interpolate.interp2d(x_mesh, y_mesh, z_mesh)
But it gives me an error: OverflowError: Too many data points to interpolate
Don't you have any ideas how to interpolate such data?
P.S. The geotiff is not very big, it is 6K x 6K pixels. And I see that pcolormesh somehow interpolates the color value between pixels if I zoom in.
So i need to be able to plot a point on a graph made by my already coded cartesian coordinate system. The geometry is like: (0,0) is at the top left of the window and as it goes right, the x increases and as it goes down, the y increases so the bottom right corner would be (800, 600).
My cartesian (0,0) is actually on the point (400, 300) and that's where I want my graphs to be aligned to.
My code for getting input, converting it to an expression and graphing these points using small rectangular dots is
expression = input("Enter a mathematical
for x in range(0, 800):
y = eval(expression)
rect(x, y, 2, 2)
My problem is: the code needs to be able to read and graph all normal mathematical expressions properly such as x, x^2, x^3, etc., but on my drawn cartesian plane, the values are actually all positives due to the window's weird quadrant system created by the graphics library.
I'm not getting the correct plotting when my program starts to plot out and map all these coordinates.
Could someone shed some light on what im supposed to do in terms of actually converting these graphics coordinates to match my cartesian plane coordinates?
NOTE every 30 graphics units = 1 tick unit of my cartesian plane.
if your problem is what i think it is, try the following code.
expression = input("Enter a mathematical
for x in range(0, 800):
x_val = x-400
y_val = eval(expression(x_val))
y = -y_val+300
rect(x, y, 2, 2)