mplot3d: how to show the ticks but not the grids? - python

I want to show the axis ticks with matplotlib/mplot3d but not the faint grids on the x/y/z background:
Is there a way to suppress the grids?

Calling ax.grid(False) should suffice. Self contained example, adding that line to this:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.grid(False)
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()

Related

plt.show() does not show the 3d scatter image

community,
I tried to create the 3d scatter by using matplotlib Axes3D on jupyter notebook.
However, it is not showing the image once I execute 'plt.show()'.
#pip install matplotlib
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax =fig.add_subplot(111, projection = '3d')
x = dframe['CTR']
y = dframe['Clicks']
z = dframe['Avg. CPC']
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
Your code works fine like this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
# dummy data (your actual data should go here)
x = [1, 2, 3, 4]
y = x
z = x
ax.scatter(x, y, z, c="r", marker="o")
plt.show()
This shows:
May be something is wrong with your data. Also, since you are using plt.show() anyway, you can remove the %matplotlib inline line.

Matplotlib 3d Plot Colorbar Scale

I have a 3d plot with a colorbar and I would like the colorbar's size to scale with the size of the projection, no matter the orientation I select with ax.view_init.
It would also be great if I could get the aspect ratio of the 3d plot to be equal at the same time as well.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.colors
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=90, azim=0)
x = np.arange(3)
X,Y = np.meshgrid(x,x)
Z = np.ones_like(X)
V = np.array([[3,2,2],[1,0,3],[2,1,0]])
norm = matplotlib.colors.Normalize(vmin=0, vmax=3)
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(norm(V)), shade=False)
m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm)
m.set_array([])
plt.colorbar(m)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
Example code stolen shamelessly from this question

Create Sea Surface Plot with Matplotlib3D wireframe

I want to create a plot which looks similar to: https://www.shutterstock.com/de/image-vector/vector-abstract-3d-wave-wireframe-surrounding-445020520
I use something like the wire frame demo with a different background (best case a blue color gradient):
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', axisbg='blue')
# Grab some test data, e.g.gcreate noisy sea surface data
X, Y, Z = axes3d.get_test_data(0.05)
# Plot a basic white wireframe for the surface
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10, color='white')
plt.show()
What would you take to make this more look like the example?
Cheers Bene
Here is one (out of many) options. Using a surface plot in the background of the wireframe can give the plot some shading and make it more look like the picture.
import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', facecolor='#1a5a98')
plt.subplots_adjust(0,0,1,1)
fig.patch.set_color('#1a5a98')
#Generate some data
x = np.linspace(0,500,501)/(9*np.pi)
X,Y = np.meshgrid(x,x)
f = lambda x,y: np.sin(x+y**0.2)*np.cos(y-x**0.4)
Z = f(X,Y)
# plot surface, for nice shading
ax.plot_surface(X, Y, Z, rstride=10, cstride=10)
# plot wireframe for white lines on top
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10, color='white', linewidth=0.5)
#set axis off
ax.axis("off")
ax.set_xlim(6.42,11.17)
ax.set_ylim(6.42,11.17)
ax.set_zlim(-6,6)
ax.view_init(elev=25, azim=-88)
plt.show()

pylab 3d scatter plots with 2d projections of plotted data

