Matplotlib drawing 3d Lines with solid of revolution - python

I want to draw a solid 3D line, I have drawn a 3D line, but I want to draw this line with solid of revolution, so my line creates with f.ex. diameter 10 mm along the 3D line. How could that be achieved?
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(0, 40, 1000)
y = np.sin(x)
z = np.cos(x)
fig = plt.figure(figsize=(8, 6))
ax = Axes3D(fig)
ax.plot(x, y, z, color="green")
ax.set(xlabel="X", ylabel="Y", zlabel="Z")
ax.set_yticks([-1, 0, 1])
ax.set_yticklabels(['min', 0, 'max'])
plt.show()

Related

How to rearrange the axes in a 3D plot?

Here is the code I'm running to plot a 2D ellipse on the "z=10 wall" of the plot:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Draw a circle on the z=10 'wall'
p = Ellipse((0, 0), 6, 3, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z= 10, zdir="z")
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.set_zlim(-10, 10)
plt.show()
However it seems that the depth axis here is considered to be y:
I'd like to have the y axis be what the z axis is right now, the x axis be what the y axis is right now, and the z axis be what the x axis is right now. Would you know how to rearange these axes?
ax.view_init(elev=30, azim=45)
ax.view_init(elev=30, azim=-60)

Scatter plot with a circle in it

I was planning to do a scatter plot with 5000 data points with a line of unit circle in the same plot, but the code I have right now show a pretty small plot with huge dots in it. I tried to make this plot size larger but plt.figure(figsize=(12,12)) doesn't work....wonder what did I do wrong?
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
x=scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
y=scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
a=np.cos(np.linspace(0, 2*np.pi, 200))
b=np.sin(np.linspace(0, 2*np.pi, 200))
plt.scatter(x,y)
plt.plot(a,b, color="red")
plt.figure(figsize=(12,12))
plt.show()
plt.figure() creates a new empty plot. You should call it before the other plotting functions
You can set a smaller dotsize, e.g plt.scatter(x, y, s=1). To force that the circle is shown as a circle, set an equal aspect ratio (set_aspect('equal')).
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
x = scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
y = scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
a = np.cos(np.linspace(0, 2 * np.pi, 200))
b = np.sin(np.linspace(0, 2 * np.pi, 200))
plt.figure(figsize=(12, 12))
plt.scatter(x, y, s=1)
plt.plot(a, b, color="red")
plt.gca().set_aspect('equal')
plt.show()
To change the figsize after the plot has been created, you can use:
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(15, 15)
Matplotlib also provides a function to create a circle. Here is an example setting a semi-transparent facecolor:
import scipy.stats
import matplotlib.pyplot as plt
x = scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
y = scipy.stats.uniform.rvs(loc=-1, scale=2, size=5000)
plt.figure(figsize=(12, 12))
plt.scatter(x, y, s=1)
ax = plt.gca()
ax.add_patch(plt.Circle((0, 0), 1, facecolor='#FF000011', edgecolor='red'))
ax.set_aspect('equal')
plt.show()

Scatter plot does not appear on the foreground on top of contourf plot

My code is the following and I believe should produce a chart where a scatter plot is superimposed on a contourf plot (i.e. appears on the foreground)
But that does not happen.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(examples[:,0], examples[:, 1])
ax.contourf(x, y, Z)
I expected that the scatter plot below would be superimposed on the contourf plot:
plt.scatter(x = examples[:,0], y = examples[:, 1])
Why is this and how the code should be changed?
Just swap contourf and scatter order:
import numpy as np
import matplotlib.pyplot as plt
N = 1000
xl = np.linspace(0, 10, N)
yl = np.linspace(0, 10, N)
x, y = np.meshgrid(xl, yl)
Z = x**2 + y**2
examples = np.random.uniform(low = 0, high = 10, size = (10, 2))
fig, ax = plt.subplots()
ax.contourf(x, y, Z)
ax.scatter(examples[:,0], examples[:, 1], color = 'red')
plt.show()
The last plot line you write overlaps the previous one.

How do you plot vertical 3D planes?

see picture
Hey, I want to plot a function in 3d matplotlib python. The functions I want to plot are x = i where i stretches from 0 to 1 with increments of 0.20. So basically 4 vertical planes just as in the picture I shared.
You can create the planes as surface plots.
Here's an example:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
X, Y = np.meshgrid(np.arange(-6, 6), np.arange(-6, 6))
Z = 0*X
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, alpha=0.5) # the horizontal plane
ax.plot_surface(Z, Y, X, alpha=0.5) # the vertical plane

Matplotlib: Have 3d orthogonal subplot share axis with 2d plot

I intend to plot an axis-aligned view on 3d objects with projection="ortho" side by side with 2d (profile) data, but I just cannot figure out how to make the vertical axes match.
In the following example I would like to have the second and the third subplot share the vertical axes:
Here is the corresponding code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot 3d view
fig = plt.figure(figsize=(16,4))
ax1 = fig.add_subplot(131, projection='3d')
ax1.view_init(40, 60)
surf = ax1.plot_surface(X, Y, Z, cmap=cm.viridis,linewidth=0)
# plot one side
ax2 = fig.add_subplot(132, projection='3d', proj_type = 'ortho')
ax2.view_init(0, 0)
surf = ax2.plot_surface(X, Y, Z, cmap=cm.viridis, linewidth=0)
ax2.set_zlim([-0.2,1])
# plot some 2d information
ax3 = fig.add_subplot(133)
ax3.set_ylim([-0.2,1])
plt.show()

Categories

Resources