简体   繁体   English

Python matplotlib / pylab - 3D地毯图

[英]Python matplotlib/pylab - a 3D carpet plot

I would like to draw a 3D carpet plot with Python Pylab or matplotlib. 我想用Python Pylab或matplotlib绘制3D地毯图。 I explain what I mean with carpet plot: 我解释了地毯情节的意思:

I have points on a X,Y meshgrid. 我在X,Y网格上有点。 This points have an elevation attached to them, but also an integer from a finite set. 这些点附有一个高程,但也是一个有限集的整数。 A common 3D plot makes it possible to vizualize relief. 一个常见的3D图可以使浮雕成为可能。 Elevation is along Z, we can vizualize elevation for a points determined by its coordinates X,Y along Z-axis. 高程沿着Z,我们可以对由其坐标X,Y沿Z轴确定的点的高程进行可视化。 Alternatively, I draw heatmaps for elevation. 或者,我绘制热图以进行提升。 It is a 2D plot, each pixel determined by X,Y has a color that is a function of altitude, red for apex and blue for bottom. 它是2D图,每个像素由X,Y确定,颜色是高度的函数,红色是顶点,蓝色是底部。 I do some clustering on these data, and after clustering, I have, for each pixel, two information: elevation and a label (integer). 我对这些数据进行了一些聚类,在聚类之后,对于每个像素,我有两个信息:高程和标签(整数)。 I can draw a 2D map with each pixel colored with its label, and this gives me the clustering results. 我可以绘制一个2D地图,每个像素都用其标签着色,这样就可以得到聚类结果。

Now, I would like to plot both information on same 3D graph. 现在,我想在同一个3D图上绘制两个信息。 Z coordinate should be elevation, and the point on surface should be colored with its "cluster color". Z坐标应为高程,表面上的点应以其“簇颜色”着色。 This is what I call the 3D carpet plot (if term does not sound correct, please tell me). 这就是我所说的3D地毯图(如果术语听起来不正确,请告诉我)。

Your "cluster color" is some set of conditions on the X,Y,Z values of your grid. 您的“簇颜色”是网格的X,Y,Z值的一些条件。 If you can make these a mask (of True/False values) it is simple to map a color onto them. 如果你可以使这些掩码( True/False值),将颜色映射到它们上很简单。 I've given an example of an arbitrary set of conditions, which you can adapt to your needs. 我给出了一组任意条件的例子,您可以根据自己的需要进行调整。 Code was adapted from http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html : 代码改编自http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

# Some sample data
x_side = np.arange(-5, 5, 0.04)
y_side = np.arange(-5, 5, 0.04)
X, Y = np.meshgrid(x_side,y_side)

# Fake mountains
Z = np.exp(-(X**2+Y**2)) + 2*np.exp(-((X-2)**2+Y**2)) 

# Assign colors based off some user-defined condition
COLORS = np.empty(X.shape, dtype=str)
COLORS[:,:] = 'b'
COLORS[(Z>.1) * (Z<.3)] = 'r'
COLORS[Z>.3] = 'g'
COLORS[X+Y < -1] = 'k'

# 3D surface plot
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, facecolors=COLORS, rstride=1, cstride=1,
        linewidth=0)
plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM