I have two 3D-points, for example a = (100, 100, 10) and b = (0, 100, 60), and would like to fit a line through those points.
I know, the 3D line equation can have different shapes:
Vector-form:
(x,y,z)=(x0,y0,z0)+t(a,b,c)
Parameter-form:
x=x0+ta
y=y0+tb
z=z0+tc
But I have a problem getting the data in the right shape for a numerical function.
The following code should work
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection ='3d')
# defining coordinates for the 2 points.
x = np.array([100, 0])
y = np.array([100, 100])
z = np.array([10, 60])
# plotting
ax.plot3D(x, y, z)
plt.show()
Here the ax.plot3D() plots a curve that joins the points (x[i], y[i], z[i]) with straight lines.
Related
I want to plot some equation in Matplotlib. But it has different result from Wolframalpha.
This is the equation:
y = 10yt + y^2t + 20
The plot result in wolframalpha is:
But when I want to plot it in the matplotlib with these code
# Creating vectors X and Y
x = np.linspace(-2, 2, 100)
# Assuming α is 10
y = ((10*y*x)+((y**2)*x)+20)
# Create the plot
fig = plt.figure(figsize = (10, 5))
plt.plot(x, y)
The result is:
Any suggestion to modify to code so it has similar plot result as wolframalpha? Thank you
As #Him has suggested in the comments, y = ((10*y*x)+((y**2)*x)+20) won't describe a relationship, so much as make an assignment, so the fact that y appears on both sides of the equation makes this difficult.
It's not trivial to express y cleanly in terms of x, but it's relatively easy to express x in terms of y, and then graph that relationship, like so:
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(-40, 40, 2000)
x = (y-20)*(((10*y)+(y**2))**-1)
fig, ax = plt.subplots()
ax.plot(x, y, linestyle = 'None', marker = '.')
ax.set_xlim(left = -4, right = 4)
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
Which produces the following result:
If you tried to plot this with a line instead of points, you'll get a big discontinuity as the asymptotic limbs try to join up
So you'd have to define the same function and evaluate it in three different ranges and plot them all so you don't get any crossovers.
import numpy as np
import matplotlib.pyplot as plt
y1 = np.linspace(-40, -10, 2000)
y2 = np.linspace(-10, 0, 2000)
y3 = np.linspace(0, 40, 2000)
x = lambda y: (y-20)*(((10*y)+(y**2))**-1)
y = np.hstack([y1, y2, y3])
fig, ax = plt.subplots()
ax.plot(x(y), y, linestyle = '-', color = 'b')
ax.set_xlim(left = -4, right = 4)
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
Which produces this result, that you were after:
My goal is to create 3 images, one is an anti-aliased image of a random spline and then two others that scaled between 0 and 1 for how "horizontal" or "vertical" the spline is at each point.
from scipy.interpolate import CubicSpline, griddata
import numpy as np
import matplotlib.pyplot as plt
def create_random_line():
# Create random spline
x = np.array([1, 15, 30, 49])
y = np.random.uniform(1, 50, 4)
f = CubicSpline(x, y, bc_type='natural')
x_new = np.linspace(0, 49, 100)
y_new = f(x_new)
y_new_deriv = f(x_new, 1)
y_angles = np.array([math.atan2(tt, 1) for tt in y_new_deriv])
# Plot the spline, derivative and angle
plt.figure(2)
plt.clf()
plt.subplot(3,1,1)
plt.plot(x, y, 'x')
plt.xlim((0, 50))
plt.ylim((0, 50))
plt.plot(x_new, y_new)
plt.subplot(3,1,2)
plt.plot(x_new, y_new_deriv)
plt.subplot(3,1,3)
plt.plot(x_new, np.rad2deg(y_angles))
plt.ylim((-90, 90))
plt.show()
# Create image of spline
image = np.zeros((50, 50))
scaled_angle_maps = np.zeros((50, 50, 2))
for xx, yy, rr in zip(y_new, x_new, np.rad2deg(y_angles)):
image[int(np.round(xx)), int(np.round(yy))] = 1
scaled_angle_maps[int(np.round(xx)), int(np.round(yy)), 0] = np.clip(1 - (np.abs(rr)/90), 0, 1)
scaled_angle_maps[int(np.round(xx)), int(np.round(yy)), 1] = np.clip(np.mod(np.abs(rr),90)/90, 0, 1)
return image, scaled_angle_maps
# Create random spline image
image, scaled_angle_maps = create_random_line()
# Plot
plt.figure(1)
plt.clf()
plt.subplot(2,2,1)
plt.imshow(image)
plt.gray()
plt.colorbar()
plt.ylim((0,50))
plt.subplot(2,2,3)
plt.imshow(scaled_angle_maps[:,:,0])
plt.ylim((0,50))
plt.colorbar()
plt.title('horizontal')
plt.subplot(2,2,4)
plt.imshow(scaled_angle_maps[:,:,1])
plt.ylim((0,50))
plt.colorbar()
plt.title('vertical')
plt.show()
But, I would like this anti-aliased. I have been reading about Wu's algorithm, but most implementations appear to be for straight lines. I tried creating it from the matplotlib canvas, but that did not work out well.
Then, second, I would like to have an arbitrary thickness to the spline in the image, though I suppose I could just scipy.ndimage.grey_dilation after the image is created.
So, am I missing an easy method of creating a random spline image? It feels like there should be a simpler method to do this.
I'm trying to create a contour plot to show a height field at time 0, represented by h_list[0, :, :]. When I run the code, an empty plot shows up with the correct axes and scales, but no contour lines. The shape of h_list is [250, 99, 99], but I am only calling the first x index, which is shape [99, 99]. This matches the others.
My code is this:
h_list = np.array(h_list)
con = plt.figure()
ax = con.add_subplot(111)
x = np.linspace(0, 1000, 99)
y = np.linspace(0, 1000, 99)
X, Y = np.meshgrid(x, y)
Z = h_list[0, :, :]
ax.contour(X, Y, Z)
ax.set_xlim(0, 1000)
ax.set_ylim(0, 1000)
The shapes of all variables are the same and I've set the appropriate limits so I'm not sure why matplotlib isn't recognizing my data. I do not get any compiling errors, just no graph. Any ideas?
Edit: Z has shape [99, 99], which is the same as X and Y.
I have a loss function of two variables W1, W2 and an output z = F(W1,W2).
Now I plot the contour map of this loss function. Now say, I have calculated gradient vector at two points, therefore I have two gradient vectors now. I want to plot these gradient vector on my contour plot but I have no idea how to procces. Any help is appreciated
enter code here
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
feature_x = np.arange(-50, 50, 2)
feature_y = np.arange(-50, 50, 3)
# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)
fig, ax = plt.subplots(1, 1)
z = 0.5*np.array((Y-X)*(Y-X) + 0.5*(1-X)*(1-X))
# plots contour lines
ax.contour(X, Y, z, 10, cmap = 'jet')
ax.grid(True)
ax.axis('scaled')
#ax.clabel(cp, inline=1, fontsize=10)
ax.set_title('Contour Plot')
ax.set_xlabel('feature_x')
ax.set_ylabel('feature_y')
plt.show()
You could use FancyArrowPatch to draw the gradients at a few selected positions.
from matplotlib.patches import FancyArrowPatch
x1 = -20 # position of the gradient
y1 = 10
dz1_dx = 10 # value of the gradient at that position
dz1_dy = -5
arrow = FancyArrowPatch((x1, y1), (x1+dz1_dx, y1+dz1_dy),
arrowstyle='simple', color='k', mutation_scale=10)
ax.add_patch(arrow)
Otherwise if you want to plot the whole vector field quiver might be an option:
feature_x = np.arange(-50, 50, 2)
feature_y = np.arange(-50, 50, 2)
x, y = np.meshgrid(feature_x, feature_y)
z = 0.5*(y-x)**2 + 0.5*(1-x)**2
u = 2*x - y - 1
v = y - x
# Normalize all gradients to focus on the direction not the magnitude
norm = np.linalg.norm(np.array((u, v)), axis=0)
u = u / norm
v = v / norm
fig, ax = plt.subplots(1, 1)
ax.set_aspect(1)
ax.plot(feature_x, feature_y, c='k')
ax.quiver(x, y, u, v, units='xy', scale=0.5, color='gray')
ax.contour(x, y, z, 10, cmap='jet', lw=2)
arrow = FancyArrowPatch((35, 35), (35+34*0.2, 35+0), arrowstyle='simple',
color='r', mutation_scale=10)
ax.add_patch(arrow) # NOTE: this gradient is scaled to make it better visible
I added the line y = x in this plot and marked the point where this lines intersects with a contour line. Here you can see clearly
that:
Gradients are orthogonal to level surfaces
So for your point (80, 80) the gradient (79, 0) is correct even so the general shape of isolines maybe suggest that there should be be a part in y-direction.
But if you look along the line y=x you see that the gradients there are always only in x-direction.
Consider the folowing plot:
fig, ax = plt.subplots(figsize = (14, 6))
ax.set_facecolor('k')
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
xs = np.arange(60, 70) # xs = np.linspace(60, 70, 100)
ys = np.arange(0, 100, .5) # ys = np.linspace(0, 100, 100)
v = [[[x, y] for x in xs] for y in ys]
lines = LineCollection(v, linewidth = 1, cmap = plt.cm.Greys_r)
lines.set_array(xs)
ax.add_collection(lines)
How can I change the color of the lines according to their x coordinates (horizontally) so as to create a "shading" effect like this:
Here, the greater x is, the "whiter" the LineCollection is.
Following this reasoning, I thought that specifying lines.set_array(xs) would do the trick but as you can see in my plot the color gradation is still following the y axis. Strangely the pattern is repeating itself, from black to white (every 5) over and over (up to 100).
I think (not sure at all) the problem lies in the v variable that contains the coordinates. The concatenation of x and y might be improper.
The shape of the list v you supply to the LineCollection is indeed not suitable to create a gradient of the desired direction. This is because each line in a LineCollection can only have single color. Here the lines range from x=60 to x=70 and each of those lines has one color.
What you need to do instead is to create a line collection where each line is devided into several segments, each of which can then have its own color.
To this end an array of dimensions (n, m, l), where n is the number of segments, m is the number of points per segment, and l is the dimension (2D, hence l=2) needs to be used.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
fig, ax = plt.subplots(figsize = (14, 6))
ax.set_facecolor('k')
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
xs = np.linspace(60, 70, 100)
ys = np.linspace(0, 100, 100)
X,Y = np.meshgrid(xs,ys)
s = X.shape
segs = np.empty(((s[0])*(s[1]-1),2,2))
segs[:,0,0] = X[:,:-1].flatten()
segs[:,1,0] = X[:,1:].flatten()
segs[:,0,1] = Y[:,:-1].flatten()
segs[:,1,1] = Y[:,1:].flatten()
lines = LineCollection(segs, linewidth = 1, cmap = plt.cm.Greys_r)
lines.set_array(X[:,:-1].flatten())
ax.add_collection(lines)
plt.show()