I am given a bunch of x,y-coordinates. I want to plot these coordinates, but depending on their values, I want to give them a different color.
Apparently, this can easily be done by providing the cmap
parameter to pyplot's scatter function:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
rng = np.random.RandomState(1)
data = rng.randn(500,2)
plt.scatter(data[:,0], data[:,1], s=20, c=data[:,0], cmap='jet')
plt.show()
This code snippet does not perform the way I want it to, though: The code I have provided will colorize points according to their position along the x-axis only. I want a color code that takes both x- and y-coordinates into account. Points located at the top left corner of my figure are suppossed to look alike. But they are also suppossed to look different from points in the bottom left corner.
I hope I have made my question a bit clearer. Thx for any help!
If you want to plot each quadrant in a different colour, you need to somehow map your data into 4 different numbers, 1 per quadrant. Here is a crude way to do this:
quadrant = (data[:, 0] > 0).astype(int) + 2 * (data[:, 1] > 0).astype(int)
now call
plt.scatter(data[:,0], data[:,1], s=20, c=quadrant, cmap='jet')
note if this is what you wanted, you might prefer a qualitative colormap over jet
if you want a smooth transition, you would need to map each of your dimensions to an orthogonal colour dimension. Here is how to do it in the RGB colour space, but this will look a bit ugly:
def normalize(arr):
arr_min = np.min(arr)
arr_max = np.max(arr)
return (arr - arr_min) / (arr_max - arr_min)
red = normalize(data[:, 0])
green = normalize(data[:, 1])
blue = np.zeros_like(red)
rgb = np.vstack((red, green, blue)).T
rgb = np.vstack((red, green, blue)).T
Now we have mapped red on the x-axis and green on the y-axis. So a point in the upper right is yellow as it has max red and green.
Change your c
parameter, eg:
plt.scatter(data[:,0], data[:,1], c=np.abs(data[:,0]))
You can also use eg c=data[:,0]**2+data[:,1]**2
.
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.