Reputation: 331
I don't understand why pyplot print always red color, but variable changing
def showDot(dot):
classColormap = ListedColormap(['#FF0000', '#00FF00', '#000000'])
pl.scatter(dot[0][0],dot[0][1],c=dot[1],cmap=classColormap)
pl.show()
Also when i write c = 2(constant color, but not red) pyplot print red
Upvotes: 1
Views: 1697
Reputation: 2889
It is not entirely clear to me what you want the end result to look like, but it seems to me that c
has to be some number specifying the intensity. To me, it looks like c=dot[1]
would return an array in your code. The following code does produce different colors. Here I have simply defined the intensity acording to the distance from origo. You probably have something else.
I have never used colormaps before but my guess is that it needs to operate on a range of values in order to assign different colors. If you add one and one dot, my guess is that it always assigns the same color from the colormap (the mid value perhaps?)
from matplotlib import pyplot as pl
from matplotlib import colors as cl
import numpy as np
mean = (1, 2)
cov = [[1, 0.2], [.2, 1]]
dots = np.random.multivariate_normal(mean, cov, (2, 20))
# The distance from origo, which will define the intensity
dots_intensity = np.hypot(dots[0,:], dots[1,:])
classColormap = cl.ListedColormap(['#FF0000', '#00FF00', '#000000'])
pl.scatter(dots[0,:], dots[1,:],c=dots_intensity,cmap=classColormap)
hope it helps.
I found this article useful https://medium.com/better-programming/how-to-use-colormaps-with-matplotlib-to-create-colorful-plots-in-python-969b5a892f0c
Upvotes: 1