Bothering frame on matplotlib 3D plots - python

I make 3d plots with matplotlib and I always get a weird frame with a normalized scale around my plot. Where does it come from and how can I get rid of it ?
Here is an example code that drives me to the problem :
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.linspace(0,10)
# ------------- Figure ---------------
fig, ax = plt.subplots(figsize = (9,6))
ax = fig.gca(projection='3d')
ax.plot(np.sin(x), np.cos(y), z)
plt.show()
And here is the result :
I use plt.subplots() because I want a figure with a 3D and a 2D plot side by side.

You call plt.subplots(...) and this, of course, instantiates an Axes, complete of horizontal and vertical spines, before Matplotlib is informed that you want a 3D enabled Axes.
When you later call plt.gca(...) it's too late…
Simply use
fig, ax = plt.subplots(figsize = (9,6), subplot_kw={"projection" : "3d"})
or
fig = plt.figure(figsize = (9,6))
ax = fig.add_subplot(111, projection='3d')
Addressing OP's comment
Figure.add_subplot is pretty flexible…
fig = plt.figure()
fig.add_subplot(1,5,(1,4), projection='3d')
fig.add_subplot(1,5,5)
fig.tight_layout()
plt.show()

Related

How to get octants in matplotlib 3D?

My code is:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
After plotting some points, when I use the plt.show() method then it displays a 3D axes system but there is only one octant. I need all 8 of them for my project. Is there any way to get them?
Thanks in advance.
It should put your data (presumably negative) in view when you plot it. However, it's worth knowing how to manually set the limits as well:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)

Superimpose independent plots in python

I want to work with only one figure, with multiples, different and modifiable plots, whithout the subplots formalism.
Is there a way to superimpose two differents plots, in the same way as text boxes, i.e anywhere on the figure ?
Here a "gimp made" example :
Thanks !
You can use figure.add_axes to place an axes at an arbitrary location.
fig = plt.figure()
fig.add_axes([0.1,0.2,0.3,0.4])
places an axes at x=0.1, y=0.2, width=0.3, height=0.4 in figure coordinates.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_axes([0.4,0.1,0.5,0.6], projection='3d')
X, Y = np.meshgrid(np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25))
Z = np.sin(np.sqrt(X**2 + Y**2))
surf = ax.plot_surface(X, Y, Z, cmap="plasma")
ax = fig.add_axes([0.3,0.4,0.3,.4])
plt.plot([1,2,3])
plt.show()

Python matplotlib wireframe distortion

I am trying to create a plot composed of multiple wireframe spheres using matplotlib. I found a code fragment to plot one such sphere here so I thought it would be easy to extend it to multiple spheres by just calling plot_wireframe multiple times. I was wrong. Here's a code fragment:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=np.cos(v)
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")
plt.show()
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
spheres = [ [0,0,0,1], [3,0,0,1.6] ]
for v in spheres:
ax.plot_wireframe(x*v[3]+v[0], y*v[3]+v[1], z*v[3]+v[2],linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")
plt.show()
If you run that code, the first plot will show a nice sphere, while in the second all the spheres are distorted and shifted. I searched to make sure plot_wireframe can be called multiple time on the same axis but couldn't find anything. Also, I'm a Python noob, but I don't think I'm doing anything wrong.
Thank you for the help!
Short answer: adjust the axes limits manually:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=np.cos(v)
# I'm not sure what was this for.
'''
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")
plt.show()
'''
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
spheres = [ [0,0,0,1], [3,0,0,1.6] ]
for v in spheres:
ax.plot_wireframe(x*v[3]+v[0], y*v[3]+v[1], z*v[3]+v[2],linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_xlim([0,7]) # Like so.
ax.set_ylim([-3,3])
ax.set_zlim([-3,3])
ax.set_aspect("equal")
plt.show()

Polar plot without grid in matplotlib

Is there a way to turn of the grid for polar plots in matplotlib? I tried matplotlib.pyplot.rgrids([], []), but it doesn't work.
From your axes instance, call grid(False).
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.grid(False)
r = np.arange(0,1,0.001)
theta = 2*2*np.pi*r
ax.plot(theta,r)
plt.show()

How to make a non-square contourf-plot

I'm using contourf in pyplot to plot some scalar data, but when my domain is non-square i feel like the data is misrepresented because it always plots it in a square (though the axis values will increase faster on one side.) How can i force the axis scaling to be equal, such that if my domain is twice as long in the x-direction the image is actually plotted in a rectangle with this property?
I'm doing something like this:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
contour = ax.contourf(X,Y,Z)
fig.colorbar(contour)
fig.canvas.draw()
Using ax.set_aspect:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
x=np.r_[-10:10:100j]
y=np.r_[-20:20:100j]
z= np.add.outer(x*x, y*y)
contour=ax.contour(x,y,z)
fig.colorbar(contour)
ax.set_aspect('equal')
# ax.axis('equal')
plt.show()
yields
while changing ax.set_aspect('equal') to
ax.axis('equal')
yields
This might help:
ax = fig.add_subplot(111, aspect="equal")
You need to change the axis setting:
axis('equal')
See all of the axis settings here:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

Categories

Resources