简体   繁体   English

matplotlib plot_surface带有非线性彩色图的3D图

[英]matplotlib plot_surface 3D plot with non-linear color map

I have this following python code, which displays the following 3D plot. 我有以下python代码,它显示以下3D图。 情节显示在地板上的凹凸

My code is: 我的代码是:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np


# Generate data example
X,Y = np.meshgrid(np.arange(-99,-90), np.arange(-200,250,50))
Z = np.zeros_like(X)
Z[:,0] = 100.
Z[4][7] = 10

# Normalize to [0,1]
Z = (Z-Z.min())/(Z.max()-Z.min())
colors = cm.viridis(Z)
rcount, ccount, _ = colors.shape

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rcount=rcount, ccount=ccount,
                       facecolors=colors, shade=False)

surf.set_facecolor((0,0,0,0))
plt.show()

I want to color the irregularities on the XY plane in a different color. 我想以不同的颜色为XY平面上的不规则颜色着色。 I want to be able to highlight the bumps on the XY plane. 我希望能够突出XY平面上的凸起。 How do I do that? 我怎么做?

The problem is that the grid is not very dense. 问题是网格不是很密集。 The bump consist of a single pixel. 凹凸由单个像素组成。 So there are 4 cells in the grid, 3 of which have their lower left corner at 0, and would hence not receive a different color according to their value. 因此网格中有4个单元格,其中3个单元格的左下角为0,因此根据其值不会接收到不同的颜色。 Only the one pixel which actually is the bump gets colorized. 只有实际上凹凸的一个像素才会变色。

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

X,Y = np.meshgrid(np.arange(-99,-90), np.arange(-200,250,50))
Z = np.zeros_like(X)
Z[:,0] = 100.
Z[4][7] = 10

norm = plt.Normalize(Z.min(),Z.min()+10 )
colors = cm.viridis(norm(Z))

fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(X, Y, Z, facecolors=colors, shade=False)                    
surf.set_facecolor((0,0,0,0))

plt.show()

在此输入图像描述

Now you may expand the colorized part of the plot, eg using scipy.ndimage.grey_dilation , such that all pixels that are adjacent also become yellow. 现在,您可以扩展绘图的颜色部分,例如使用scipy.ndimage.grey_dilation ,这样所有相邻的像素也会变为黄色。

from scipy import ndimage
C = ndimage.grey_dilation(Z, size=(2,2), structure=np.ones((2, 2)))
norm = plt.Normalize(Z.min(),Z.min()+10 )
colors = cm.viridis(norm(C))

在此输入图像描述

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

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