利用Sage和符号数学进行绘图与计算
1. 使用Sage和Matplotlib进行绘图
在使用Sage绘图时,有时可能需要对绘图的格式细节进行调整,而Sage本身的控制功能可能不够。这时可以借助Matplotlib的面向对象接口来实现。
1.1 获取Matplotlib图形对象
以下是一个示例代码,展示了如何获取Matplotlib图形对象并对其进行修改:
# Create a Sage plot, as shown in the first example
p1 = plot(sin, (-2*pi, 2*pi), thickness=2.0, rgbcolor=(0.5,1,0))
p2 = plot(cos, (-2*pi, 2*pi), thickness=3.0, color='purple',
alpha=0.5)
plt = p1 + p2
# Get the Matplotlib object
fig = plt.matplotlib()
from matplotlib.backends.backend_agg import FigureCanvasAgg
fig.set_canvas(FigureCanvasAgg(fig)) # this line is critical
ax = fig.gca() # get current axes
# Add a legend and plot title
ax.legend(['sin(x)', 'cos(x)'])
ax.set_title('Modified with matplotlib')
# Add
超级会员免费看
订阅专栏 解锁全文
7

被折叠的 条评论
为什么被折叠?



