有时当 x 减小时 y 增大:
from sympy import symbols, plot
p = symbols('p', positive=True)
h = (3.731444 - p**0.1902631) / 0.841728e-4
p_low = 150
p_high = 1013.25
plot(h, (p, p_low, p_high), axis_center=(p_low,0))
让曲线向右上角增长会更方便,可以用 sympy
plot
函数做到这一点吗?
我尝试使用反向
range
和反向extent
但没有成功。
可以通过交换
xlim
的顺序来实现:
plot(h, (p, p_low, p_high), axis_center=(p_low,0), xlim=(max, min))
使用
plot
的 backend
参数,您还可以访问 matplolib 后端。它的优点是您可以注入 matplotlib
命令。需要 BaseBackend
的子类。
有关详细信息,请参阅MatplotlibBackend的源代码。
这里有一个说明性的例子:
import sympy.plotting.plot as plot
from sympy.plotting.plot import MatplotlibBackend
from sympy import symbols
class SwapXAxis(MatplotlibBackend):
def show(self):
# here matplolib code
for ax in self.ax:
# reverse scale on x-axis
ax.invert_xaxis()
# anchor the y-axis at the new origin
ax.spines['right'].set_position('zero')
# adjust labels and ticks
ax.yaxis.set_ticks_position("right")
ax.yaxis.set_label_position("right")
# call parent method
super().show()
p = symbols('p', positive=True)
h = (3.731444 - p**0.1902631) / 0.841728e-4
p_low = 150
p_high = 1013.25
plot(h, (p, p_low, p_high), backend=SwapXAxis, axis_center=(p_low,0))