matplotlib 3D散点图,其标记颜色与RGB值相对应 [英] matplotlib 3D scatterplot with marker color corresponding to RGB values

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

问题描述

我已经使用mahotas将图片加载到numpy数组中.

I have loaded a picture into a numpy array using mahotas.

import mahotas
img = mahotas.imread('test.jpg')

img中的每个像素都由RGB值数组表示:

Each pixel in img is represented by an array of RGB values:

img[1,1] = [254, 200, 189]

我制作了一个3D散点图,其中一个轴为R值,第二轴为G值,第三轴为B值.这没问题:

I have made a 3D scatterplot of R values on one axis, G values on the 2nd axis and B values on the third axis. This is no problem:

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
for i in range(1,img.shape[1]+1):
    xs = img[i,1][0]
    ys = img[i,1][1]
    zs = img[i,1][2]
    ax.scatter(xs, ys, zs, c='0.5', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

(我只是暂时绘制图像的第一列).

(I'm just plotting the first column of the image for the time being).

如何通过每个图像像素的颜色为每个散点图着色?即,我想我想按点的RGB值对点进行着色,但是我不确定是否可行?

How can I color each of the scatterplot dots by the color of each image pixel? i.e. I guess I would like to color the dots by their RGB value, but I'm not sure if this is possible?

推荐答案

是的,您可以执行此操作,但这需要通过不同于c参数的单独机制来完成.简而言之,请使用facecolors=rgb_array.

Yes, you can do this, but it needs to be done through a separate mechanism than the c argument. In a nutshell, use facecolors=rgb_array.

首先,让我解释发生了什么. scatter返回的Collection具有两个用于设置颜色的系统"(由于缺乏更好的用语).

First off, let me explain what's going on. The Collection that scatter returns has two "systems" (for lack of a better term) for setting colors.

如果使用c参数,则通过ScalarMappable系统"设置颜色.这指定应通过将颜色图应用于单个变量来控制颜色. (这是从ScalarMappable继承的任何内容的set_array方法.)

If you use the c argument, you're setting the colors through the ScalarMappable "system". This specifies that the colors should be controlled by applying a colormap to a single variable. (This is the set_array method of anything that inherits from ScalarMappable.)

除了ScalarMappable系统外,还可以独立设置集合的颜色.在这种情况下,您可以使用facecolors kwarg.

In addition to the ScalarMappable system, the colors of a collection can be set independently. In that case, you'd use the facecolors kwarg.

作为一个简单的例子,这些点将具有随机指定的rgb颜色:

As a quick example, these points will have randomly specified rgb colors:

import matplotlib.pyplot as plt
import numpy as np

x, y = np.random.random((2, 10))
rgb = np.random.random((10, 3))

fig, ax = plt.subplots()
ax.scatter(x, y, s=200, facecolors=rgb)
plt.show()

这篇关于matplotlib 3D散点图,其标记颜色与RGB值相对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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