-1

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.

Alfed
  • 3
  • 2
  • 3
    Does this answer your question? [matplotlib scatter plot with different markers and colors](https://stackoverflow.com/questions/26490817/matplotlib-scatter-plot-with-different-markers-and-colors) – Derek O Dec 22 '22 at 04:43

3 Answers3

0

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

poiboi
  • 84
  • 9
  • I believe there is a more efficient method of setting each point's colors. I'm not too sure on how it can be implemented. – poiboi Dec 22 '22 at 06:06
  • Thank you for answering! But I still wondering to coloring datas by orders (row / column number) not by values. I'll try to find another way. – Alfed Dec 22 '22 at 07:01
0

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

tfpf
  • 612
  • 1
  • 9
  • 19
0

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'))
gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Maybe this one is I'm looking for. I'm not sure about cmap variable(function?), so gonna try this one. – Alfed Dec 23 '22 at 04:23