sympy 函数图下方的颜色区域 [英] Color area beneath sympy function plot
本文介绍了sympy 函数图下方的颜色区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 sympy 函数,我想绘制它并为曲线下方的区域着色,我该怎么做?
代码
将sympy导入为syx = sy.symbols('x')f = sy.sin(x)sy.plot(f, (x, 0, sy.pi))
剧情
解决方案
您可以使用 plt.fill_between
(
I have a sympy function, I want to plot it and color the area beneath the curve, how can I do it?
Code
import sympy as sy
x = sy.symbols('x')
f = sy.sin(x)
sy.plot(f, (x, 0, sy.pi))
Plot
解决方案
You can use plt.fill_between
(documentation) but you need to convert your sympy
function in a numpy
array with sy.lambdify
(documentation) before, because plt.fill_between
takes in arrays.
Check this code as a reference:
import sympy as sy
import numpy as np
import matplotlib.pyplot as plt
x = sy.symbols('x')
f = sy.sin(x)
x_array = np.linspace(0, np.pi, 1000)
f_array = sy.lambdify(x, f)(x_array)
fig, ax = plt.subplots()
ax.plot(x_array, f_array, color = 'red')
ax.fill_between(x_array, f_array, facecolor = 'red', alpha = 0.3)
plt.show()
这篇关于sympy 函数图下方的颜色区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文