0

I am working on a visualization problem where I want to plot a geometrical part using polygons specified by their node coordinates (I have basically been importing the polygon coordinates from a FEM Program). Now I am struggling with changing the facecolor of each polygon without replotting everytime (takes a lot of time). Here is what I've coded:

fig = plt.figure()
ax = axes3d.Axes3D(fig)

for i in range(0, len(e_coordinates)):
    if len(e_coordinates[i]) == 3:
        poly = p3dc([[e_coordinates[i][0], e_coordinates[i][1], e_coordinates[i][2]]],
                    facecolors=colors[i], edgecolors='black', linewidths=0.3)
    elif len(e_coordinates[i]) == 4:
        poly = p3dc([[e_coordinates[i][0], e_coordinates[i][1], e_coordinates[i][2], e_coordinates[i][3]]],
                    facecolors=colors[i], edgecolors='black', linewidths=0.3)
    ax.add_collection3d(poly)

plt.show()

The array e_coordinates specifies the polygon shape and position (either its a polygon with 3 or 4 edges) and the vector colors specifies the inital facecolors.

I am looking for something like (attention pseudo-code ;) )

polys = ax.get_collection()
polys.set_color(colors)
ax.set_collection(polys)
ax.update()

I hope I've been able to make clear what I am trying to do and that maybe someone has an helpful idea!

UPDATE: This is what I have now, but the plotting of the updated colors is not working for me...

fig = plt.figure()
ax = axes3d.Axes3D(fig)
poly = p3dc(e_coordinates, facecolor="white")
ax.add_collection3d(poly)
for j in range(0,100):
    colors = color_generater(j) # my own function to get desired colors
    ax.collections[0].set_facecolor(colors)
    plt.draw()
    plt.pause(0.01)
frfritz
  • 41
  • 1
  • 7
  • Are you creating a lot of Poly3DCollections? Why not use only a single one? That would sure be faster. But then, I'm not sure what `p3dc` is in your code, so maybe you're doing something else entirely?! In any case, updating the colors should be as easy as [`collection.set_facecolor`](https://matplotlib.org/3.1.1/api/_as_gen/mpl_toolkits.mplot3d.art3d.Poly3DCollection.html#mpl_toolkits.mplot3d.art3d.Poly3DCollection.set_facecolor). – ImportanceOfBeingErnest Dec 16 '19 at 14:47
  • You're right, I was indeed creating a lot of collections because I didn't realise one collection could have polygons with different numbers of edges. I changed it now and the color updating seems to work too. Do you have an idea about the updating part of the plot though? I don't want the figure window to close after every plot... – frfritz Dec 17 '19 at 10:47
  • As I understand, you want to update the colors. This would be done using the `set_facecolor` method as stated above. – ImportanceOfBeingErnest Dec 17 '19 at 11:20
  • I am using the `set_facecolor` method, but when I call `plt.draw()` afterwards in the loop, its not plotting new colors but rather puts the old plut into a new sets of axes... Find what I did now in my post update – frfritz Dec 17 '19 at 15:43
  • This looks like it should work. You can make this a [mcve], such that one can copy and paste to test. – ImportanceOfBeingErnest Dec 17 '19 at 16:02
  • While making the example I found the mistake! It had nothing to do with what I thought. I was using a colorbar in the plot as well (which I would have never thought could be a problem) and wanted to adjust the colorscale in each iteration according to the colors that are updated. That produces a problem, so I've changed to a fix colorscale which is defined before the loop and covers the range of all possible values. @ImportanceOfBeingErnest thank you very much for leading me on the right path, it's much appreciated! – frfritz Dec 17 '19 at 16:57
  • 2
    Always make a minimal example *before* asking a question. It saves you and everyone else a lot of time. – ImportanceOfBeingErnest Dec 17 '19 at 16:58

0 Answers0