Matplotlib draw relative points in 3d space - python

I would like to draw the trayectories of an industrial robot plotting lines in 3D. I have already something in mind but I am stuck because I want to plot points relative to different frames. I tried something but it is not very elegant. Sorry for not showing it here the source code, technical problems.
I will use matplotlib and Python to program it.
Question: Is there a function in matplotlib to draw relative points in 3D space?

Relative to what?
It is YOU who can best tell the coordinates of the point.
If you want to count everything relative to a p0(x0,y0) point, then you add the x0, y0 values to each point.
from matplotlib import pyplot as pl
points = [[2,2],[3,3],[4,4],[5,5]]
p0 = [3,3]
for p in points:
pl.plot(p[0]+p0[0], p[1]+p0[1], "r.")
print p[0]+p0[0], p[1]+p0[1]
pl.show()
If you use numpy then you can even add the p0 to a whole array storing the coordinates.
If you want to calculate each point relative to the previous one, then do so, just little change in the code.

Related

Marking out points on surface directly above level curve in xy-plane

I have a 3D plot with a surface z = f(x,y) and a level curve g(x,y) = c. This last one lies in the xy-plane. I need to somehow mark out the points on the surface which lie directly above the level curve. I am trying to maximize f, but are restricted to the points directly above g(x,y)=c. Therefore, for illustrational purposes, it would be nice to make it clear for my readers which points we are evaluating. My current plot looks like this:
Current plot
My first idea was to somehow collect the points on g(x,y) = c, adding them to a list, and then again plot z = f(x,y) with these points as the argument. Is that overcomplicating things?
I know that Python has commands for projecting from surfaces to the xy-plane. What I am trying here is kinda the other way around. Does Python have a command for that?
Answers are highly appreciated.

animated plot in python

hope you are doing fine
i want to plotting an animated plot to watch a point go to its specific destination(the point could be a moving car though)
point location has specified with a numpy array that has an x and y in each row.
position_mat(point location) will be updated in each iteration
this animated plot should displayed on an other plot with its nodes and paths, the point will move on this plot.
any other method or thing that can do this work will be considered, thanks.

Cartopy: Can't plot vector field with uncertainties (and related questions)

I've been trying for a while now to plot vector field with uncertainty ellipses in Cartopy. The idea is that if I have a location (lat/lon) and a vector (wind speed, for example), but that vector has an uncertainty (measured in standard deviation, for example), then I'd like to plot an ellipses around the tip of the arrow indicating that uncertainty. In GMT, psvelo does the trick, my goal is something like this.
This is the same question as has been asked before here - I'm reopening it because I think that if someone can help me understand transforms better and I can find the location of the tip of the arrow, I can plot the error ellipse myself. Plus, some Matplotlib/Cartopy functionality might have changed in the last 4 years.
So, here's what I tried so far:
Making a map, using quiver to plot the vectors, and then trying to access some sort of scale parameter in the returned Quiver object. I couldn't find anything useful, and even though the scale attribute looked like it would've been the right thing, it turned out never to be set unless I set it myself.
If I do set the scaling myself, I don't know how to do this if my location and vector have different units, and both are obviously not related to the axis width. For example, if I decided that I want to have a 50 m/s long vector at 10°E, 40°N, to be a certain fraction of the width of the axis, what would my scale parameter be? Me trying out random combinations of transformations has not gotten any results. (The idea here then being, if I can figure out that relation, then I am one step closer to knowing where to put the ellipse.)
I've tried to figure out quiver 's autoscaling to see how I can "predict" what it's going to do internally, and then use that to know where the tip of the arrow is. Sadly, it's not as straightforward as the Matlab variant, so I failed at that as well.
Lastly, I also don't understand why I can't use cartopy.crs.Geodetic() as my source coordinate system. The error I get is invalid transform: Spherical quiver is not supported - consider using PlateCarree/RotatedPole. From reading the Cartopy documentation, wouldn't that be the appropriate one if my vector's location is measured in latitude, longitude and altitude?
Here's an MWE:
# imports
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# data
lon, lat = np.array([10, 10.5]), np.array([40, 40])
east, north = np.array([0, 50]), np.array([50, 0])
# map
fig, ax = plt.subplots(subplot_kw={"projection": ccrs.Mercator()})
ax.set_extent([7, 13, 38, 42], ccrs.Geodetic())
ax.coastlines("50m")
q = ax.quiver(lon, lat, east, north, transform=ccrs.PlateCarree())
plt.show()
I really think this is a feature that Cartopy should have, as it is one of the biggest hurdles I've encountered so far when using Python for geoscience applications. Currently, the only approach I know is to write a GMT script file from within my Python program, and run GMT with a Python system call, and that's really a pain.
I know that GMT is developing their own Python interface, but they haven't even incorporated all the basic functionality, so it's anyone's guess when they will get to psvelo...
Thanks for all your help and tips,
PBB
Well the hard part about this is the matplotlib part. If I were trying to make this, I'd focus on that before making it work in Cartopy. Technically, the point you need is somewhere in the set of paths generated by the quiver command (located in q._paths in your MWE). A simpler solution would be to use pivot='tip' so that the point of the arrow is always located at the (x,y) point.
The error you're getting from Cartopy when you try to use Geodetic is because doing everying correctly when working on a sphere involves more complicated math--thus not everything works with Geodetic. If instead you use PlateCarree, it will treat lon/lat as Cartesian coordinates on a plane.

