# SCATTERPLOT new color to each marker

**URL:** <https://discourse.julialang.org/t/scatterplot-new-color-to-each-marker/86905>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 7, 2022, 6:27pm UTC](https://discourse.julialang.org/t/scatterplot-new-color-to-each-marker/86905 "2022-09-07T18:27:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![naturalmechanics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naturalmechanics/32/20561_2.png) [@naturalmechanics](https://discourse.julialang.org/u/naturalmechanics)\
**Post date:** [September 7, 2022, 6:27pm UTC](https://discourse.julialang.org/t/scatterplot-new-color-to-each-marker/86905/1 "2022-09-07T18:27:22Z")

</div>

Hi

I want to make a scatterplot, where each marker has an unique color.

Consider this code:

```julia
xVals = []
yVals = []
cellVals = []
cols = []

my_function() # this function will fill up xVals, yVals, and cellVals

mx = max(cellVals ...)
mn = min(cellVals ...)
df = mx - mn

P = plot()

for cNum in size(xVals)[1] : -1 :1
	
	v = cellVals[cNum]
	dv = v - mn
	dv = dv * 1 / df
	rd = dv
	bl = 1 - dv 
	push!(cols, RGB(rd,0,bl))
	
end
plot!(P, xVals, yVals, seriestype = :scatter, aspect_ratio = 1, c=cols)
display(P)

```

This fails with error message :

> ERROR: MethodError: no method matching plot\_color(::Tuple{Float64, Int64, Float64})  
> Closest candidates are:  
> plot\_color(::Any, ::Nothing) at ~/.julia/packages/PlotUtils/NE7U1/src/colors.jl:20  
> plot\_color(::Any, ::Number) at ~/.julia/packages/PlotUtils/NE7U1/src/colors.jl:23  
> plot\_color(::Colorant) at ~/.julia/packages/PlotUtils/NE7U1/src/colors.jl:15  
> …  
> Stacktrace:  
> [1] get\_series\_color(c::Tuple{Float64, Int64, Float64}, sp::Plots.Subplot{Plots.GRBackend}, n::Int64, seriestype::Symbol)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/args.jl:2081  
> [2] (::Plots.var"#116#117"{Plots.Subplot{Plots.GRBackend}, Int64, Symbol})(x::Tuple{Float64, Int64, Float64})  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/args.jl:2085  
> [3] iterate  
> @ ./generator.jl:47 [inlined]  
> [4] \_collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, Plots.var"#116#117"{Plots.Subplot{Plots.GRBackend}, Int64, Symbol}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})  
> @ Base ./array.jl:744  
> [5] collect\_similar  
> @ ./array.jl:653 [inlined]  
> [6] map  
> @ ./abstractarray.jl:2867 [inlined]  
> [7] get\_series\_color(c::Vector{Any}, sp::Plots.Subplot{Plots.GRBackend}, n::Int64, seriestype::Symbol)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/args.jl:2084  
> [8] \_update\_series\_attributes!(plotattributes::RecipesPipeline.DefaultsDict, plt::Plots.Plot{Plots.GRBackend}, sp::Plots.Subplot{Plots.GRBackend})  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/args.jl:2166  
> [9] add\_series!(plt::Plots.Plot{Plots.GRBackend}, plotattributes::RecipesPipeline.DefaultsDict)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/pipeline.jl:364  
> [10] \_process\_seriesrecipe(plt::Any, plotattributes::Any)  
> @ RecipesPipeline ~/.julia/packages/RecipesPipeline/OXGmH/src/series\_recipe.jl:46  
> [11] \_process\_seriesrecipes!(plt::Any, kw\_list::Any)  
> @ RecipesPipeline ~/.julia/packages/RecipesPipeline/OXGmH/src/series\_recipe.jl:27  
> [12] recipe\_pipeline!(plt::Any, plotattributes::Any, args::Any)  
> @ RecipesPipeline ~/.julia/packages/RecipesPipeline/OXGmH/src/RecipesPipeline.jl:97  
> [13] \_plot!(plt::Plots.Plot, plotattributes::Any, args::Any)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/plot.jl:216  
> [14] #plot!#160  
> @ ~/.julia/packages/Plots/yJrrq/src/plot.jl:206 [inlined]  
> [15] top-level scope  
> @ REPL[8]:1

If i try to use heatmap, like this :

```julia
using ColorSchemes
heatmap(xVals, yVals, cellVals, c = :thermal)

```

also fails with :

