How can I plot the surface of each side of a polygon? - python

I'm trying to plot the surfaces of a polygon generated from arrays of x, y, z
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
print("numpy version: " + np.__version__)
# [x, y, z] coordinates
p1 = [322697.1875, 3663966.5, -30000.0]
p2 = [325054.34375, 3663966.5, -30000.0]
p3 = [325054.34375, 3665679.5, -30000.0]
p4 = [322697.1875, 3665679.5, -30000.0]
p5 = [322697.1875, 3663966.5, -27703.123046875]
p6 = [325054.34375, 3663966.5, -27703.154296875]
p7 = [325054.34375, 3665679.5, -27703.70703125]
p8 = [322697.1875, 3665679.5, -27703.673828125]
points = [p1, p2, p3, p4, p5, p6, p7, p8]
points = np.array(points)
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
X, Y = np.meshgrid(x, y)
zr = np.tile(z, [8, 1])
fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection = '3d')
ax.plot_surface(X, Y, zr, alpha=0.5)
plt.show()
Here is the output
I would like the output to show each side of the polygon as shaded. What am I doing wrong?

Here you go
=^..^=
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# setup data
p0 = [0, 0, 0]
p1 = [1, 1, 1]
p2 = [2, 2, 2]
p3 = [3, 3, 3]
p4 = [4, 4, 4]
p5 = [5, 5, 5]
p6 = [6, 6, 6]
p7 = [7, 7, 7]
p8 = [8, 8, 8]
# create data array
points = [p0, p1, p2, p3, p4, p5, p6, p7, p8]
points = np.array(points)
# get array co-ordinates
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
# create mesh for X and Y set points
X, Y = np.meshgrid(x, y)
# create 0 surface
Z1 = np.zeros_like(X)
# create 8 surface
Z2 = np.full_like(X, 8)
# plot data
fig = plt.figure(figsize=(16, 10))
ax = fig.gca(projection='3d')
# setup each surface
ax.plot_surface(X, Y, Z1, alpha=0.3)
ax.plot_surface(X, Z1, Y, alpha=0.3)
ax.plot_surface(Z1, X, Y, alpha=0.3)
ax.plot_surface(X, Y, Z2, alpha=0.3)
ax.plot_surface(X, Z2, Y, alpha=0.3)
ax.plot_surface(Z2, X, Y, alpha=0.3)
plt.show()
Output:

Related

Convert four-dimensional numpy array to list of x, y, z, intensity

I have a numpy array created as follows
results = np.zeros((X, Y, Z))
Then I am setting values of the points in 3D space as follows (representative of density / intensity of that point)
results[x,y,z] = 5.0
I now want to visualize this data using the x,y,z coordinates and an intensity value (like opacity or size of a scatter plot). However I cannot figure out how to convert this into four lists of x, y, z, and intensity, for a 3D scatter plot. How do I do this?
i would do smth like this:
import numpy as np
import matplotlib.pyplot as plt
dots = np.random.randint(0, 2, size = (3, 3, 3))
dots *= np.random.randint(0, 2, size = (3, 3, 3))
dots *= np.arange(27).reshape(3, 3, 3)
x, y, z = np.where(dots!=0)
o = dots[x, y, z]
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
for i in range(len(x)):
print(o[i]/27)
ax.plot([x[i]], [y[i]], [z[i]], 'o', color=[0, 0, 0, float(o[i])/27])
output:
dots =
[[[ 0 0 0]
[ 0 0 0]
[ 6 0 0]]
[[ 0 0 11]
[ 0 13 0]
[15 16 17]]
[[ 0 0 0]
[21 22 23]
[24 0 0]]]
My solution:
fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(projection="3d")
plt.title("Spherical Potential Heatmap ($J = 32, simuls = 6.4M, E = 1, cutoff = 100$)", fontsize=18)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
mask = base_array_e0 > 100
idx = np.arange(int(np.prod(base_array_e0.shape)))
x, y, z = np.unravel_index(idx, base_array_e0.shape)
plot = ax.scatter(x, y, z, c=base_array_e0.flatten(), s=10.0 * mask, edgecolor="face", alpha=0.15, marker="o", cmap="magma", linewidth=0)
color_bar = plt.colorbar(plot, ax = ax,fraction=0.036, pad=0.04)
color_bar.set_alpha(1)
color_bar.draw_all()
color_bar.set_label('Steps')
plt.savefig('random_walk_3d_energy_sphere_0.png', bbox_inches='tight');

