Python 3D散点图颜色图问题 [英] Python 3d scatterplot colormap issue

查看:50
本文介绍了Python 3D散点图颜色图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四维数据(x、y、z 位移;以及各自的电压),我希望将它们绘制在 Python 的 3d 散点图中.我已经绘制了3d图,但是我想使用色图改变点的颜色,具体取决于点电压的大小.

I have four dimensional data (x, y, z displacements; and respective voltages) which I wish to plot in a 3d scatterplot in python. I've gotten the 3d plot to render, but I want to have the colour of the points change using a colourmap, dependent upon the magnitude of the point's voltage.

我已经尝试了一些东西,但似乎无法让它工作我收到错误ValueError:无法转换参数类型<type 'numpy.ndarray'>到 rgba 数组.我不确定如何转换我需要转换的内容,所以如果有人可以提供一些帮助,我将不胜感激.

I've tried a few things, but can't seem to get it to work I'm getting the error ValueError: Cannot convert argument type <type 'numpy.ndarray'> to rgba array. I'm not sure exactly how to convert what I need to convert, so if anybody could please offer some help, I'd be most appreciative.

我的代码在这里:

fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D
cmhot = plt.cm.get_cmap("hot")
ax = fig.add_subplot(111, projection='3d',)
ax.scatter(x, y, z, v, s=50, c = cmhot)
plt.show()

推荐答案

ax.scatter 可以采用颜色参数 c ,它是标量的序列(例如, list array ),和 cmap 参数来指定颜色映射.因此,为了使颜色根据电压的幅度而变化,您可以定义:

ax.scatter can take a color parameter c which is a sequence (e.g. a list or an array) of scalars, and a cmap parameter to specify a color map. So to make the colors vary according to the magnitude of the voltages, you could define:

c = np.abs(v)

这使正负电压具有相同的颜色.相反,如果您希望每种颜色(正色或负色)都有自己的颜色,则可以使用 c = v .

This makes positive and negative voltages have the same color. If instead you wished each color (positive or negative) to have its own color, you could just use c = v.

例如,

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x, y, z, v = (np.random.random((4,100))-0.5)*15
c = np.abs(v)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = plt.get_cmap("hot")
cax = ax.scatter(x, y, z, v, s=50, c=c, cmap=cmhot)

plt.show()

这篇关于Python 3D散点图颜色图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