Sympy和绘图 [英] Sympy and plotting

查看:40
本文介绍了Sympy和绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Sympy如何处理图形有一些疑问.

I have a few questions about what how to work with graphics, using Sympy.

我的代码:

from sympy import *
x, y = symbols("x y")
plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))

1)获得沿x轴拉伸的图形.

1) The graph is obtained stretched along the x axis.

如何使曲线看起来像一个圆?

How to make so that the curve looked like a circle?

2)如何向图表添加其他元素.例如点 O(0, 0) 和线 y = x.

2) How to add other elements to the chart. For example, the point O(0, 0) and the line y = x.

推荐答案

根据

According to the docstring of plot.py, you can get the backend wrapper of the Matplotlib axes and figure that SymPy uses, through _backend attribute, and then modify properties as any other Matplotlib objects. Check this example:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
from sympy import *

x, y = symbols("x y")
hp = plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))
fig = hp._backend.fig
ax = hp._backend.ax
xx = yy = np.linspace(-3,3)
ax.plot(xx,yy) # y = x
ax.plot([0],[0],'o') # Point (0,0)
ax.set_aspect('equal','datalim')
fig.canvas.draw()

Sympy Plot对象具有append和extend方法,该方法允许将Plot对象添加到其他对象,但这是行不通的(至少对我而言,并使用Jupyter).

The Sympy Plot objects have append and extend methods that allows add a Plot object to other, but this don't work (at least for me and using Jupyter).

另一个选择是仅使用Matplotlib:

Another option is use only Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1,1)
xx,yy = np.linspace(-3,3), np.linspace(-3,3)
x,y = np.meshgrid(xx,yy)
ax.contour(x, y, (x**2+y**2-4), [0]);
ax.plot([0],[0],"o")
ax.plot(xx,yy)
ax.set_aspect('equal','datalim')

这篇关于Sympy和绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