I'm trying to recreate a 3D scatter plot figure but I'm having a hard time with getting the range right on the y axis.
This is the figure that I am trying to emulate:
Here's my code for the np.arange():
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=[15,15])
z = 520 * np.random.random(100)
x = np.arange(0,20,0.2)
y = np.arange(0,20,0.2)
ax3 = fig.add_subplot(2,2,3, projection='3d')
ax3.set_xlabel('x', c='r', size=14)
ax3.set_ylabel('y', c='r', size=14)
ax3.set_zlabel('z', c='r', size=14)
ax3.scatter3D(x,y,z, c=z, cmap='jet')
ax3.view_init(25,45);
This is the output:
I'm not trying to make it look exactly the same with the angle but I need to get the axis plots correct.
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.
I have a problem when making graphs using matplotlib,
for some simple graphs the library works fine, for example:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
but when I use figure module, i get blank figures, like this
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = np.linspace(0, 10, 200)
y = np.sin(x)
ax.plot(x, y, 'r-', linewidth=2, label=r'$y=\sin(x)$', alpha=0.6)
ax.legend(loc='upper center')
plt.show()
I've tried the same code in a a different python editor or using the shell and it woks fine. I'm using :
Windows,
PyDev v4.2,
python v2.7.10
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:
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()