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
2 Likes

Thx! Works beautifully on a small set of triangles. Still working on a proper solution for the original use case.

@ziolai I know you are mostly interested in Plots.jl solutions, but since you mentioned the challenge with large sets of triangles, consider the following alternative:

using Meshes

import GLMakie as Mke

triangles = rand(Triangle, 100)

viz(triangles, color = rand(100))

Here is another example with 2D triangles:

using CoordRefSystems

triangles = rand(Triangle, 100, crs=Cartesian2D)

viz(triangles, color = rand(100))

You can learn more by reading the Meshes.jl documentation. The vector of random colors is converted into actual Colors.jl using the Colorfy.jl package that we also maintain.

Please reach out if you have questions.

Dear @Juliohm,

Sincere thanks for reaching out. Much appreciated. I have been following your work on Meshes.jl with keen interest, curiosity and owe.

Your recent input forces me to think harder. Please allow me some space to wiggle a bit and get back to you.

Thank you. Domenico.

1 Like

Getting closer!

Requires further looking into. More later.

1 Like

I would omit the Ngon argument. Also notice that you can pass a vector of colors with length equal to the number of vertices to get a smooth visualization. Usually, finite element solutions provide the results at the vertices themselves.

1 Like
  1. Thx for the advice on the Ngon argument.

  2. Agree that finite element solutions typically provide nodal values. Here, however, we plot the gradient of the finite element solution that we assume to be constant on each element. We therefore find it more convenient to plot per-element (as opposed to per-node) values.

  3. Question: does the function viz() provide a colorbar option?

  4. I hope to provide more transparent explanation soon.

1 Like

You can create colorbars as usual with Makie.jl. We also provide a custom cbar function that takes care of the color assignments the same way viz does:

1 Like

Remains work in progress.

More soon.

1 Like

I have just updated that chapter in the book to demonstrate the usage of the colorrange option. It might be useful if you are visualizing multiple fields with potentially different ranges of values.

Also, consider setting the aspect of the axes to Mke.DataAspect() as explained in the chapter. It is always a good idea to preserve the real size of the domain.

Correct. Requires more time to fix these issues.