0

I am trying to re-create this plot:

Desired plot

This is what I have so far:

Current plot

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!

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • is this question similar to what you want? https://stackoverflow.com/questions/17682216/scatter-plot-and-color-mapping-in-python – Thulfiqar May 26 '21 at 20:57

2 Answers2

0

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()

scatter plot with rgb color values

JohanC
  • 71,591
  • 8
  • 33
  • 66
0

This worked for me but I used Black to Green instead of Black to Red.

limits = [-2.25, 2.25, -2.25, 2.25]

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

black = np.array([0, 0, 0])
green = np.array([0, 1, 0])
t = np.linspace(0, 1, 25).reshape(-1, 1)
colors = t * green + (1 - t) * black

s = np.linspace(0, 1, 25)
sizes = s * (100 - 10) + 10

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