fill space between 3 graphs in Matplotlib

I need to fill in the shaded area between my graphs.What should I write in fill_between to do this?
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: (2 * z - z ** 2) ** (1 / 2)
y1 = lambda x: (6 * x - x ** 2) ** (1 / 2)
y2 = lambda c: c
x = np.linspace(0, 12, 500)
z = np.linspace(0, 12, 500)
c = np.linspace(0, 12, 500)
plt.ylim(0, 4)
plt.xlim(0, 4)
plt.plot(z, y(z), color='blue', label="$y=\\sqrt{2x-x^2}$")
plt.plot(c, y2(c), color='black', label='$y=x$')
plt.plot(x, y1(x), color='red', label='$y=\\sqrt{6x-x^2}$')
plt.plot([0, 4], [0, 0], color='yellow', label='y=0')
plt.grid(True, zorder=5)
miny = np.minimum(y2(c), y1(x))
plt.fill_between(x, y(z), miny, where=(miny > y(x)), alpha=0.5)
plt.legend()
plt.show()
Make the domain of your functions to be the same (e.g. [0, 4]). The below code does what you want:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 500)
y = np.piecewise(x, [x <= 2, x > 2], [lambda x: np.sqrt(2 * x - x ** 2), 0])
y1 = np.sqrt(6 * x - x ** 2)
y2 = x
y3 = 0*x
plt.plot(x, y, color='blue', label="$y=\\sqrt{2x-x^2}$")
plt.plot(x, y1, color='red', label='$y1=\\sqrt{6x-x^2}$')
plt.plot(x, y2, color='black', label='$y2=x$')
plt.plot(x, y3, color='yellow', label='y3=0')
plt.grid(True, zorder=5)
miny = np.minimum(y2, y1)
plt.fill_between(x, y, miny, where = (miny > y), alpha=0.5)
plt.legend()
plt.show()

Fill the area between multiple curves and lines

I have 3 curves and I want to fill the area between them. How should I do this?
This is what I have so far:
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: -(z ** 2)
y1 = lambda x: x ** (1 / 3)
x = np.linspace(0, 2, 100)
z = np.linspace(0, 2, 100)
plt.plot(z, y(z), color='blue', label="y=-(x^2)")
plt.ylim(-2, 2)
plt.xlim(0, 2)
plt.plot(x, y1(x), color='red', label='y=x^(1/3)')
plt.plot([1, 1, 1], [0, -2, 2], color='black', label='x=1')
plt.grid(True, zorder=5)
plt.legend()
k = np.arange(0,2)
f = [0,-0.2]
p = [0,0.2]
plt.fill_between(k,f,p,interpolate=True)
plt.show()
You can use where in fill_between to take care of x = 1 line. See below:
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: -(z ** 2)
y1 = lambda x: x ** (1 / 3)
x = np.linspace(0, 2, 100)
z = np.linspace(0, 2, 100)
plt.ylim(-2, 2)
plt.xlim(0, 2)
#plt.grid(True, zorder=5)
plt.plot(z, y(z), color='blue', label="y=-(x^2)")
plt.plot(x, y1(x), color='red', label='y=x^(1/3)')
plt.plot([1, 1, 1], [0, -2, 2], color='black', label='x=1')
plt.fill_between(x, y(z), y1(x), where=x<=1)
plt.legend()
plt.show()

Best way to plot a 2d contour plot with a numpy meshgrid

