如何在Matplotlib中绘制三维热图颜色

2024-06-25 06:50:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Matplotlib 3D绘制数据集的3维,如下所示: enter image description here

但现在我还想把第四维度(0到20之间的标量值)想象成一个热图。所以基本上,我希望每个点都是基于第四个维度的值的颜色。

Matplotlib中有这样的东西吗?如何将一组介于[0-20]之间的数字转换为热图颜色?

我从这里拿走了代码:http://matplotlib.org/mpl_examples/mplot3d/scatter3d_demo.py


Tags: 数据代码orghttpmatplotlib颜色绘制数字
1条回答
网友
1楼 · 发布于 2024-06-25 06:50:03

是的,像这样的:

更新这是一个带有颜色条的版本。

import numpy as np
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure(figsize=(8,6))

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

xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, 0, 100)

colmap = cm.ScalarMappable(cmap=cm.hsv)
colmap.set_array(zs)

yg = ax.scatter(xs, ys, zs, c=cm.hsv(zs/max(zs)), marker='o')
cb = fig.colorbar(colmap)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')


plt.show()

看起来像:

colbar

更新下面是一个用第4维属性着色数据点的显式示例。

import numpy as np
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure(figsize=(8,6))

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

xs = randrange(n, 0, 100)
ys = randrange(n, 0, 100)
zs = randrange(n, 0, 100)
the_fourth_dimension = randrange(n,0,100)

colors = cm.hsv(the_fourth_dimension/max(the_fourth_dimension))

colmap = cm.ScalarMappable(cmap=cm.hsv)
colmap.set_array(the_fourth_dimension)

yg = ax.scatter(xs, ys, zs, c=colors, marker='o')
cb = fig.colorbar(colmap)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')


plt.show()

4dcols

相关问题 更多 >