# How to set face color per triangle when using plot\_trisurf in PyPlot

**URL:** <https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [July 5, 2017, 12:32pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662 "2017-07-05T12:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fruno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fruno/32/5013_2.png) [@fruno](https://discourse.julialang.org/u/fruno)\
**Post date:** [July 5, 2017, 12:32pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/1 "2017-07-05T12:32:26Z")

</div>

Hi, I want to set the face color of each triangle individually when plotting with `plot_trisurf` in PyPlot. The matplotlib manual seems to indicate that face color is set with the option ‘color’ (as a tuple), but there is also an optional argument ‘facecolor’ that is passed to Poly3DCollection. However, I cannot figure out how to apply this so that each triangle (patch) gets a separately defined color. I do manage to set the edge colors separately using the argument ‘edgecolor’.

This produces the result when all triangles (only two here) get the same color:

```julia
using PyPlot
fig = figure()
ax = Axes3D(fig)
x = [0., 1., 1., 0.]
y = [0., 0.5, 1., 1.]
z = [0., 0., 1., 0.2]
t = [1 2 4; 2 3 4]-1 # Zero-based indexing required (?)
fclr = [1. 0. 0.; 0. 0. 1.]
c = (0.,1.,0.)
plot_trisurf(x,y,z,triangles=t,color=c,edgecolor=fclr,alpha=0.8, linewidth=3.);

```

I have not managed to get any effect by setting the ‘facecolor’ argument and I don’t know the syntax for passing multiple colors to ‘color’.

Any pointers to where I can find documentation about this, i.e., plotting surfaces with individually defined triangles, without resorting to a loop “over the triangles”?

Tested with v. 0.5.2 and 0.6.0.

---

<div class="post-metadata">

**Author:** ![krcools](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krcools/32/10212_2.png) [@krcools](https://discourse.julialang.org/u/krcools)\
**Post date:** [July 5, 2017, 3:31pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/2 "2017-07-05T15:31:05Z")

</div>

I tried to do the same thing. At the time browsing through the matplotlib source lead me to a line saying ‘todo: support facecolors’. Gave up at that point. Not sure what the status is now.

I succeeded in making a coloured mesh plot using PlotlyJs though.

---

<div class="post-metadata">

**Author:** ![fruno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fruno/32/5013_2.png) [@fruno](https://discourse.julialang.org/u/fruno)\
**Post date:** [July 7, 2017, 4:14pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/3 "2017-07-07T16:14:52Z")

</div>

Thanks,

using PlotlyJS.jl I managed to set the face colors individually! But with this solution I don’t know how to set the edge colors… 🙂

The code that gives me (sort of) what I want:

```julia
using PlotlyJS
function triplot(vertices,faces,facecolor;opacity=0.5,cameraEye=[-1 -1 1])
    fcStr = [@sprintf("rgb(%5.2f,%5.2f,%5.2f)",255*facecolor[k,1],255*facecolor[k,2],255*facecolor[k,3]) for k = 1:size(facecolor,1) ]
    t = mesh3d(
        x = vertices[:,1], y = vertices[:,2], z = vertices[:,3],
        i = faces[:,1]-1, j = faces[:,2]-1, i = faces[:,3]-1, # Zero-based indexing in plotly
        facecolor=fcStr,opacity=opacity)
        layout = Layout(;title="Basic Triangle Plot",
                        scene=attr(;camera=attr(eye=attr(x=cameraEye[1],y=cameraEye[2],z=cameraEye[3]))))
    plot(t, layout)
end

verts = [0. 0. 0.; 1. 0.5 0.; 1. 1. 1.; 0. 1. 0.2]
faces = [1 2 4; 2 3 4]
fclr = [1. 0. 0.; 0. 0. 1.]
triplot(verts,faces,fclr)

```

A step in the right direction.

---

<div class="post-metadata">

**Author:** ![fruno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fruno/32/5013_2.png) [@fruno](https://discourse.julialang.org/u/fruno)\
**Post date:** [July 8, 2017, 11:06pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/4 "2017-07-08T23:06:02Z")

</div>

On further inspection, this is a non-working MWE. The triangle indexing appears to have no impact (incorrect syntax?) and the triangles are not colored in the given order. Any ideas would be appreciated. Another graphics package?

---

<div class="post-metadata">

**Author:** ![krcools](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krcools/32/10212_2.png) [@krcools](https://discourse.julialang.org/u/krcools)\
**Post date:** [July 9, 2017, 11:59pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/5 "2017-07-09T23:59:06Z")

</div>

This is how I use PlotlyJs. As far as i know it works as expected: [https://github.com/krcools/CompScienceMeshes.jl/blob/master/examples/plotlyjs\_patches.jl](https://github.com/krcools/CompScienceMeshes.jl/blob/master/examples/plotlyjs_patches.jl)

---

<div class="post-metadata">

**Author:** ![fruno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fruno/32/5013_2.png) [@fruno](https://discourse.julialang.org/u/fruno)\
**Post date:** [July 30, 2017, 2:29pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/6 "2017-07-30T14:29:42Z")

</div>

Thanks, it does work as expected now! I had a typo in the code above (not assigning any values to ‘k’). Vacation can be productive… 🙂

---

<div class="post-metadata">

**Author:** ![Ben\_Schubert](https://avatars.discourse-cdn.com/v4/letter/b/f0a364/32.png) [@Ben\_Schubert](https://discourse.julialang.org/u/Ben_Schubert)\
**Post date:** [February 7, 2018, 5:25am UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/7 "2018-02-07T05:25:28Z")

</div>

So I’m really new to Python (starting playing around today!) but I think I found a fix. Just put

```julia
        if 'facecolors' in kwargs:
            fcolors = kwargs.pop('facecolors')
            polyc.sets_facecolors(fcolors)

```

at line 2032 and it should work if you use facecolor as an input argument to plot\_trisurf. Just make sure you format the input colour array correctly and you’ll be dandy!

I know this is editing the library functions and that can be dubious, but if it works…

---

<div class="post-metadata">

**Author:** ![loicland](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@loicland](https://discourse.julialang.org/u/loicland)\
**Post date:** [April 3, 2018, 2:47pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/8 "2018-04-03T14:47:42Z")

</div>

You can do it using `set_array`, see [this stack thread](https://stackoverflow.com/questions/24218543/colouring-the-surface-of-a-sphere-with-a-set-of-scalar-values-in-matplotlib/24229480#comment37425657_24229480)

---

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [December 29, 2020, 9:52pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/9 "2020-12-29T21:52:34Z")

</div>

For FEM visualization, I really need the functionality of Matlab’s trisurf()… specifically where I specify a value on all the nodes, and I get a “smoothed” color plot on the triangles.

I can’t get PyPlot set\_array() to work (I really don’t understand Python Numpy well enough to figure out the more arcane documentation.)

I’m able to import a triangle mesh from GMSH, and get it to plot, but everytime I feed any array to P.set\_array() I get pages of Python errors, or a single color. Any help would be greatly appreciated.

Thanks. GMSH triangle file hole.m available here:  
[https://ellipticalcow.com/hole.m![pyplot\_plot\_trisurf|660x500]](https://ellipticalcow.com/hole.m!%5Bpyplot_plot_trisurf%7C660x500%5D)

The attached plot shows what I’m looking for (that plot was made in Paraview).

```julia
## gmsh matlab format (NICE!) just need to replace % with # and get rid of . 
include("hole.m") ## an xy triangularization, z = zero 

T = Tgmsh[:,1:3]
X = vec(nodes[:,1])
Y = vec(nodes[:,2])
Tpy = T .- 1 ## Numpy apparently uses zero based indexing... 
P = PyPlot.plot_trisurf(X,Y,zeros(length(X)),triangles=Tpy)

C = vec(rand(length(X))) ## to be replaced with FEM data 

P.set_array(C) ## ?? help appreciated. thanks

```

Also, I’d love to able to do this in Makie too, but that seems to be an open issue.

> <https://github.com/MakieOrg/Makie.jl/issues/511>
>
> Trisurf plots accept Vectors of x, y, and z, triangulate them into a mesh, and t…hen plot them. PyPlot has an easy to use module for this, as does Matlab. We can look into implementing this using a Julia-based tessellation package such as VoronoiDelaunay.jl, or using EarCut.jl for triangulation (though that may be more difficult, depending on how we specify vertices).

 ![pyplot_plot_trisurf](https://global.discourse-cdn.com/julialang/original/3X/4/6/4626933d3126a70a96716465c571d2bc6d4099e5.jpeg)

---

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [December 29, 2020, 9:56pm UTC](https://discourse.julialang.org/t/how-to-set-face-color-per-triangle-when-using-plot-trisurf-in-pyplot/4662/10 "2020-12-29T21:56:31Z")

</div>

my link to the mesh file got corrupted. here it is

[https://ellipticalcow.com/hole.m](https://ellipticalcow.com/hole.m)
