tripcolor使用每个顶点的RGB值

时间:2017-01-11 16:51:39

标签: python matplotlib plot

我有一个带有n个顶点的2D三角网格,存储在变量public class ReturnResultActivityImpl implements ReturnResultActivity { SettableFuture future; public ReturnResultActivityImpl() { } public ReturnResultActivityImpl(SettableFuture future) { this.future = future; } public void returnResult(WorkflowResult workflowResult) { System.out.print("Marking future as Completed"); future.set(workflowResult); } } public class WorkflowResult { public WorkflowResult(boolean s, String n) { this.success = s; this.note = n; } private boolean success; private String note; } public class WorkflowStarter { @Autowired ReturnResultActivityClient returnResultActivityClient; @Autowired DummyWorkflowClientExternalFactory dummyWorkflowClientExternalFactory; @Autowired AmazonSimpleWorkflowClient swfClient; String domain = "test-domain; boolean isRegister = true; int days = 7; int terminationTimeoutSeconds = 5000; int threadPollCount = 2; int taskExecutorThreadCount = 4; public String testWorkflow() throws Exception { SettableFuture<WorkflowResult> workflowResultFuture = SettableFuture.create(); String taskListName = "testTaskList-" + RandomStringUtils.randomAlphabetic(8); ReturnResultActivity activity = new ReturnResultActivityImpl(workflowResultFuture); SpringActivityWorker activityWorker = buildReturnResultActivityWorker(taskListName, Arrays.asList(activity)); DummyWorkflowClientExternalFactory factory = new DummyWorkflowClientExternalFactoryImpl(swfClient, domain); factory.getClient().doSomething(taskListName) WorkflowResult result = workflowResultSettableFuture.get(20, TimeUnit.SECONDS); return "Call result note - " + result.getNote(); } public SpringActivityWorker buildReturnResultActivityWorker(String taskListName, List activityImplementations) throws Exception { return setupActivityWorker(swfClient, domain, taskListName, isRegister, days, activityImplementations, terminationTimeoutSeconds, threadPollCount, taskExecutorThreadCount); } } public class Workflow { @Autowired private DummyActivityClient dummyActivityClient; @Autowired private ReturnResultActivityClient returnResultActivityClient; @Override public void doSomething(final String resultActivityTaskListName) { Promise<Void> activityPromise = dummyActivityClient.dummyActivity(); returnResult(resultActivityTaskListName, activityPromise); } @Asynchronous private void returnResult(final String taskListname, Promise waitFor) { ActivitySchedulingOptions schedulingOptions = new ActivitySchedulingOptions(); schedulingOptions.setTaskList(taskListname); WorkflowResult result = new WorkflowResult(true,"All successful"); returnResultActivityClient.returnResult(result, schedulingOptions); } } (matplotlib.tri.Triangulation对象)中;我可以很容易地用matplotlib的tripcolor函数绘制网格,一切正常。但是,对于每个顶点(tri),我也有(r,g,b)三元组,并且这些值不会沿着单个维度下降,因此无法轻松转换为颜色映射(例如,想象一下,如果你在一张公园的大照片上覆盖一个三角形网格,那么为每个顶点分配它下面的像素的颜色。)

我以为我可以做这样的事情:

vcolors
  

ValueError:集合只能映射排名1数组

有没有一种方便的方法将matplotlib.pyplot.tripcolor(tri, vcolors) - 像(n x 3)矩阵转换成tripcolor可用的东西?是否有替代tripcolor的替代顶点颜色?

我尝试过的一件事就是制作自己的色彩图:

vcolors

然而这没有做任何事情---没有出现数字而且没有出现错误;该函数返回一个数字句柄,但没有任何东西被渲染(我正在使用IPython笔记本)。请注意,如果我调用以下内容,则情节显示正常:

z = numpy.asarray(range(len(vcolors)), dtype=np.float) / (len(vcolors) - 1)
cmap = matplotlib.colors.Colormap(vcolors, N=len(vcolors))
matplotlib.pyplot.tripcolor(tri, z, cmap=cmap)
matplotlib.pyplot.show()

我正在使用Python 2.7。

3 个答案:

答案 0 :(得分:1)

matplotlib的{​​{1}}和tripcolor代码中生根后,我提出了以下解决方案,只要使用'gouraud'阴影,它就会起作用(否则,它在推断面部颜色方面做得很差;见下文)。

诀窍是创建一个色彩映射,当Colormapn(包括)之间的0个均匀间隔的数字再现原始颜色数组时,

1

同样,请注意,def colors_to_cmap(colors): ''' colors_to_cmap(nx3_or_nx4_rgba_array) yields a matplotlib colormap object that, when that will reproduce the colors in the given array when passed a list of n evenly spaced numbers between 0 and 1 (inclusive), where n is the length of the argument. Example: cmap = colors_to_cmap(colors) zs = np.asarray(range(len(colors)), dtype=np.float) / (len(colors)-1) # cmap(zs) should reproduce colors; cmap[zs[i]] == colors[i] ''' colors = np.asarray(colors) if colors.shape[1] == 3: colors = np.hstack((colors, np.ones((len(colors),1)))) steps = (0.5 + np.asarray(range(len(colors)-1), dtype=np.float))/(len(colors) - 1) return matplotlib.colors.LinearSegmentedColormap( 'auto_cmap', {clrname: ([(0, col[0], col[0])] + [(step, c0, c1) for (step,c0,c1) in zip(steps, col[:-1], col[1:])] + [(1, col[-1], col[-1])]) for (clridx,clrname) in enumerate(['red', 'green', 'blue', 'alpha']) for col in [colors[:,clridx]]}, N=len(colors)) 着色是必需的。为了说明失败的原因,以下代码块显示了我的特定用例。 (我正在绘制具有部分透明数据覆盖的扁平皮质片的一部分)。在此代码中,有40,886个顶点('gouraud')和81,126个三角形(the_map.coordinates中); the_map.indexed_faces数组的形状为colors

以下代码适用于'gouraud'着色:

(40886, 3)
  

Plot using Gouraud shading

但是没有'gouraud'阴影,脸颜色可能是根据它们的顶点平均值来分配的(尚未验证),这显然是错误的:

tri = matplotlib.tri.Triangulation(the_map.coordinates[0],
                                   the_map.coordinates[1],
                                   triangles=the_map.indexed_faces.T)
cmap = rgbs_to_cmap(colors)
zs = np.asarray(range(the_map.vertex_count), dtype=np.float) / (the_map.vertex_count - 1)
plt.figure(figsize=(16,16))
plt.tripcolor(tri, zs, cmap=cmap, shading='gouraud')
  

enter image description here

答案 1 :(得分:0)

创建颜色贴图的一种更简单的方法是通过from_list

z = numpy.arange(n)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
    'mymap', rgb, N=len(rgb)
    )

答案 2 :(得分:0)

在使用tripcolor函数时,必须使用色彩映射表,但内部调用的PolyCollectionTriMesh类(来自matplotlib.collection)可以处理RGB颜色数组也是如此。我已基于tripcolor源使用以下代码来绘制具有给定RGB面部颜色的三角形网格:

tri = Triangulation(...)
colors = nx3 RGB array
maskedTris = tri.get_masked_triangles()
verts = np.stack((tri.x[maskedTris], tri.y[maskedTris]), axis=-1)
collection = PolyCollection(verts)
collection.set_facecolor(colors)
plt.gca().add_collection(collection)
plt.gca().autoscale_view()

要设置每个顶点的颜色(Gouraud底纹),请改用TriMesh(与set_facecolor一起使用。)