[英]How to make data points in a 3D python scatter plot look like “discs” instead of “spheres”
[英]Scatter Plot 3D with labels and spheres
我試圖用一些數據點(x,y,z,半徑)制作一個散點圖,這是我到目前為止的結果:
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{\text{Mpc}}{h}\right]$')
ax.set_ylabel(r'$y$ $\left[\frac{\text{Mpc}}{h}\right]$')
ax.set_zlabel(r'$z$ $\left[\frac{\text{Mpc}}{h}\right]$')
#plt.savefig('spheres.png')
plt.show()
如何改善此圖以使x和y標簽與抽搐沒有重疊?
並且有可能在這個3D情節中制作球體而不是區域嗎?
您可以創建由所描述的每個位置繪制一個使用領域,而不是圓標記的曲線在這里 。 這是一個例子:
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()
要解決該標簽定位問題,嘗試添加第二行所描述的位置 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.