I want to plot a scatter plot with 4 specified colors of my choice, preferably a good combination with RGB, when provided a weighting array like the following example where I am plotting with 3 colors.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])
color = [[0.8, 0.1, 0.1],
[0.1, 0.8, 0.1],
[0.1, 0.1, 0.8],
[0.1, 0.8, 0.1]]
plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)
Since by default python makes this an RGB plot the plot looks like this --
Now, If I want to plot with a color
array that has 4 elements the plot is not so good to illustrate information in useful way in the RGBA plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])
color = [[0.9, 0.1, 0.1, 0.1],
[0.1, 0.9, 0.1, 0.1],
[0.1, 0.1, 0.9, 0.1],
[0.1, 0.1, 0.1, 0.9]]
plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)
because the plot looks like this --
what is the best way to have specified colors, for example, red, green, blue and golden?
CodePudding user response:
You can just select and use hex values if you need a specific color.
color = ['#ff0000', '#00ff5d', '#0092ff', '#ffe800']
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[2,3],[1,4]])
color = ['#ff0000', '#00ff5d', '#0092ff', '#ffe800']
plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color)
plt.show()
CodePudding user response:
You can create a RGBA array and then scale it accoardingly (the values inside the array have to be float values in [0, 1]
).
The last entry, A, of the RGBA stands for alpha (transparency, 0 means invisible). You may want to set them to higher values if you want to observe the dots easily.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2])
y = np.array([[2, 3],[1, 4]])
color_rgba = np.array([[255, 0, 0, 1],
[0, 255, 0, 1],
[0, 0, 255, 1],
[255, 215, 0, 1]], dtype=float)
color_rgba[:, :3] = color_rgba[:, :3] / 255
plt.scatter(np.repeat(x, y.shape[1]), y.flat, s=100, c=color_rgba)
plt.show()