简体   繁体   中英

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:

所需的情节

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]

I get better luck using a (5,5) matrix and a 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 .

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 颜色值的散点图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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