Is there any way to express changing color from Matplotlib, Python?

Question:

Before you reading, I apologize about broken English.
I have a data array of moving mass, and want to show them by time area.
Like:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 4, 5, 6])

plt.plot(x,y)

In this code, I just want to see them with different color each rows.
For example,
point (0,3) is white dot,
point (3,5) is black dot,
and (1,4) , (2,5) are gray dot but different brightness.

I just started python, so I searched pyplot lib but didn’t find examples.

I tried with Seaborn library, and Pyplot 3d examples. But didn’t find solution to express what want to do.

Asked By: Alfed

||

Answers:

Pass a color argument into a scatter function that displays given points and any defined features.

#array declaration
#...

for x1, y1 in zip(x, y):
    if x1 == 0:
        color = "white"
    elif x1 == 1 or x1 == 2:
        color = "gray"
    elif x1 == 3:
        color = "black"
        
    plt.scatter(x1, y1, c=color)

# plot linear line from arrays

We use the zip class to iterate through both arrays at once, allowing us to plot each point from the given arrays. We use the x-coordinates from the x array to determine what color to label the dot. The scatter function puts this point on the graph, giving us options to change features of the given dot(s).

The final code would look something like this:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 4, 5, 6])

for x1, y1 in zip(x, y):
    if x1 == 0:
        color = "white"
    elif x1 == 1 or x1 == 2:
        color = "gray"
    elif x1 == 3:
        color = "black"
        
    plt.scatter(x1, y1, c=color)

plt.plot(x, y)
plt.show()

Documentation on matplotlib’s scatter function can be found here

Answered By: poiboi

poiboi was on the right track. Here’s an example which automatically sets a linear gradient for the colours of the dots.

import matplotlib.pyplot as plt
import numpy as np
x = [0, 1, 2, 3]
y = [3, 4, 5, 6]
plt.scatter(x, y, c=x[:: -1], cmap='gray', vmin=min(x), vmax=max(x))
plt.show()

Example Output

The c keyword argument tells Matplotlib which colour to use for which point using a grey colourmap. By default, said colourmap goes from black to white, so we pass x reversed. vmin and vmax are the least and greatest values to be assigned colours. Note that the first point is white (hence invisible).

Answered By: tfpf

If your aim is to identify the order of your points, you can use a colormap, specifying that the vector that determines the coloring is simply the sequence of the indices of the points.

enter image description here

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20201222)
x = np.random.randint(5, 20, 20)
y = np.random.randint(5, 20, 20)

r = range(len(x))
plt.scatter(x, y, s=80, c=r, cmap='plasma')
plt.grid()
cb = plt.colorbar()
cb.set_ticks(r)
cb.set_ticklabels(("%d"%(order+1) for order in r))

and eventually the very awaited overworked implementation

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate

np.random.seed(20201222)
x = np.random.rand(20)*5+np.arange(20)/3
y = np.random.rand(20)*5+np.arange(20)/3
tck, u = interpolate.splprep([x,y] ,s=0.05)
x1, y1 = interpolate.splev(np.linspace(0, 1, 3333), tck)

r = range(len(x))
plt.plot(x1, y1, color='k', lw=0.4, alpha=0.4)
plt.scatter(x, y, s=60, c=r, cmap='Greys', ec='grey', zorder=4)
plt.xlim((0,12)), plt.ylim((0,12))
plt.grid(1)
plt.gca().set_aspect(1)
cb = plt.colorbar()
cb.set_ticks((0, len(x)-1))
cb.set_ticklabels(('First', 'Last'))
Answered By: gboffi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.