Color of the marker to be read from a dictionary

I have a dictionary (Y) and I want to plot its values using scatter(x,y).
What I want is for the colors of the markers to be either blue or red (or any other two colors) corresponding to 0 and 1 read from another dictionary with the same keys.

Let’s say as a simple example:

Y = Dict("A" => 15, "B" => 20, "C" => 45)
Z = Dict("A" => 0 , "B" => 1, "C" => 0)

This is what I have so far:

using Plots
X= 1:length(Y)
p=scatter(X, [values(Y)...];color= [values(Z)...])

but the colors don’t match the values in z dictionary.

That’s probably because the order of keys and values in a dictionary is not guaranteed. Judging by your example it’s unclear what you need the keys for, but the secret is to always iterate over a predefined vector of keys to get the associated values in the same order.

I see.
The keys are only used as the xticks of the plot.
OK I try iteration over the keys. Thanks you

1 Like