In a few webpages (link1, link2) I have seen people filling 2D planes by colors:
However, these examples are given without source code. How can I generate plot like this in Python? Specifically, if I have array of x
and y
values, how can I write a function color = f(x, y)
, so that scatter(x, y, color)
can generate plot like this?
So far I have tried the following method:
import numpy as np
import matplotlib.pyplot as plt
base_x_color = np.array([0.0, 1.0, 0.0])
base_y_color = np.array([1.0, 0.0, 0.0])
x = np.random.rand(100000)
y = np.random.rand(100000)
color = x[:,None] * base_x_color[None,:] + y[:,None] * base_y_color[None,:]
plt.figure()
plt.scatter(x, y, s=5, c=color)
plt.show()
However, the generated image (shown below) always have one black corner. I would like to set all four corners as non-black colors, and smoothly transit between them.
You can use
numpy.meshgrid
to implement bilinear interpolation:Output: