带有标签和球体的散点图 3D [英] Scatter Plot 3D with labels and spheres
本文介绍了带有标签和球体的散点图 3D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用一些数据点(x、y、z、半径)绘制散点图,这是我目前的结果:
将 numpy 导入为 np从 mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 pltx = np.random.rand(20)y = np.random.rand(20)z = np.random.rand(20)r = np.random.rand(20)plt.rc('text', usetex=True)plt.rcParams['text.latex.preamble']=[r"usepackage{amsmath}"]fig = plt.figure()ax = fig.add_subplot(111, 投影='3d')ax.scatter(x, y, z, s=np.pi*r**2*100, c='blue', alpha=0.75)ax.set_xlabel(r'$x$ $left[frac{ ext{Mpc}}{h}
ight]$')ax.set_ylabel(r'$y$ $left[frac{ ext{Mpc}}{h}
ight]$')ax.set_zlabel(r'$z$ $left[frac{ ext{Mpc}}{h}
ight]$')#plt.savefig('spheres.png')plt.show()
如何改进此图以使 x 和 y 标签与抽动不重叠?
是否有可能在这个 3D 图中制作球体而不是区域?
解决方案
您可以创建一个使用球体而不是圆形标记的绘图,方法是在每个位置绘制一个标记,如.
I am trying to make a scatter plot with some data points (x,y,z,radius) and this is my result up to now:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
x = np.random.rand(20)
y = np.random.rand(20)
z = np.random.rand(20)
r = np.random.rand(20)
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble']=[r"usepackage{amsmath}"]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, s=np.pi*r**2*100, c='blue', alpha=0.75)
ax.set_xlabel(r'$x$ $left[frac{ ext{Mpc}}{h}
ight]$')
ax.set_ylabel(r'$y$ $left[frac{ ext{Mpc}}{h}
ight]$')
ax.set_zlabel(r'$z$ $left[frac{ ext{Mpc}}{h}
ight]$')
#plt.savefig('spheres.png')
plt.show()
How can I improve this plot in order to have no overlap of the x- and y-labels with the tics?
And it there a possibility to make spheres instead of areas in this 3D-plot?
解决方案
You can create a plot that uses spheres instead of circle markers by drawing one at each location as described here. Here's an example:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def drawSphere(xCenter, yCenter, zCenter, r):
#draw sphere
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)
# shift and scale sphere
x = r*x + xCenter
y = r*y + yCenter
z = r*z + zCenter
return (x,y,z)
x = 10*np.random.rand(20)
y = 10*np.random.rand(20)
z = 10*np.random.rand(20)
r = np.random.rand(20)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# draw a sphere for each data point
for (xi,yi,zi,ri) in zip(x,y,z,r):
(xs,ys,zs) = drawSphere(xi,yi,zi,ri)
ax.plot_wireframe(xs, ys, zs, color="r")
plt.show()
To fix the label positioning problem try adding a second line as described here.
这篇关于带有标签和球体的散点图 3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文