Setting different color for each series in scatter plot on matplotlib

Yotam :

Suppose I have three data sets:

X = [1,2,3,4]
Y1 = [4,8,12,16]
Y2 = [1,4,9,16]

I can scatter plot this:

from matplotlib import pyplot as plt
plt.scatter(X,Y1,color='red')
plt.scatter(X,Y2,color='blue')
plt.show()

How can I do this with 10 sets?

I searched for this and could find any reference to what I'm asking.

Edit: clarifying (hopefully) my question

If I call scatter multiple times, I can only set the same color on each scatter. Also, I know I can set a color array manually but I'm sure there is a better way to do this. My question is then, "How can I automatically scatter-plot my several data sets, each with a different color.

If that helps, I can easily assign a unique number to each data set.

DSM :

I don't know what you mean by 'manually'. You can choose a colourmap and make a colour array easily enough:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

x = np.arange(10)
ys = [i+x+(i*x)**2 for i in range(10)]

colors = cm.rainbow(np.linspace(0, 1, len(ys)))
for y, c in zip(ys, colors):
    plt.scatter(x, y, color=c)

Matplotlib graph with different colors

Or you can make your own colour cycler using itertools.cycle and specifying the colours you want to loop over, using next to get the one you want. For example, with 3 colours:

import itertools

colors = itertools.cycle(["r", "b", "g"])
for y in ys:
    plt.scatter(x, y, color=next(colors))

Matplotlib graph with only 3 colors

Come to think of it, maybe it's cleaner not to use zip with the first one neither:

colors = iter(cm.rainbow(np.linspace(0, 1, len(ys))))
for y in ys:
    plt.scatter(x, y, color=next(colors))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Setting different color for each series in scatter plot on matplotlib

Different color for each set in scatter plot on matplotlib

How to plot different shades of a color for each point in a scatter plot in matplotlib?

Specify color of each point in scatter plot (matplotlib)

matplotlib: How to plot multiple scatter plots in one grid where each plot is different color based on a separate list?

Matplotlib scatter plot with different text at each point

Highcharts Scatter plot - Add series from database as a different color

Matplotlib scatter plot with different text at each data point

pandas - scatter plot with different color legend for each point

Setting color of scatter according to the in matplotlib

color issue in scatter plot with matplotlib

Matplotlib scatter plot different colors in legend and plot

Multi color -Time series scatter plot in python

Matlab: How to set color of legend in a scatter plot where each data point gets a different color?

scatter plot matplotlib results in same color

removing outline color of scatter plot in matplotlib python

matplotlib subplot alignment with scatter plot and color bar

Matplotlib - usage of Scatter plot with specific color assigment

Is there a way to make matplotlib scatter plot marker or color according to a discrete variable in a different column?

matplotlib scatter plot with different markers and colors

Scatter matplotlib plot with different x starting points

Scatter Plot with different color for positive and negative values

How to conciliate dots annotation in Matplotlib scatter plot with manual limit setting?

Matplotlib Scatter Plot Returns Different Plots in Different Versions

Matplotlib Color gradient on scatter plot based on values from dataframe pandas

Matplotlib scatter plot color-coded by text, how to add legend?

Python, Matplotlib, Scatter plot, Change color on the clicked point

matplotlib scatter plot with color label and legend specified by c option

How to specify scatter plot point color in matplotlib pyplot?