如何自定义并向 Sympy 图添加额外元素? [英] How to customize and add extra elements to Sympy plots?

查看:46
本文介绍了如何自定义并向 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.

推荐答案

根据 plot.py,你可以通过_backend属性获取SymPy使用的Matplotlib轴和图形的后端包装器,然后像任何其他Matplotlib一样修改属性对象.检查这个例子:

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天全站免登陆