Chair
Chair

Reputation: 195

Can I assign specific values from a ColorGradient to a marker?

I apologize for this super basic question, but I am not experienced in plotting, and a lot of the documentation for Julia plotting assumes more knowledge than I have!

I am creating a scatter plot, using Plots, where each marker is plotted based on spatial position, and I want to scale the color by magnitude of value that each marker holds. I created a color gradient as such:

C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
cgrad(g,[0.01,0.99]) |> C

M is related to the number of markers, this way I create a suitable scale of colors based on the number of markers I have.

I assumed I was creating some kind of structure that would assign a color from this gradient based off a value ranging from 0.01 to 0.99. However, I guess I don't understand what the structure C is. When I assign color = C(v), where v is between 0 and 1.00, I get an error saying that C does not accept type Float64.

Is there a way I can assign a marker some color from this gradient based off its value? I have all of the values for each location stored in another array.

UPDATE: I have also tried indexing into C. I turned my values into Int64 ranging from 1-99, and tried to set color=C[v], but C also does not take Type Int64.

UPDATE 2: Ok, so i realized my issue was I did not understand the |> functionality, So i rewrote the code to look like:

C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
myGrad = (cgrad(g,[0.00,1.00]) |> C)

and now I can index into my color gradiant! However I still am having an issue setting the color equal to the value stored in the myGradient array.

for i = 1:M
    X,Y = find_coords(i,pd)
    colors = myGrad[c_index[i]]
    outline = rand(Float64,3)
    plt = plot!(X,Y,colors, markerstrokecolor = outline)
end

When I type myGrad[c_index[i]] into REPL it plots a color. However I am getting an error from the above code which states

"Cannot convert RGB{Float64} to series data for plotting"

If i change the plot line as follows I get a slightly different error:

plt = plot!(X,Y,markercolor = colors, markerstrokecolor = outline)

ERROR: LoadError: MethodError: no method matching plot_color(::Float64)

So for some reason I cant store this color, as a color variable for my plot.

Upvotes: 2

Views: 1677

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

There are a few different issues at play here. Firstly, if you want to create a scatter plot, you should probably use scatter. It also doesn't seem necessary to plot things in a loop here, although it's hard to tell as your code isn't a minimum working example (MWE), as it relies on things defined somewhere else in your code.

Here's an example of how this might work:

using Plots

# Create a discrete color gradient with 20 points
my_colors = [cgrad(:inferno, [0.01, 0.99])[z] for z ∈ range(0.0, 1.0, length = 20)]

# Draw some random data points
x, y = sort(rand(100)), rand(100)
# Assign a color between 1 and 20 on the color grid to each point
z = sort(rand(1:20, 100))

# Plot
scatter(x, y, color = my_colors[z], markerstrokecolor = "white", label = "", 
            markersize = [10 for _ ∈ 1:100])

gives:

enter image description here

Upvotes: 2

Related Questions