python - 如何在 Python 中的函数指定的域中绘制 3 维图?

标签 python matplotlib

因此,我尝试使用 matplotlib.pyplot 绘制 3 维图,但仅限于其边界由函数设置的特定域。这是代码

import numpy
from scipy.integrate import quad
import matplotlib.pyplot
import matplotlib.ticker
from mpl_toolkits.mplot3d import Axes3D

def get_x1min(pT, eta, rS):
    return (pT * numpy.exp(eta)) / (rS - pT * numpy.exp(- eta))

def get_x2(x1, pT, eta, rS):
    return (x1 * pT * numpy.exp(- eta)) / (x1 * rS - pT * numpy.exp(eta))

rS = 1960.0
eta = 0.0
pT = [66.0, 77.5, 89.5, 92.0, 94.0, 96.0, 98.0, 100.0, 103.0, 105.0, 107.0, 109.0, 111.0, 113.0, 118.5, 136.5, 157.5, 182.0, 200.0, 209.5, 241.5, 278.5, 321.0, 370.0, 426.5, 492.0]

fig = matplotlib.pyplot.figure()
ax = Axes3D(fig)
x1_test = numpy.linspace(0.1, 1.0, 20)
x1_test, pT = numpy.meshgrid(x1_test, pT)
x2_test = get_x2(x1_test, pT, eta, rS)
ax.plot_surface(x1_test, pT, x2_test, rstride=1, cstride=1, cmap=matplotlib.pyplot.get_cmap('rainbow'))
matplotlib.pyplot.show()

这工作正常,但域是一个矩形,唯一需要的更改是让 x1_test 从可以从函数 get_x1min 获得的最小值开始。有什么方法可以实现吗?

提前致谢!

最佳答案

您可以将不需要的值设置为np.nan,然后它们将不会被绘制。但请注意,颜色映射似乎存在一些问题。您可以使用 vminvmax 关键字来解决这个问题:

mask = x1_test< get_x1min(pT, eta, rS)
x2_test[mask] = numpy.nan

ax.plot_surface(
    x1_test, pT, x2_test, rstride=1, cstride=1,
    cmap=matplotlib.pyplot.get_cmap('rainbow'),
    vmin = numpy.nanmin(x2_test),
    vmax = numpy.nanmax(x2_test),    
    )

matplotlib.pyplot.show()

其余代码保持不变。结果看起来像这样:

result of the OP's code patched with the code posted above

关于python - 如何在 Python 中的函数指定的域中绘制 3 维图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806093/

相关文章:

python - 如何使用 Python 通过 API v3 创建 Google 日历事件

python - 从 Python 数据结构中解压位置参数

python - 如何加速 matplotlib、子图绘图/绘图和保存?

python - matplotlib hexbins 中的十六进制大小基于附近点的密度

python - 如何将 hboxes 和 vboxes 代码添加到 python gtk 中的笔记本选项卡

Python 实例到 COM 对象

python - plt.plot()、object.plot() 之间有什么区别

python - matplotlib 中超过 9 个子图

python - %matplotlib 笔记本显示空白直方图

python - Django/html 表 : Description inside header row shifts to the right