# Can I paint the background of a plot?

**URL:** <https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807>\
**Category:** Visualization\
**Created:** [January 22, 2021, 8:40pm UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807 "2021-01-22T20:40:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 22, 2021, 8:40pm UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/1 "2021-01-22T20:40:01Z")

</div>

I would like to make a function `f(x,y)` that returns a color and give that to `plot` to create the background of the plot.

Is there something close to this on one of the plotting packages?

Thanks.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 23, 2021, 12:31am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/3 "2021-01-23T00:31:48Z")

</div>

You can just make a `heatmap` for example?  
EDIT: eg using Plots.jl

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 23, 2021, 12:42am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/4 "2021-01-23T00:42:00Z")

</div>

I want to give the plot a color to use at each x,y location, not a scalar that gets mapped to a color.

I did try `heatmap` of `Plots.jl` just in case it magically works, but it says it can’t convert `ColorTypes.RGB` to `Float64`.

Imagine I want to plot on one of these:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2183a293b854ce4953c1f5bfb69e0d74e403520.jpeg)

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 23, 2021, 1:01am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/5 "2021-01-23T01:01:41Z")

</div>

I realize this doesn’t do what you want, but maybe it’s helpful?

```julia
using ColorSchemes
using Makie

f(x,y) = RGB(x/255,y/255,x/2/255)

xs = 0:255
ys = 0:255
zs = xs .* ys'

colors = ColorScheme([f(x,y) for x in xs for y in ys])

image(xs,ys,zs, colormap=colors)

```

Outputs:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/c/3c3faf034a894ca5841d6c9305fb3ab1ac70db2c.jpeg)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 23, 2021, 2:04am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/6 "2021-01-23T02:04:08Z")

</div>

Oh that wasn’t clear.

In that case, do `using Images` and make a matrix of your RGB objects. It should display as an image with those colours.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 23, 2021, 2:19am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/7 "2021-01-23T02:19:44Z")

</div>

Oh yes, I used `Colors.jl` functions to make the chromaticity diagram image I shared. I would like to be able to plot various things on top of that image, preferably in the x,y space so that axes are sensible.

However I’m fighting with blue screens of death tonight which isn’t making anything easier.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 23, 2021, 2:30am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/8 "2021-01-23T02:30:53Z")

</div>

Would you be happy to share the code for that? It’s very nice.

Plots uses palettes. There should be a way to encode the colours into a palette so you can do the same with heatmap with axes.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 23, 2021, 3:40am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/9 "2021-01-23T03:40:28Z")

</div>

There’s some hard coded scaling that could be generalized and I dislike the C,M,Y seams that appear more prominently in this than in other implementations but here you go:.

```julia
### A Pluto.jl notebook ###
# v0.12.18

using Markdown
using InteractiveUtils

# ╔═╡ 5ba5d640-5cd0-11eb-24d0-e3dbc6ed284b
begin
	import Pkg
	Pkg.activate(mktempdir())
	Pkg.add("Colors"); using Colors
	Pkg.add("PlutoUI"); using PlutoUI
	Pkg.add("PolygonOps"); using PolygonOps
	Pkg.add("ImageShow"); using ImageShow
	Pkg.add("ImageIO"); using ImageIO
end

# ╔═╡ 982fe8a0-5cd8-11eb-3690-316355a4724a
monochromatic_locus = convert.( xyY, colormatch.(360:780))

# ╔═╡ d68fa790-5cef-11eb-062e-af9940a5a224
begin
	polygon = [[c.x,c.y] for c in monochromatic_locus]
	push!(polygon, polygon[1])
end;

# ╔═╡ c20e8ca0-5d12-11eb-114a-572c90ef9920
function chrom(x,y)
	if inpolygon((x,y), polygon)==1
		c = RGB(xyY(x,y,1e-6))
		k = 1.0/maximum((c.r, c.g, c.b))
		c = RGB(k*c.r, k*c.g, k*c.b)
	else
		c = RGB(0,0,0)
	end
end

# ╔═╡ e8b02b80-5cf3-11eb-3882-a742b74e5f5c
chromdiag = let
	chromdiag = fill(RGB(0,0,0),900, 800)
	for x ∈ 1:800
		for y ∈ 1:900
			xf = x/1000
			yf = y/1000
			chromdiag[900-y+1,x] = chrom(xf,yf)
		end
	end
	chromdiag
end

# ╔═╡ Cell order:
# ╠═e8b02b80-5cf3-11eb-3882-a742b74e5f5c
# ╠═c20e8ca0-5d12-11eb-114a-572c90ef9920
# ╠═d68fa790-5cef-11eb-062e-af9940a5a224
# ╠═982fe8a0-5cd8-11eb-3690-316355a4724a
# ╠═5ba5d640-5cd0-11eb-24d0-e3dbc6ed284b

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 23, 2021, 8:00am UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/10 "2021-01-23T08:00:15Z")

</div>

This is excellent.  
GR plots the resulting RGB array directly. Only setting the y-axis is a bit tricky because of the flipped y-axis due to the way chromdiag was constructed:

```julia
plot((1:800)/1000, (1:900)/1000, reverse(chromdiag,dims=1),xlabel="x",ylabel="y",yflip=false)

```

On this topic, the wikipedia article on [CIE 1931 color space](https://en.wikipedia.org/wiki/CIE_1931_color_space) is worth taking a look at.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 23, 2021, 2:30pm UTC](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/53807/11 "2021-01-23T14:30:38Z")

</div>

Awesome! Is the key to making that work that your x and y ranges match the image dimensions exactly?

Looks like exactly what I needed. Thanks!
