csv 根据Z值为3D图指定3种颜色

lndjwyie  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(132)

我试图让我的3D python图根据CSV文件中的Z值变成3种不同的颜色。我试图给一个点涂上一种特定的颜色,然后点下面的一种颜色和点上面的一种颜色。我可以让图变成点上面和下面的颜色,但我似乎不知道如何让它变成3种颜色。
我尝试将Z值分成3个不同的3个子集,但当我试图绘制图时,图只是空的。我还尝试通过if语句将Z分配给颜色来编写它,但也不起作用。这是适用于2颜色的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd

headers = ['name','ra','x rads','x par','dec','y rads','Parallax','Parallax Error','central distance','Z Max','Z Min']
mergeddata = pd.read_csv(r'C:\Users\GregL\Downloads\mergedata - no neg parallax #s (2).csv')

mergeddata.z = mergeddata['central distance']
mergeddata.x = mergeddata['x par']
mergeddata.y = mergeddata['y rads']

x= mergeddata.x
y= mergeddata.y
z = mergeddata.z

colors = [z <= 1956.783590]

fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111, projection='3d')

surf=ax.scatter3D(x,y,z,c=colors, cmap='coolwarm',s=.5,marker='^')

ax.set_title('3D Data Distance Plot')

ax.set_zlim(-100,10000)
ax.set_xlim(-50,50)
ax.set_ylim(-50,50)

ax.set_xlabel('RA')
ax.set_ylabel('DEC')
ax.set_zlabel('CENTRAL DISTANCE')

plt.show()

得到了这个图

zzoitvuj

zzoitvuj1#

正如Claudio在评论中提到的,你可以创建一个颜色值,然后分配一个合适的颜色Map表。在这里,我将基于Matplotlib的Tab10创建一个离散的颜色Map表:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as col

fig = plt.figure()
ax = plt.axes(projection ='3d')

z = np.linspace(0, 1, 100)
x = z * np.sin(25 * z)
y = z * np.cos(25 * z)

# values for color
c = [int(zv / 0.4) for zv in z]
# discrete colormap with 3 colors
cmap=col.ListedColormap(cm.tab10.colors[:len(np.unique(c))])
ax.scatter(x, y, z, c=c, cmap=cmap)
plt.show()

或者,您可以创建多个ax.scatter命令,每个命令绘制一个子集。这种方法的优点是您可以为每个子集设置自定义标签或渲染属性:

fig = plt.figure()
ax = plt.axes(projection ='3d')

i1 = z < 0.3
i2 = (z >= 0.3) & (z < 0.6)
i3 = z >= 0.6

ax.scatter(x[i1], y[i1], z[i1], label="a")
ax.scatter(x[i2], y[i2], z[i2], label="b")
ax.scatter(x[i3], y[i3], z[i3], label="c")
ax.legend()
plt.show()

相关问题