三维散点+颜色标记图

1. 问题

类似这样的图是怎么绘制的?
在这里插入图片描述

2. Python方案

刚好看到了:[Pyplot] 绘制三维散点图使用颜色表示数值大小
这篇文章有现成的画法,摘录过来备用(做了一定的优化)。

import matplotlib.ticker
import matplotlib.pyplot as plt
import random
# 1.0 初始化数据
# f(x,y,z) = v
# 其中x,y,z为随机数,v=x*y*z
x = [random.randint(0,50) for i in range(0,20)]
y = [random.randint(0,50) for i in range(0,20)]
z = [random.randint(0,50) for i in range(0,20)]
v = [(x[i]+y[i])*z[i] for i in range(0,20)]
# 1.1 根据各个点的值(v[]),设置点的颜色值,每个点的颜色使用一个rgb三维的元组表示,例如,若想让点显示为红色,则颜色值为(1.0,0,0)
# 设置各个点的颜色
# 每个点的颜色值按照colormap("seismic",100)进行设计,其中colormap类型为"seismic",共分为100个级别(level)
min_v = min(v)
max_v = max(v)
color = [plt.get_cmap("seismic", 20)(int(float(i-min_v)/(max_v-min_v)*100)) for i in v]

# 2.0 显示三维散点图
# 新建一个figure()
fig = plt.figure()
# 在figure()中增加一个subplot,并且返回axes
ax = fig.add_subplot(111, projection='3d')
ax.view_init(
                # elev=30,    # 仰角
             azim=225    # 方位角
            )
# 设置colormap,与上面提到的类似,使用"seismic"类型的colormap,共100个级别
plt.set_cmap(plt.get_cmap("seismic", 20))
# 绘制三维散点,各个点颜色使用color列表中的值,形状为"."
im = ax.scatter(x, y, z,s=200,c=color,marker='.')
# 2.1 增加侧边colorbar
# 设置侧边colorbar,colorbar上显示的值使用lambda方程设置
fig.colorbar(im, format=matplotlib.ticker.FuncFormatter(lambda x,pos:int(x*(max_v-min_v)+min_v)))
# 2.2 增加坐标轴标签
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# 2.3显示
plt.show()

结果如图所示:
在这里插入图片描述
注意:使用的matplotlib包版本为3.5

3. Matlab 方案

% sets=geneParasets();
load paraselect-cold2;
% mea = mea * 100000000;

base = -5:-1;
index = 1;
for i = 1:size(base, 2)
    for j = 1:size(base, 2)
        for k = 1:size(base, 2)
            x = base(i);
            y = base(j);
            z = base(k);
            c = mea(index,1);
            
%             fprintf("%.4f     %.4f      %.4f===       %.4f \n",x,y,z,c);
            scatter3(x, y, z,50,c,'filled');
            hold on;
            index = index+1;
        end
    end
end

title('测试');
xlabel('X');ylabel('Y');zlabel('Z');
colormap summer;
colorbar;

% 
% function sets = geneParasets()
%     sets = [];
%     base = [10e-5,10e-4,10e-3,10e-2,10e-1];
%     for i = 1:size(base, 2)
%         for j = 1:size(base, 2)
%             for k = 1:size(base, 2)
%                 once = [base(i), base(j), base(k)];
%                 sets = [sets; once];
%             end
%         end
%     end
% end

在这里插入图片描述

在不同的编程语言中,有不同的方法来绘制三维,以下介绍Python和R语言的实现方式: ### Python(使用Matplotlib库) ```python import matplotlib as mpl import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # 生成随机数据 xs1 = np.random.randint(30, 40, 100) ys1 = np.random.randint(20, 30, 100) zs1 = np.random.randint(10, 20, 100) xs2 = np.random.randint(50, 60, 100) ys2 = np.random.randint(30, 40, 100) zs2 = np.random.randint(50, 70, 100) xs3 = np.random.randint(10, 30, 100) ys3 = np.random.randint(40, 50, 100) zs3 = np.random.randint(40, 50, 100) # 创建一个画布figure,然后在这个画布上加各种元素 fig = plt.figure() # 将画布作用于 Axes3D 对象上 ax = Axes3D(fig) # 画出(xs1, ys1, zs1)的 ax.scatter(xs1, ys1, zs1) ax.scatter(xs2, ys2, zs2, c='r', marker='^') ax.scatter(xs3, ys3, zs3, c='g', marker='*') # 画出坐标轴 ax.set_xlabel('X label') ax.set_ylabel('Y label') ax.set_zlabel('Z label') plt.show() ``` 上述代码通过`matplotlib`库创建三维,首先生成随机数据,然后创建画布和三维坐标轴对象,最后使用`scatter`方法绘制并设置坐标轴标签,最后显示形[^1]。 ### R语言(使用`scatter3D`函数) ```R library(rgl) # 加载数据 data(iris) # 改版画布版式大小 pmar <- par(mar = c(5.1, 4.1, 4.1, 6.1)) # 绘制三维 with(iris, scatter3D(x = Sepal.Length, y = Sepal.Width, z = Petal.Length, pch = 21, cex = 1.5, col = "black", bg = "#F57446", xlab = "Sepal.Length", ylab = "Sepal.Width", zlab = "Petal.Length", ticktype = "detailed", bty = "f", box = TRUE, theta = 60, phi = 20, d = 3, colkey = FALSE)) ``` 此代码使用R语言的`scatter3D`函数绘制三维,以`iris`数据集的`Sepal.Length`为x轴,`Sepal.Width`为y轴,`Petal.Length`为z轴,并设置了的各种参数,如颜色标记等[^2]。 ### Python(另一个示例) ```python import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 三门课程的成绩 x = [100, 98, 79, 68, 85] y = [95, 99, 80, 60, 90] z = [93, 90, 85, 70, 88] # 绘制三维 fig = plt.figure() ax = Axes3D(fig) ax.scatter(x, y, z) # 添加坐标轴 plt.rcParams['font.sans-serif'] = ['FangSong'] # 设置中文 ax.set_xlabel('课程1') ax.set_ylabel('课程2') ax.set_zlabel('课程3') plt.show() ``` 该示例同样使用Python的`matplotlib`库,以三门课程的成绩为数据绘制三维,并设置了中文坐标轴标签[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值