【问题标题】:Grayscale sympy plot灰度同情图
【发布时间】:2021-06-03 12:00:13
【问题描述】:

我尝试在一个图中绘制两个表面,都使用 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()

【问题讨论】:

    标签: python sympy plot3d


    【解决方案1】:

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

    【讨论】:

    • 谢谢,不过现在人影出现又很快消失
    • 更新后的版本是否更适合您?
    • 它没有,但是谢谢,我认为这是我版本的 plot3d 指令的问题。相反,我使用了一个 numpy meshgrid 和 plot_surface。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2019-11-30
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多