# Plot Shape by Value

**URL:** <https://discourse.julialang.org/t/plot-shape-by-value/131183>\
**Category:** Visualization\
**Tags:** question, plots\
**Created:** [July 29, 2025, 7:35pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183 "2025-07-29T19:35:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [July 29, 2025, 7:35pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/1 "2025-07-29T19:35:05Z")

</div>

Greetings.

I am seeking to extend

```julia-auto
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

 ![Screenshot 2025-07-29 at 21.31.48](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5f98f02f3bf3807cb17a6615718b7a397857c4a.png)

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

Thx.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 29, 2025, 7:57pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/2 "2025-07-29T19:57:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [July 29, 2025, 8:03pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/3 "2025-07-29T20:03:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 29, 2025, 9:11pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/4 "2025-07-29T21:11:12Z")

</div>

```julia-auto
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()

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/31f8d91128b87a34680846c7fa860b73b18e71e7.png)

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [July 30, 2025, 6:52am UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/5 "2025-07-30T06:52:43Z")

</div>

Thx! Much appreciated.

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

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 30, 2025, 3:01pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/6 "2025-07-30T15:01:54Z")

</div>

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](https://juliagraphics.github.io/Colors.jl/dev/colormapsandcolorscales/). 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](https://docs.juliaplots.org/latest/generated/attributes_series/).

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [July 30, 2025, 3:37pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/7 "2025-07-30T15:37:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 30, 2025, 4:00pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/8 "2025-07-30T16:00:39Z")

</div>

Untested:

- Create a colormap with N colors [Colormaps and Colorscales · Colors](https://juliagraphics.github.io/Colors.jl/dev/colormapsandcolorscales/#Predefined-sequential-and-diverging-colormaps), 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`

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 5, 2025, 8:00am UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/9 "2025-08-05T08:00:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 5, 2025, 10:49am UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/10 "2025-08-05T10:49:46Z")

</div>

@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:

```julia
using Meshes

import GLMakie as Mke

triangles = rand(Triangle, 100)

viz(triangles, color = rand(100))

```

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

Here is another example with 2D triangles:

```julia
using CoordRefSystems

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

viz(triangles, color = rand(100))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4fa922fa4cdec58d3cf5532dfa833cc60871d128.png)

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.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 5, 2025, 11:40am UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/11 "2025-08-05T11:40:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 5, 2025, 6:53pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/12 "2025-08-05T18:53:01Z")

</div>

Getting closer!

 ![Screenshot 2025-08-05 at 20.44.32](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4756f863b437f2e6ab38e99791990d5c82731de.png)

Requires further looking into. More later.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 5, 2025, 10:16pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/13 "2025-08-05T22:16:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 6, 2025, 1:36pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/14 "2025-08-06T13:36:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 6, 2025, 2:34pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/15 "2025-08-06T14:34:22Z")

</div>

> [@ziolai](#):
>
> - Question: does the function `viz()` provide a colorbar option?

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:

> **[2  Scientific visualization – Geospatial Data Science with Julia](https://juliaearth.github.io/geospatial-data-science-with-julia/02-geoviz.html)**

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 6, 2025, 5:29pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/16 "2025-08-06T17:29:59Z")

</div>

Remains work in progress.

 ![Screenshot 2025-08-06 at 19.23.40](https://global.discourse-cdn.com/julialang/original/3X/7/a/7aec23722c340280f756667043358449105d038f.png)

More soon.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 6, 2025, 5:37pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/17 "2025-08-06T17:37:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [August 6, 2025, 6:02pm UTC](https://discourse.julialang.org/t/plot-shape-by-value/131183/18 "2025-08-06T18:02:08Z")

</div>

Correct. Requires more time to fix these issues.
