Python:有没有办法绘制一个“部分"?使用 Matplotlib 绘制曲面图? [英] Python: Is there a way to plot a "partial" surface plot with Matplotlib?

查看:52
本文介绍了Python:有没有办法绘制一个“部分"?使用 Matplotlib 绘制曲面图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Matplotlib绘制如下的局部"表面图

请注意,它不是X-Y平面上的完整网格,但是在顶视图中缺少一个角.以下是我试过但没有用的代码.

将 numpy 导入为 np从 matplotlib 导入 pyplot从mpl_toolkits.mplot3d导入Axes3DX = np.array([[0,1],[0,1,2],[0,1,2,3],])Y = np.array([[0,0],[1,1,1],[2,2,2,2],])Z = np.array([[0.5, 0.6],[0.7, 0.8, 0.9],[1.0, 1.1, 1.2, 1.3],])无花果= pyplot.figure()ax = fig.add_subplot(111, 投影='3d')ax.plot_surface(X,Y,Z)

错误为:

<块引用>

ValueError:设置具有序列的数组元素.

任何指针将不胜感激!谢谢!

解决方案

您可以通过在不想绘制的区域中使用Z的 np.nan 值来轻松实现此目的.这是

from mpl_toolkits.mplot3d 导入 Axes3D从 matplotlib 导入 cm从matplotlib.ticker导入LinearLocator,FormatStrFormatter导入matplotlib.pyplot作为plt将numpy导入为np无花果= plt.figure()ax = fig.gca(投影='3d')X = np.arange(-5,5,0.25)Y = np.arange(-5,5,0.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X**2 + Y**2)Z = np.sin(.5*R)Z[X+Y>4.] = np.nan # 对角线切片surf = ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,cmap = cm.coolwarm,线宽=0,抗锯齿=假,vmin=-1,vmax=1)ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))fig.colorbar(冲浪,收缩=0.5,方面=5)plt.show()

这里还要注意,我必须在 plot 命令中使用 vminvmax 关键字,否则 nans 会抛出颜色缩放.

I wanted to plot a "partial" surface plot like the following one with Matplotlib

Note that it's not a complete meshgrid on X-Y plane but missing a corner from top view. The following is the code I tried but didn't work.

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

X = np.array([[0,1],
              [0,1,2],
              [0,1,2,3],
             ])
Y = np.array([[0,0],
              [1,1,1],
              [2,2,2,2],
             ])
Z = np.array([[0.5, 0.6],
              [0.7, 0.8, 0.9],
              [1.0, 1.1, 1.2, 1.3],
             ])
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)

The error being:

ValueError: setting an array element with a sequence.

Any pointer would be appreciated! Thanks!

解决方案

You can do this easily by using np.nan values for Z in the regions you don't want to plot. Here's a modified version of this example but with the cut, as show below:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(.5*R)

Z[X+Y>4.] = np.nan  # the diagonal slice

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False, vmin=-1, vmax=1)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

Note here also that I had to use vmin and vmax keywords in the plot command or the color scaling would be thrown by the nans.

这篇关于Python:有没有办法绘制一个“部分"?使用 Matplotlib 绘制曲面图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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