简体   繁体   中英

How to plot points in different color?

I want to plot a scatter plot in python so that every point (x,y) gets different shade of the same color. I assume that data points are ordered in both data0x and data0y (according to how they are picked by my algorithm) so I want those from the beginning to be darker than the ones from the end.

ax.scatter(data0x, data0y,c=, marker=markers[0])

How can this be done?

You want to use a color map (gallery) . You need to assign (by what ever scheme you want) a float in [0, 1] to each point, say

c = arange(len(data0x)) / len(data0x)

then

ax.scatter(data0x, data0, c=c, cmap='blues',...)

There are a few options. You can use the colormaps (as tcaswell suggests), or you can also specify the color directly.

I generally find that when I want to use specific colors, it's easier to specify those directly rather than think of the color and then figure out how to get it from the colormap. In case that's what you want to do, here's an example for specifying the color directly in a scatter plot.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(50)
y = np.random.rand(50)

c = np.array([1, 1, 0])  # base color, set for each point (here, yellow)
z = x/max(x)   # the darkness for each point (here, scale along x-axis)

plt.scatter(x,y, s=300, c=c[np.newaxis,:]*z[:, np.newaxis], marker=">")
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM