0

I tried to plot two surfaces in one figure, both with sympy, and change the cmap, but it doesn work, it still using 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()

1 Answers1

0

The answer here seems already a bit outdated. Some experimenting gets it to work somewhat:

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()

resulting plot

PS: The previous code works on my system (sympy 1.7.1, PyCharm, Win10, Qt5Agg interactive backend), but maybe on your system unset_show() has some unwanted side effects. Could you test without that function (probably needing to start up your engin again, as unset_show() doesn't seem to have an inverse)? Maybe like this:

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()
JohanC
  • 71,591
  • 8
  • 33
  • 66