0

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

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], -3, 3), show=False, surface_color = lambda a, b, c : -c, cmap = 'gray')
p2 = plot3d(Fx, (x[0], -3, 3), (x[1], -3, 3), show=False, surface_color = lambda a, b, c : -c, cmap = 'gray')


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

4

1 回答 1

0

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

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], -3, 3), show=False, surface_color=lambda a, b, c: -c, cmap='gray')
p2 = plot3d(Fx, (x[0], -3, 3), (x[1], -3, 3), show=False, surface_color=lambda a, b, c: -c, 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()

结果图

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, 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], -3, 3), show=False, surface_color=lambda a, b, c: -c)
p2 = plot3d(Fx, (x[0], -3, 3), (x[1], -3, 3), show=False, surface_color=lambda a, b, c: -c)

plotgrid = sp.plotting.PlotGrid(1, 2, p1, p2, 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()
于 2021-03-05T01:45:43.643 回答