简体   繁体   English

使用 (n,3) 数组创建散点图,其中 n 是数据集中的数据点数,作为 plt.scatter() 中的“颜色”参数

[英]Creating a Scatterplot using a (n,3) array where n is the number of data points in dataset as the 'color' parameter in plt.scatter()

I am trying to re-create this plot:我正在尝试重新创建这个 plot:

所需的情节

This is what I have so far:这是我到目前为止所拥有的:

当前地块

limits = [-2.25,2.25,-2.25,2.25] # [xmin,xmax,ymin,ymax]

x   = data['x']
y   = data['y']
rows,cols = data.shape

colors = np.array([np.arange(0,1,1/5) for row in range(0,5)]).T

sizes     = np.linspace(1,rows+1, num=rows)

plt.scatter(x, y, s=sizes, c = colors, cmap='gist_heat')
plt.xlabel("y")
plt.ylabel("x")
plt.xlim(limits[0],limits[1])
plt.ylim(limits[2],limits[3])
plt.title('2D Data')
plt.show()

How do I get the black to red fade.如何让黑色变为红色褪色。 Apparently I am suppose to use the colors parameter where 'colors' must be a (n,3) NumPy array, where n is the number of data points and each of the three columns corresponds to an RGB value in the range [0,1]显然我想使用colors参数,其中“颜色”必须是 (n,3) NumPy 数组,其中 n 是数据点的数量,三列中的每一列对应于范围 [0,1 ]

I get better luck using a (5,5) matrix and a cmap.使用 (5,5) 矩阵和 cmap 会获得更好的运气。 Thanks in advance for the help!在此先感谢您的帮助!

An idea is to create rgb-values, where r goes smoothly from 0 to 1 while g and b both go from 0 to 0.3 .一个想法是创建 rgb 值,其中r01平滑,而gb都从00.3 go 。

import matplotlib.pyplot as plt
import numpy as np

limits = [-2.25, 2.25, -2.25, 2.25]  # [xmin,xmax,ymin,ymax]

x = np.repeat(np.linspace(0, 1, 5), 5)
y = np.tile(np.linspace(0, 1, 5), 5)
rows, cols = 5, 5

colors = np.array([np.linspace(0, 1, rows*cols),
                   np.linspace(0, 0.3, rows*cols),
                   np.linspace(0, 0.3, rows*cols)]).T
sizes = np.arange(1, rows*cols + 1)

plt.scatter(x, y, s=sizes, c=colors)
plt.xlabel("y")
plt.ylabel("x")
plt.xlim(limits[0], limits[1])
plt.ylim(limits[2], limits[3])
plt.title('2D Data')
plt.show()

带有 rgb 颜色值的散点图

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

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