本文介绍了在SymPy的``plot3d''中更改颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SymPy 中,您可以根据表达式自动绘制曲面,即

In SymPy you can automatically plot a surface from an expression, namely

from sympy import symbols
from sympy.plotting import plot3d

x, y = symbols('x y')
monkey_saddle = x**3 - 3*x*y**2
plot3d(monkey_saddle, cmap="RdYlBu")

得到

我找不到任何更改颜色图的 kwarg.我知道我可以创建一个 lambdify 然后直接使用 matplotlib 制作绘图.但我很好奇是否有办法直接在 SymPy 中做到这一点.

I could not find any kwarg that change the colormap. I know that I can create a lambdify and then directly make the plot using matplotlib. But I am curious if there is a way to do it directly in SymPy.

推荐答案

我看了sympy.plotting.plot.py的源码,好像cmap设置为jet:

I read the source code of sympy.plotting.plot.py, it seems that the cmap is set to jet:

collection = self.ax.plot_surface(x, y, z,
                                  cmap=self.cm.jet,
                                  rstride=1, cstride=1,
                                  linewidth=0.1)

需要设置collections对象的cmap,在调用plot3d()之前先调用unset_show()来禁止调用pyplot.show():

You need to set the cmap of the collections object, and before calling plot3d() call unset_show() to disable calling pyplot.show():

from sympy import symbols
from sympy.plotting import plot3d
from sympy.plotting.plot import unset_show

unset_show()

x, y = symbols('x y')
monkey_saddle = x**3 - 3*x*y**2
p = plot3d(monkey_saddle)

p._backend.ax.collections[0].set_cmap("RdYlBu_r")

这篇关于在SymPy的``plot3d''中更改颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:48