How to configure a colormap using PyPlot

I am trying to set the default colormap using PyPlot, but the colors are still the default colors.

Here’s a MWE

using PyPlot

PyPlot.matplotlib.rc("image",cmap="gray")

fig, ax = subplots()

x  = 1:0.1:10
y = exp.(x/π)

ax.plot(x/π,y, "-o")

and the plot is
example_pyplot

What am I doing wrong?

Not sure this is what you want, but you can do this:

cm = get_cmap(:gray)

fig, ax = subplots()

x  = 1:0.1:10
y = exp.(x/π)

ax.plot(x/π,y, "-o",color=cm(0.0))
ax.plot(x/π,2y, "-o",color=cm(0.4))
ax.plot(x/π,3y, "-o",color=cm(0.9))

The values to get the color need to be either floats between 0.0 and 1.0 or Int between 0 and 255.

1 Like

Actually, my intent is to use by default 20 different colors to plot 20 different data sets.
I thought of using tab20 color map (see here).

I know that I can simply do this

cm = get_cmap(:tab20)

fig, ax = subplots()

x  = 1:0.1:10
y = exp.(x/π)
colorrange = (0:19) ./ 20
for i=1:20
    ax.plot(x/π,i*y, "-o",color=cm(colorrange[i]))
end

and get
pyplot_colormap_example

But I was wondering if there is a way to simply set the default color set, and let all the colors be chosen by order from this set.

I see. I found a way to do this from this page of the manual I followed this solution:


using PyPlot

using PyCall

cm=get_cmap(:tab20);

cycler = pyimport("cycler")
PyPlot.rc("axes",prop_cycle=cycler.cycler(color=[cm(t/19) for t in 0:19]))


fig, ax = subplots()

x  = 1:0.1:10
y = exp.(x/π)

for i=1:20
    ax.plot(x/π,i*y, "-o")
end

which gives the plot you have shown above! Probably there’s a nicer way to extract the tab20 cycler!?

3 Likes