# Adding custom color scales to plots

**URL:** https://discourse.julialang.org/t/adding-custom-color-scales-to-plots/106004
**Category:** Visualization
**Tags:** question, plotting
**Created:** [November 9, 2023, 5:31pm UTC](https://discourse.julialang.org/t/adding-custom-color-scales-to-plots/106004 "2023-11-09T17:31:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jreaso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jreaso/32/203726_2.png) [@jreaso](https://discourse.julialang.org/u/jreaso)
#### Post date: [November 9, 2023, 5:31pm UTC](https://discourse.julialang.org/t/adding-custom-color-scales-to-plots/106004/1 "2023-11-09T17:31:56Z")

</div>

I have a line plot simply constructed from a vector `x` and a vector `y`, I have then computed a `colors` vector which corresponds to each x value. I want to add a color scale of sorts below my plot (or on the plot but below the line) where the color aligns with the x values.

I came up with the solution to use a scatter plot

```julia
plot(x, y)
scatter!(x, fill(-0.5, length(x)), color=colors, label=false, marker=:rect, markersize=10)

```

On first inspection, this achieves what I want, but since the marker have significant width, the borders are off center and don’t align with the actual change in color. I tried to change the marker width but couldn’t figure out how.

What is the best way to do this?

_Edit: I found a solution I was happy with but would be interested to see if there is a nicer way to do this!_

---

<div class="post-metadata">

### Author: ![jreaso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jreaso/32/203726_2.png) [@jreaso](https://discourse.julialang.org/u/jreaso)
#### Post date: [November 10, 2023, 12:54am UTC](https://discourse.julialang.org/t/adding-custom-color-scales-to-plots/106004/2 "2023-11-10T00:54:33Z")

</div>

A solution I found acceptable is to use Shape to draw rectangles. Instead of using `scatter(...)`, I used:

```julia
for i in eachindex(x)
    shape = Shape([x[i]-rw, x[i]-rw, x[i]-rw, x[i]-rw], 
                  [0, -1, -1, 0]) 
    plot!(shape, color=colors[i], linecolor=:transparent, label=false)
end

```

where `rw` is half the rectangle width, I actually found setting `rw` to be the full distance between `x` points to be best as when they perfectly aligned, I got a strange lines artifact. Of course, the `y` values of the rectangle should be adjusted, it worked for my purposes to but the rectangles between -1 and 0 vertically.
