繁体   English   中英

在 python 中绘制散点图 plot 和 4 colors

[英]plotting scatter plot with 4 colors in python

我想要 plot 一个散点图 plot 和我选择的 4 个指定的 colors,最好是与 RGB 的良好组合,当提供一个权重数组时,如下例我正在绘制 3 colors。

import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])

color = [[0.8, 0.1, 0.1],
         [0.1, 0.8, 0.1],
         [0.1, 0.1, 0.8],
         [0.1, 0.8, 0.1]]

plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)

因为默认情况下 python 使它成为 RGB plot plot 看起来像这样 -

在此处输入图像描述

现在,如果我想要 plot 具有 4 个元素的color数组,则 plot 不能很好地在 RGBA plot 中以有用的方式说明信息。

import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])

color = [[0.9, 0.1, 0.1, 0.1],
         [0.1, 0.9, 0.1, 0.1],
         [0.1, 0.1, 0.9, 0.1],
         [0.1, 0.1, 0.1, 0.9]]

plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)

因为 plot 看起来像这样——

在此处输入图像描述

指定 colors 的最佳方法是什么,例如,红色、绿色、蓝色和金色?

您可以创建一个 RGBA 数组,然后相应地缩放它(数组内的值必须是[0, 1]中的浮点值)。

RGBA 的最后一项 A 代表 alpha(透明度,0 表示不可见)。 如果您想轻松观察这些点,您可能需要将它们设置为更高的值。

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2])
y = np.array([[2, 3],[1, 4]])

color_rgba = np.array([[255, 0, 0, 1],
                        [0, 255, 0, 1],
                        [0, 0, 255, 1],
                        [255, 215, 0, 1]], dtype=float)

color_rgba[:, :3] = color_rgba[:, :3] / 255

plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color_rgba)

plt.show()

在此处输入图像描述

如果您需要特定颜色,您可以只使用 select 并使用十六进制值。

颜色 = ['#ff0000', '#00ff5d', '#0092ff', '#ffe800']

import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])

color = ['#ff0000', '#00ff5d', '#0092ff', '#ffe800']

plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)
plt.show()

我的颜色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM