灰度对称图

问题描述

我试图在一个图形中绘制两个表面,都带有 sympy,并更改了 cmap,但它不起作用,它仍然使用 de viridis 颜色图

import sympy as sp
from sympy.plotting import plot3d
from matplotlib import pyplot as plt

x = "global"
x = sp.symbols('x0:2')

Fx = (3*((1 - x[0])**2))*(sp.exp((-1*(x[0]**2)) - ((x[1]) + 1)**2)) - (10*(((sp.Rational(1,5))*x[0]) - ((x[0])**3) - ((x[1])**5)))*(sp.exp(-1*((x[0])**2) - ((x[1])**2))) - (sp.Rational(1,3))*(sp.exp(-1*((x[0] + 1)**2) - ((x[1])**2)))
p1 = plot3d(Fx,(x[0],-3,3),(x[1],show=False,surface_color = lambda a,b,c : -c,cmap = 'gray')
p2 = plot3d(Fx,cmap = 'gray')


plotgrid = sp.plotting.PlotGrid(1,2,p1,p2,show=False)
plotgrid.show()

解决方法

答案 here 似乎已经有点过时了。一些实验使它在某种程度上起作用:

import sympy as sp
from sympy.plotting import plot3d
from sympy.plotting.plot import unset_show

unset_show()

x = sp.symbols('x0:2')

Fx = (3 * ((1 - x[0]) ** 2)) * (sp.exp((-1 * (x[0] ** 2)) - ((x[1]) + 1) ** 2)) - (
        10 * (((sp.Rational(1,5)) * x[0]) - ((x[0]) ** 3) - ((x[1]) ** 5))) * (
         sp.exp(-1 * ((x[0]) ** 2) - ((x[1]) ** 2))) - (sp.Rational(1,3)) * (
         sp.exp(-1 * ((x[0] + 1) ** 2) - ((x[1]) ** 2)))
p1 = plot3d(Fx,(x[0],-3,3),(x[1],show=False,surface_color=lambda a,b,c: -c,cmap='gray')
p2 = plot3d(Fx,cmap='gray')

plotgrid = sp.plotting.PlotGrid(1,2,p1,p2,show=False)

plotgrid.show()
plotgrid._backend.ax[0].collections[0].set_cmap("gray")
plotgrid._backend.ax[1].collections[0].set_cmap("Reds")
plotgrid._backend.ax[0].figure.show()

resulting plot

PS:之前的代码适用于我的系统(sympy 1.7.1、PyCharm、Win10、Qt5Agg 交互式后端),但可能在您的系统上 unset_show() 有一些不需要的副作用。您能否在没有该功能的情况下进行测试(可能需要再次启动您的引擎,因为 unset_show() 似乎没有反函数)?也许是这样的:

import sympy as sp
from sympy.plotting import plot3d

x = sp.symbols('x0:2')

Fx = (3 * ((1 - x[0]) ** 2)) * (sp.exp((-1 * (x[0] ** 2)) - ((x[1]) + 1) ** 2)) - (
        10 * (((sp.Rational(1,c: -c)
p2 = plot3d(Fx,c: -c)

plotgrid = sp.plotting.PlotGrid(1,show=False)

plotgrid.show() # needed to fill in ._backend
plotgrid._backend.ax[0].collections[0].set_cmap("gray")
plotgrid._backend.ax[1].collections[0].set_cmap("Reds_r")
# plotgrid._backend.ax[0].figure.show()
plotgrid.show()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...