i'm looking for the best way to create a contour plot using a numpy meshgrid.
I have excel data in columns simplyfied looking like this:
x data values: -3, -2, -1, 0, 1, 2 ,3, -3, -2, -1, 0, 1, 2, 3
y data values: 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2
z data values: 7 , 5, 6, 5, 1, 0, 9, 5, 3, 8, 3, 1, 0, 4
The x and y values define a 2d plane with the length (x-Axis) of 7 values and depth (y-Axis) of 2 values. The z values define the colour at the corresponing points (more or less a z-Axis).
I've tried:
import matplotlib.pyplot as plt
import numpy as np
x = [-3,-2,-1,0,1,2,3]
y = [1,2]
z = [7,5,6,5,1,0,9,5,3,8,3,1,0,4]
x, y = np.meshgrid(x, y)
A = np.array(z)
B = np.reshape(A, (-1, 2))
fig = plt.figure()
ax1 = plt.contourf(x, y, B)
plt.show()
I'm pretty sure i'm not getting how the meshgrid works. Do i have to use the whole List of x and y values for it to work?
How do i create a rectangular 2d plot with the length (x) of 7 and the depth (y) of 2 and the z values defining the shading/colour at the x and y values?
Thanks in advance guys!
Try
x_, y_ = np.meshgrid(x, y)
z_grid = np.array(z).reshape(2,7)
fig = plt.figure()
ax1 = plt.contourf(x_,y_,z_grid)
plt.show()
Edit: If you would like to smooth, as per your comment, you can try something like scipy.ndimage.zoom() as described here, i.e., in your case
from scipy import ndimage
z_grid = np.array(z).reshape(2,7)
z_grid_interp = ndimage.zoom(z_grid, 100)
x_, y_ = np.meshgrid(np.linspace(-3,3,z_grid_interp.shape[1]),np.linspace(1,2,z_grid_interp.shape[0]))
and then plot as before:
fig = plt.figure()
ax1 = plt.contourf(x_,y_,z_grid_interp)
plt.show()
This is one way where you use the shape of the meshgrid (X or Y) to reshape your z array. You can, moreover, add a color bar using plt.colorbar()
import matplotlib.pyplot as plt
import numpy as np
x = [-3,-2,-1,0,1,2,3]
y = [1,2]
z = np.array([7,5,6,5,1,0,9,5,3,8,3,1,0,4])
X, Y = np.meshgrid(x, y)
print (X.shape, Y.shape)
# (2, 7) (2, 7) Both have same shape
Z = z.reshape(X.shape) # Use either X or Y to define shape
fig = plt.figure()
ax1 = plt.contourf(X, Y, Z)
plt.colorbar(ax1)
plt.show()
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 3 )
y = np.linspace(0, 3, 4)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.contour(X, Y, Z, cmap='RdGy');

How to plot 3D surface with X, Y, Z when Z is a list of list in Python?

In my case, X is a range(0, 100), Y is a range(0, 10), Z is a list of list. Z has the same length as X, which is 100, and each element list inside of Z has the same dimension of Y.
Z = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ..., [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]].
I have the following code, but it does not work, it complains two or more arrays have incompatible dimensions on axis 1.
fig = plt.figure(figsize=(200, 6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.set_xticklabels(x_ax)
ax.set_yticklabels(y_ax)
ax.set_title("my title of chart")
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_zlim(0, 100)
fig.colorbar(surf, shrink = 0.5, aspect = 5)
plt.show()
I guess the error is due to the data structure of Z, how do I make a compatible structure with X, and Y? Thanks
Here is a basic 3D surface plotting procedure. It seems that your X and Y are just 1D arrays. However, X, Y, and Z have to be 2D arrays of the same shape. numpy.meshgrid function is useful for creating 2D mesh from two 1D arrays.
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(np.linspace(-2,2,100))
y = np.array(np.linspace(-2,2,10))
X,Y = np.meshgrid(x,y)
Z = X * np.exp(-X**2 - Y**2);
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink = 0.5, aspect = 5)
plt.show()

Categories