Apply colormap when plotting array in Julia

Viewed 498

I am trying to apply a colormap when plotting an array of numbers in Julia 1.4.1, but all plot entries appear to only use the first color in the map. For instance:

using Plots

plot([1:10], rand(10,5), c=:viridis)

No colour change occurs even when I increase the number of plot entries to 1000:

plot([1:10], rand(10,1000), c=:viridis)

I have tried to apply the map using color or cgrad too, but had no success. So this leaves me wondering: how can I apply this colourmap such that it correctly spans the number of plot entries?

1 Answers

Do you want each line to use a different color? I think you are looking for the palette argument.

First,create the color palette based on viridis, as suggested by Anshul in the comments (see end for alternative method).

julia> using Plots

julia> p = Plots.palette(:viridis, 11)

In the last code, 11 is the number of lines

Now, let's plot using the palette argument.

julia> plot( [1:10], rand(10,11), palette=p )

Alternative method to create the palette is creating a color gradient function

julia> C(g::ColorGradient) = RGB[ g[z] for z in range(0,stop=1,length=11) ]

We define the palette using that function

julia> g = :viridis

julia> p = C(cgrad(g))
Related