I have the following problem, I want to create my own colormap (red-mix-violet-mix-blue) that maps to values between -2 and +2 and want to use it to color points in my plot.
The plot should then have the colorscale to the right.
That is how I create the map so far. But I am not really sure if it mixes the colors.
cmap = matplotlib.colors.ListedColormap(["red","violet","blue"], name='from_list', N=None)
m = cm.ScalarMappable(norm=norm, cmap=cmap)
That way I map the colors to the values.
colors = itertools.cycle([m.to_rgba(1.22), ..])
Then I plot it:
for i in range(0, len(array_dg)):
plt.plot(array_dg[i], markers.next(),alpha=alpha[i], c=colors.next())
My problems are:
1. I can't plot the color scale.
2. I am not completely sure if my scale is creating a continues (smooth) colorscale.
Best Answer
There is an illustrative example of how to create custom colormaps here. The docstring is essential for understanding the meaning of
cdict
. Once you get that under your belt, you might use acdict
like this:Although the
cdict
format gives you a lot of flexibility, I find for simple gradients its format is rather unintuitive. Here is a utility function to help generate simple LinearSegmentedColormaps:By the way, the
for-loop
plots one point for every call to
plt.plot
. This will work for a small number of points, but will become extremely slow for many points.plt.plot
can only draw in one color, butplt.scatter
can assign a different color to each dot. Thus,plt.scatter
is the way to go.