> Error showing value of type Plots.Plot{Plots.GRBackend}:  
> ERROR: MethodError: no method matching heatmap\_edges(::Vector{Float64}, ::Symbol, ::Vector{Float64}, ::Symbol, ::Tuple{Int64}, ::Bool)  
> Closest candidates are:  
> heatmap\_edges(::AbstractVector, ::Symbol, ::AbstractVector, ::Symbol, ::Tuple{Int64, Int64}, ::Bool) at ~/.julia/packages/Plots/yJrrq/src/utils.jl:288  
> heatmap\_edges(::AbstractVector, ::Symbol, ::AbstractVector, ::Symbol, ::Tuple{Int64, Int64}) at ~/.julia/packages/Plots/yJrrq/src/utils.jl:288  
> heatmap\_edges(::AbstractVector, ::Symbol) at ~/.julia/packages/Plots/yJrrq/src/utils.jl:278  
> …  
> Stacktrace:  
> [1] gr\_add\_series(sp::Plots.Subplot{Plots.GRBackend}, series::Plots.Series)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/backends/gr.jl:1897  
> [2] gr\_display(sp::Plots.Subplot{Plots.GRBackend}, w::Measures.AbsoluteLength, h::Measures.AbsoluteLength, viewport\_canvas::Vector{Float64})  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/backends/gr.jl:1034  
> [3] gr\_display(plt::Plots.Plot{Plots.GRBackend}, fmt::String)  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/backends/gr.jl:699  
> [4] gr\_display  
> @ ~/.julia/packages/Plots/yJrrq/src/backends/gr.jl:665 [inlined]  
> [5] \_display(plt::Plots.Plot{Plots.GRBackend})  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/backends/gr.jl:2240  
> [6] display(#unused#::Plots.PlotsDisplay, plt::Plots.Plot{Plots.GRBackend})  
> @ Plots ~/.julia/packages/Plots/yJrrq/src/output.jl:164

The content of `xVals`, `yVals` and `cellVals` are all real numbers.

> julia\> xVals  
> 6846-element Vector{Any}:  
> 0.0195  
> 0.0195  
> 0.019  
> 0.0185  
> 0.0185  
> 0.019  
> 0.019  
> 0.019  
> ⋮  
> -8.673617379884035e-19  
> 0.0004999999999999988  
> 0.0005  
> 0.0005  
> 1.0842021724855044e-19  
> -0.0005  
> -0.0005000000000000002  
> -4.336808689942018e-19

> julia\> yVals  
> 6846-element Vector{Any}:  
> 0.0002886751345948129  
> -0.0002886751345948132  
> -0.0005773502691896261  
> -0.00028867513459481295  
> 0.000288675134594813  
> 0.0005773502691896261  
> -0.0005773502691896257  
> -0.0011547005383792518  
> ⋮  
> 0.0011547005383792516  
> 0.0014433756729740647  
> 0.0002886751345948129  
> -0.0002886751345948128  
> -0.0005773502691896258  
> -0.00028867513459481295  
> 0.0002886751345948127  
> 0.0005773502691896258

> julia\> cellVals  
> 6846-element Vector{Any}:  
> 89  
> 78  
> 37  
> 19  
> 89  
> 128  
> 37  
> 58  
> ⋮  
> 19  
> 89  
> 34  
> 60  
> 200  
> 50  
> 19  
> 128

So its not that I am trying to make a heatmap out of imaginary numbers or complex ones.

The only thing that seems to work is this :

```julia
P = plot()

for cNum in size(xVals)[1] : -1 :1
	
	v = cellVals[cNum]
	dv = v - mn
	dv = dv * 1 / df
	rd = dv
	bl = 1 - dv 
	push!(cols, (rd,0,bl))
	plot!(P, [xVals[cNum]], [yVals[cNum]], seriestype = :scatter, aspect_ratio = 1, c=RGB(rd,0,bl))
end

display(P)

```

Ya, I have counted down the array, for no reason.

The result is :

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/7812b0cab907bd59138226b2bce80726ba5e7aca.png)

So,basically, i plotted every single marker, where i take its x-Coordinate as a vector with one element, and same with the y-Coordinate. It thus plots as many y-series as there are markers in the plot.

This is not nice.

Is there a better way **to achieve an unique color, to each unique marker of a scatterplot, based on the value of an element of an array, where each element is associated to the markers in a 1-to-1 basis**?

Also, how can I move the list of y-series more to the right or turn it off completely, replacing it with a colorbar? Thank you.

Output of `versioninfo()`

```julia
Julia Version 1.7.3
Commit 742b9abb4d* (2022-05-06 12:58 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, westmere)

```

Thank you.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 8, 2022, 6:59am UTC](https://discourse.julialang.org/t/scatterplot-new-color-to-each-marker/86905/2 "2022-09-08T06:59:33Z")

</div>

I think you are looking for `marker_z`:

```julia
using Plots

scatter([1,4,5], [7,4,8], marker_z = [1,2,3])

```
