如何在 matplotlib.pyplot.trisurf 中指定颜色 [英] How to specify colors in matplotlib.pyplot.trisurf

查看:30
本文介绍了如何在 matplotlib.pyplot.trisurf 中指定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用trisurf绘制船体.我需要指定每个三角形的颜色(有很多).这能做到吗?我试过了,但是不起作用:

I'm trying to plot a hull using trisurf. I need to specify the color of each triangle (there are many). Can this be done? I tried this but it does not work:

import matplotlib.pyplot as plt:

...

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(points[:,0], points[:,1], points[:,2],
            triangles=faces, cmap=facecolors)
plt.show()

facecolors 是一个带有len(faces)行的矩阵;每行是(R,G,B).如果我省略 cmap 参数,它当然可以很好地但单色地绘制.

facecolorsis a matrix withlen(faces)rows; each row is (R,G,B). If I omit thecmapargument it plots fine but monochromatically, of course.

可以做我想做的事吗?

推荐答案

关于颜色你需要决定是否

Concerning the colors you will need to decide if

  1. 您想将数据 (points[:,2]) 与颜色图结合使用来为您的表面着色,或者如果
  2. 您想自己指定颜色.
  1. you want to use the data (points[:,2]) in conjunction with a colormap to colorize your surface or if
  2. you want to specify the colors yourself.

在第一种情况下,cmap 需要是一个 matplotlib 颜色图而不是一个数组.您可以使用命名的颜色图,例如 "jet",或创建自己的颜色图.

In the first case, cmap needs to be a matplotlib colormap and not an array. You can use a named colormap, like "jet", or create your own colormap.

在第二种情况下,您需要省略 cmap 关键字,而使用 facecolors 关键字参数,该参数将传递给 Poly3DCollection在后台.facecolor 参数目前被忽略.在代码您可以看到尽管 facecolor 参数被正确传递到 Poly3DCollection,facecolor 之后被 color 参数覆盖,这似乎不接受numpy数组.

In the second case, you need to omit the cmap keyword and use the facecolors keyword argument instead, which will be passed to the Poly3DCollection in the background. The facecolor argument is currently being ignored. In the code you can see that although the facecolor argument is correctly passed onto the Poly3DCollection, the facecolor is afterwards overwritten by the colorargument, which does not seem to accept a numpy array.

这篇关于如何在 matplotlib.pyplot.trisurf 中指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