I am trying to create a simple 3D scatter plot but I want to also show a 2D projection of this data on the same figure.
This would allow to show a correlation between two of those 3 variables that might be hard to see in a 3D plot.
I remember seeing this somewhere before but was not able to find it again.
Here is some toy example:
x= np.random.random(100)
y= np.random.random(100)
z= sin(x**2+y**2)
fig= figure()
ax= fig.add_subplot(111, projection= '3d')
ax.scatter(x,y,z)
You can add 2D projections of your 3D scatter data by using the plot method and specifying zdir:
import numpy as np
import matplotlib.pyplot as plt
x= np.random.random(100)
y= np.random.random(100)
z= np.sin(3*x**2+y**2)
fig= plt.figure()
ax= fig.add_subplot(111, projection= '3d')
ax.scatter(x,y,z)
ax.plot(x, z, 'r+', zdir='y', zs=1.5)
ax.plot(y, z, 'g+', zdir='x', zs=-0.5)
ax.plot(x, y, 'k+', zdir='z', zs=-1.5)
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([-0.5, 1.5])
ax.set_zlim([-1.5, 1.5])
plt.show()
The other answer works with matplotlib 0.99, but 1.0 and later versions need something a bit different (this code checked with v1.3.1):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x= np.random.random(100)
y= np.random.random(100)
z= np.sin(3*x**2+y**2)
fig= plt.figure()
ax = Axes3D(fig)
ax.scatter(x,y,z)
ax.plot(x, z, 'r+', zdir='y', zs=1.5)
ax.plot(y, z, 'g+', zdir='x', zs=-0.5)
ax.plot(x, y, 'k+', zdir='z', zs=-1.5)
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([-0.5, 1.5])
ax.set_zlim([-1.5, 1.5])
plt.show()
You can see what version of matplotlib you have by importing it and printing the version string:
import matplotlib
print matplotlib.__version__

plot a 3d surface plot using matplotlib

Hello all I have lists of x coordinates and y and Z. Now I want to use matplotlib to plot a 3d surface. I tried to write the code, but the output was blank.
Can any one help me with this:
this is the code:
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
z=[-3,-6,10,8,23,75]
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.plot_surface(x,y, z)
#ax.view_init(90, -90)
plt.show()
The values I gave are random but generally I get the values in a list like the above x,y,z lists. Now can anyone point out the error I making in the code so that i can correct it
I want a graph like the one below:
The code which I have written for plotting 3d surface is below:
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import math
f=open(filedir+'/'+'temp_1.txt','r')
for line in f:
line=line.strip()
temp=line.split(',')
temp1=float(temp[0])
temp2=float(temp[1])
temp3=float(temp[2])
#num_z=math.log(temp3)
num_x=temp1
num_y=temp2
num_z=temp3
x.append(num_x)
y.append(num_y)
z.append(num_z)
for num in range(len(z)):
if z[num] != 0:
z1.append(math.log(z[num]))
else:
z1.append(0)
xi=np.linspace(min(x),max(x))
yi=np.linspace(min(y),max(y))
min_x=min(x)-2
max_x=max(x)+2
min_y=min(y)-2
max_y=max(y)+20
min_z=min(z1)-2
max_z=max(z1)+2
pi=3.14
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('Frequency')
ax.set_ylabel('Distance')
ax.set_zlabel('Sound_Pressure Level')
X,Y=np.meshgrid(xi,yi)
Z=griddata(x,y,z1,xi,yi)
surf=ax.plot_surface(X,Y,Z,rstride=5,cstride=5,cmap=cm.jet,linewidth=1,antialiased=True)
#ax.view_init(90, -90)
cset = ax.contour(X, Y, Z, zdir='z', offset=min_z, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=min_x, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=max_y, cmap=cm.coolwarm)
ax.set_xlim3d(min_x, max_x)
ax.set_ylim3d(min_y,max_y)
ax.set_zlim3d(min_z,max_z)
plt.show()
The input looks like this and it's in a .txt file:
1.0000000E+01,3,4.826432E-11
1.0000000E+01,4,4.342127E-11
1.0000000E+01,5,3.861855E-11
1.0000000E+01,6,3.588365E-11
1.0000000E+01,7,3.244975E-11
1.0000000E+01,8,2.986569E-11
1.0000000E+01,9,2.740276E-11
1.0000000E+01,10,2.604628E-11
2.0000000E+01,-11,4.177395E-11
2.0000000E+01,-10,4.658516E-11
2.0000000E+01,-9,5.209122E-11
2.0000000E+01,-8,5.575429E-11
2.0000000E+01,-7,5.808602E-11
2.0000000E+01,-6,5.876480E-11
2.0000000E+01,-5,5.803726E-11
2.0000000E+01,-4,5.566828E-11
2.0000000E+01,-3,5.084253E-11
2.0000000E+01,-2,4.771793E-11
2.0000000E+01,-1,4.176435E-11
2.0000000E+01,0,3.828995E-11
This is a sample input file and it has 1700 lines like this. the output is:

Categories