When saving figures made by matplotlib and coloring the same points in different ways the picture is each time different because of the legend, how to make sure the points would be exactly the same regardless of the legend? (I set a random seed so that at least is fixed)
Assistant
The issue you're experiencing is likely due to the way matplotlib generates random colors for the markers when you don't specify a color explicitly. To ensure that the same points are colored the same way every time, regardless of the legend, you can specify the colors explicitly.
Here are a few ways to do this:
Use a color map
You can use a color map (also known as a colormap) to assign specific colors to specific points. Matplotlib provides several built-in colormaps, such as viridis, plasma, inferno, etc. You can use the cmap parameter to specify the colormap.
import matplotlib.pyplot as plt
import numpy as np
Generate some data
x = np.random.rand(10)
y = np.random.rand(10)
Set the color map
cmap = plt.get_cmap('viridis')
Plot the points with specific colors
plt.scatter(x, y, c=[cmap(i/10) for i in range(10)])
plt.show() Copied2. Use a list of colors
You can specify a list of colors to use for the markers. This way, you can ensure that the same points are always colored the same way.
import matplotlib.pyplot as plt
import numpy as np
plt.show() Copied3. Use a dictionary to map values to colors
If you have a specific mapping of values to colors, you can use a dictionary to map the values to colors.
import matplotlib.pyplot as plt
import numpy as np
Generate some data
x = np.random.rand(10)
y = np.random.rand(10)
values = np.random.randint(0, 5, 10) # example values
plt.scatter(x, y, c=[color_map[v] for v in values])
plt.show() CopiedBy using one of these methods, you should be able to ensure that the same points are colored the same way every time, regardless of the legend.