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)
Related
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()
This question already has answers here:
show origin axis (x,y) in matplotlib plot
(3 answers)
Closed 2 years ago.
So I am working on a program that displays the graph of a function over an interval, and the plot size is automatically handled by matplotlib. The only thing is, it resizes without showing x=0 and y=0 cartesian axes. Everything I tried so far, like plt.subplot(), only affects the axes that show at the bottom and left, not the cartesian axes. Is there a way to add the axes in?
Here is some example code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 1, 100)
f = lambda x: x**2 - 1
plt.plot(x, f(x))
plt.show()
The graph that comes from this looks like this:
which does not show the cartesian axes. Is there a way to add this in, maybe by adding lines at x=0 and y=0?
You can set the spine axis to be in a custom position, like the origin:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2,1,100)
y = x**2
fig, ax = plt.subplots(1, figsize=(6, 4))
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.set(ylim=(-1, 4))
Otherwise, you can add a vertical and a horizontal line:
fig, ax = plt.subplots(1, figsize=(6, 4))
ax.plot(x, y)
ax.axhline(0, color='black')
ax.axvline(0, color='black')
You can do it by drawing arrows:
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
x = np.linspace(-2, 1, 100)
f = lambda x: x**2 - 1
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
plt.plot(x, f(x))
l,r = ax.get_xlim()
lo,hi = ax.get_ylim()
arrow( l-1, 0, r-l+2, 0, length_includes_head = False, head_width = 0.2 )
arrow( 0, lo-1, 0, hi-lo+2, length_includes_head = True, head_width = 0.2 )
plt.show()
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
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()
I am using mplot3d from the mpl_toolkits library. When displaying the 3D surface on the figure I'm realized the axis were not positioned as I wished they would.
Let me show, I have added to the following screenshot the position of each axis:
Is there a way to change the position of the axes in order to get this result:
Here's the working code:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(plt.figure())
def f(x,y) :
return -x**2 - y**2
X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)
# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])
ax.set_yticklabels([-1,1],rotation=-15, va='center', ha='right')
plt.show()
I have tried using xaxis.set_ticks_position('left') statement, but it doesn't work.
No documented methods, but with some hacking ideas from https://stackoverflow.com/a/15048653/1149007 you can.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = ax = fig.add_subplot(111, projection='3d')
ax.view_init(30, 30)
def f(x,y) :
return -x**2 - y**2
X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)
# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])
ax.xaxis._axinfo['juggled'] = (0,0,0)
ax.yaxis._axinfo['juggled'] = (1,1,1)
ax.zaxis._axinfo['juggled'] = (2,2,2)
plt.show()
I can no idea of the meaning of the third number in triples. If set zeros nothing changes in the figure. So should look in the code for further tuning.
You can also look at related question Changing position of vertical (z) axis of 3D plot (Matplotlib)? with low level hacking of _PLANES property.
Something changed, code blow doesn't work, all axis hide...
ax.xaxis._axinfo['juggled'] = (0,0,0)
ax.yaxis._axinfo['juggled'] = (1,1,1)
ax.zaxis._axinfo['juggled'] = (2,2,2)
I suggest using the plot function to create a graph