Plot Shape by Value

Greetings.

I am seeking to extend

using Plots 

p1 = [(0., 0.), (1., 0.), (2., 0.)];
p2 = [(1., 0.), (2., 0.), (3., 0.)];
p3 = [(1., 1.), (2., 1.), (3., 1.)];

p = Plots.plot(xlabel = "x", ylabel = "y",legend = false,aspect_ratio = :equal)
for i in 1:3  
    triangle = Shape([p1[i], p2[i], p3[i]])
    p = Plots.plot!(p,triangle,fillcolor=:blue, alpha=0.7, linecolor=:black)
end 

plot(p)

resulting in

to a version in which the color per triangle is specified by a vector with 3 Float64 numbers.

Thx.

Please specify if you need Plots.jl or if any plotting library works for you.

1 Like

Given that this is intended master course material, Plots.jl would be neat, as it used in previous exercises. I am, however, open to alternatives. Many thx.

function plotTriangles()
	p1 = [(0., 0.), (1., 0.), (2., 0.)];
	p2 = [(1., 0.), (2., 0.), (3., 0.)];
	p3 = [(1., 1.), (2., 1.), (3., 1.)];

	color_vectors = [[0.5,0.2,0.1], [0.2,0.7,0.3], [0.1,0.3,0.5]]

	p = Plots.plot(xlabel = "x", ylabel = "y",legend = false,aspect_ratio = :equal)
	for i in 1:3  
			triangle = Shape([p1[i], p2[i], p3[i]])
			p = Plots.plot!(p,triangle,fillcolor=RGB(color_vectors[i]...), alpha=0.7, linecolor=:black)
	end 

	plot(p)
end

plotTriangles()

Thx! Much appreciated.

Any suggestion for how to create color_vectors from Float64 input data using e.g. applycolormap or RGB?

I have no idea of what you are asking or what you are trying to accomplish. Maybe you can find the functionality you want here Colormaps and Colorscales · Colors. If so, then you can probably skip the step in my code above (which is what I thought you originally asked about) and see seriescolor at Series Attributes · Plots.

Sincere apologies for being ambiguous.

I am looking to post-process the results of two-dimensional finite element computations on a triangular grid. Having solved Laplacian(u) = f for u using linear finite elements, Gradient(u) is constant on each element. I am thus looking to plot Gradient(u) on each element.

I will look into the pointers you provided.

Thank you.

Untested:

  • Create a colormap with N colors Colormaps and Colorscales · Colors, pass it as color_palette to plot.
  • Determine extrema of your gradients.
  • Map gradient values to colormap indices 1:N something like grad -> round(Int, (grad-gradmin)/(gradmax-gradmin) * (N-1) + 1)
  • Get colormap index for each triangle’s gradient, pass as fillcolor or seriescolor to plot
1 Like