1

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

enter image description here

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

enter image description here

what is the best way to have specified colors, for example, red, green, blue and golden? in other words, can I make the scatter plot in a way so that first column of color array represents red, second column represents green, third represents blue and fourth column represents golden (instead of transparency). And the values in each column will specify the shade of the specific color.

2 Answers 2

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. But I was looking for something that can help me visualize an array which is of size N by 4 and contains some normalized values between 0 and 1. With my previous method of three colors it was very convenient to illustrate those results with red green and blue. But the challenge is I cannot preselect the color information. Do you have any suggestions for that?
0

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

My Colors

2 Comments

Is this what you were looking for? There are various options present ! [link]matplotlib.org/stable/gallery/color/… [lnik]matplotlib.org/stable/gallery/color/index.html
I have to get the color information from the ‘color’ array which is the information I want to visualize on the plot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.