这是取自Mathematica的示例。
Plot[Abs[Exp[2 I x - x^2/2]], {x, -4, 4}, Filling -> Axis,
FillingStyle -> Automatic,
ColorFunction -> Function[{x, y}, Hue[Rescale[Arg[Exp[2 I x - x^2/2]], {-Pi, Pi}]]],
ColorFunctionScaling -> False]
产生下图
我想在python中进行等效绘制。 matplotlib是否有等效的colorfunction选项?
python参考方案
这不像您的Mathematica示例那么优雅,但是以下代码在matplotlib中复制了您的示例。基本思想是将函数绘制为不可见的多边形,显示归一化颜色图的图像(使用自定义范数函数将值包装在±pi/2之外),然后将函数多边形作为剪切蒙版应用到该图像。
码:
# Function (improve smoothness of plot by increasing samples from 500)
x = np.linspace(-4,4,500)
y = abs(np.e**(2j*x - x**2/2))
# Set up figure
fig, ax = plt.subplots()
ax.set_ylim(ymin=0, ymax=1)
# Plot line without fill
line, = ax.fill(x, y, facecolor='none')