Creating triangulated grid mesh using matplotlib

So I have been creating mesh's using software called BlueKenue for hydraulic models, which is great. In the document I am currently writing I would like to include an image of the mesh however the mesh's I have constructed are very long in the x-axis and short in the y direction. Unfortunately BlueKenue will not allow you to have different scale ranges on your axis (or if it does I have not been able to find a way), i.e. if you have increments of 5 on one axis you will have likewise on the other. I have included an image of the mesh I currently have to illustrate my problem. If I can construct this mesh in matplotlib I can then ensure my image is suitably clear.
My question is can I reproduce this mesh in Matplotlib in a relatively simple way? (I am fairly new to python). The mesh is a regular grid which has been triangulated.
Edit:
Mesh dimensions 29.76 x 2
x intervals = 0.16m (186 points along the x axis)
y intervals = 0.20m (10 points along the y axis)
Thanks

How do I convert (or scale) axis values and redefine the tick frequency in matplotlib?

I am displaying a jpg image (I rotate this by 90 degrees, if this is relevant) and of course
the axes display the pixel coordinates. I would like to convert the axis so that instead of displaying the pixel number, it will display my unit of choice - be it radians, degrees, or in my case an astronomical coordinate. I know the conversion from pixel to (eg) degree. Here is a snippet of what my code looks like currently:
import matplotlib.pyplot as plt
import Image
import matplotlib
thumb = Image.open(self.image)
thumb = thumb.rotate(90)
dpi = plt.rcParams['figure.dpi']
figsize = thumb.size[0]/dpi, thumb.size[1]/dpi
fig = plt.figure(figsize=figsize)
plt.imshow(thumb, origin='lower',aspect='equal')
plt.show()
...so following on from this, can I take each value that matplotlib would print on the axis, and change/replace it with a string to output instead? I would want to do this for a specific coordinate format - eg, rather than an angle of 10.44 (degrees), I would like it to read 10 26' 24'' (ie, degrees, arcmins, arcsecs)
Finally on this theme, I'd want control over the tick frequency, on the plot. Matplotlib might print the axis value every 50 pixels, but I'd really want it every (for example) degree.
It sounds like I would like to define some kind of array with the pixel values and their converted values (degrees etc) that I want to be displayed, having control over the sampling frequency over the range xmin/xmax range.
Are there any matplotlib experts on Stack Overflow? If so, thanks very much in advance for your help! To make this a more learning experience, I'd really appreciate being prodded in the direction of tutorials etc on this kind of matplotlib problem. I've found myself getting very confused with axes, axis, figures, artists etc!
Cheers,
Dave
It looks like you're dealing with the matplotlib.pyplot interface, which means that you'll be able to bypass most of the dealing with artists, axes, and the like. You can control the values and labels of the tick marks by using the matplotlib.pyplot.xticks command, as follows:
tick_locs = [list of locations where you want your tick marks placed]
tick_lbls = [list of corresponding labels for each of the tick marks]
plt.xticks(tick_locs, tick_lbls)
For your particular example, you'll have to compute what the tick marks are relative to the units (i.e. pixels) of your original plot (since you're using imshow) - you said you know how to do this, though.
I haven't dealt with images much, but you may be able to use a different plotting method (e.g. pcolor) that allows you to supply x and y information. That may give you a few more options for specifying the units of your image.
For tutorials, you would do well to look through the matplotlib gallery - find something you like, and read the code that produced it. One of the guys in our office recently bought a book on Python visualization - that may be worthwhile looking at.
The way that I generally think of all the various pieces is as follows:
A Figure is a container for all the Axes
An Axes is the space where what you draw (i.e. your plot) actually shows up
An Axis is the actual x and y axes
Artists? That's too deep in the interface for me: I've never had to worry about those yet, even though I rarely use the pyplot module in production plots.

Categories

Resources