我想繪製Matplotlib中的隱式方程(形式爲f(x,y)= g(x,y),例如X^y = y^x)。這可能嗎?是否可以使用Matplotlib繪製隱式方程?
回答
我不相信有這很好的支持,但你可以嘗試像
import matplotlib.pyplot
from numpy import arange
from numpy import meshgrid
delta = 0.025
xrange = arange(-5.0, 20.0, delta)
yrange = arange(-5.0, 20.0, delta)
X, Y = meshgrid(xrange,yrange)
# F is one side of the equation, G is the other
F = Y**X
G = X**Y
matplotlib.pyplot.contour(X, Y, (F - G), [0])
matplotlib.pyplot.show()
見API docs爲contour
:如果第四個參數是那麼一個序列它指定了等高線繪製。但情節只會與你的範圍的分辨率一樣好,並且有些特徵可能永遠不會正確,通常在自我交叉點。
matplotlib不繪製方程;它繪製了系列的點。您可以使用類似scipy.optimize
這樣的工具從數值上計算隱式方程的x值(反之亦然),或根據需要任意數量的其他工具來計算y點。
例如,這裏是我繪製隱含方程x ** 2 + x * y + y ** 2 = 10
在一定區域內的例子。
from functools import partial
import numpy
import scipy.optimize
import matplotlib.pyplot as pp
def z(x, y):
return x ** 2 + x * y + y ** 2 - 10
x_window = 0, 5
y_window = 0, 5
xs = []
ys = []
for x in numpy.linspace(*x_window, num=200):
try:
# A more efficient technique would use the last-found-y-value as a
# starting point
y = scipy.optimize.brentq(partial(z, x), *y_window)
except ValueError:
# Should we not be able to find a solution in this window.
pass
else:
xs.append(x)
ys.append(y)
pp.plot(xs, ys)
pp.xlim(*x_window)
pp.ylim(*y_window)
pp.show()
如果你願意用比matplotlib以外的東西(但仍蟒蛇),有聖人:
一個例子:http://sagenb.org/home/pub/1806
許多謝謝Steve,Mike,Alex。我已經與史蒂夫的解決方案一起(請參閱下面的代碼)。我唯一剩下的問題是等高線圖出現在我的網格線後面,而不是一個規則的情節,我可以用zorder強迫它到前面。任何更多halp非常讚賞。
乾杯, 格迪斯
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
fig = plt.figure(1)
ax = fig.add_subplot(111)
# set up axis
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# setup x and y ranges and precision
x = np.arange(-0.5,5.5,0.01)
y = np.arange(-0.5,5.5,0.01)
# draw a curve
line, = ax.plot(x, x**2,zorder=100)
# draw a contour
X,Y=np.meshgrid(x,y)
F=X**Y
G=Y**X
ax.contour(X,Y,(F-G),[0],zorder=100)
#set bounds
ax.set_xbound(-1,7)
ax.set_ybound(-1,7)
#produce gridlines of different colors/widths
ax.xaxis.set_minor_locator(MultipleLocator(0.2))
ax.yaxis.set_minor_locator(MultipleLocator(0.2))
ax.xaxis.grid(True,'minor',linestyle='-')
ax.yaxis.grid(True,'minor',linestyle='-')
minor_grid_lines = [tick.gridline for tick in ax.xaxis.get_minor_ticks()]
for idx,loc in enumerate(ax.xaxis.get_minorticklocs()):
if loc % 2.0 == 0:
minor_grid_lines[idx].set_color('0.3')
minor_grid_lines[idx].set_linewidth(2)
elif loc % 1.0 == 0:
minor_grid_lines[idx].set_c('0.5')
minor_grid_lines[idx].set_linewidth(1)
else:
minor_grid_lines[idx].set_c('0.7')
minor_grid_lines[idx].set_linewidth(1)
minor_grid_lines = [tick.gridline for tick in ax.yaxis.get_minor_ticks()]
for idx,loc in enumerate(ax.yaxis.get_minorticklocs()):
if loc % 2.0 == 0:
minor_grid_lines[idx].set_color('0.3')
minor_grid_lines[idx].set_linewidth(2)
elif loc % 1.0 == 0:
minor_grid_lines[idx].set_c('0.5')
minor_grid_lines[idx].set_linewidth(1)
else:
minor_grid_lines[idx].set_c('0.7')
minor_grid_lines[idx].set_linewidth(1)
plt.show()
@Geddes,它看起來像支持zorder的輪廓最近才被添加到matplotlib源文件中。從他們的SVN主幹:http://matplotlib.svn.sourceforge.net/viewvc/matplotlib?view = rev&revision = 8098 – Mark 2010-03-21 14:19:21
既然你已經標記爲sympy這個問題,我會給這樣一個例子。
從文檔:http://docs.sympy.org/modules/plotting.html。
from sympy import var, Plot
var('x y')
Plot(x*y**3 - y*x**3)
看起來新的語法是'plot_implicit(Eq(x ** 5 + y ** 5,1))',並且新的doc鏈接[在這裏](http://docs.sympy .ORG /最新/模塊/ plotting.html)。 – 2017-10-12 12:44:16
sympy中有一個隱式方程(和不等式)繪圖儀。它是作爲GSoC的一部分創建的,它將圖形生成爲matplotlib圖形實例。在http://docs.sympy.org/latest/modules/plotting.html#sympy.plotting.plot_implicit.plot_implicit
文檔由於sympy版本0.7.2可用的是:
>>> from sympy.plotting import plot_implicit
>>> p = plot_implicit(x < sin(x)) # also creates a window with the plot
>>> the_matplotlib_axes_instance = p._backend._ax
- 1. 是否可以繪製matplotlib hexbin圖上相同點的列表?
- 2. 是否可以以編程方式顯示/隱藏SSRS PageNumber?
- 3. 以編程方式在matplotlib中繪製疊加的偏移圖
- 4. 如何繪製隱式方程
- 5. 是否有可能以編程方式隱藏停靠圖標
- 6. matplotlib中隱式方程的繪圖系統
- 7. 是否可以使用SAS繪製實時可視化圖?
- 8. 繪製圖形使用matplotlib
- 9. 使用Matplotlib繪製網格
- 10. 使用matplotlib繪製python
- 11. 繪製[X,Y]使用matplotlib
- 12. 是否可以繪製動畫?
- 13. 是否可以自定義繪製ListViewGroup
- 14. 使用Matplotlib以圖例樣式繪製離散色條
- 15. iPhone:是否有可能以編程方式製作視頻
- 16. 是否可以使用json數據繪製圖表?
- 17. 是否可以使用vanilla JavaScript創建和繪製畫布?
- 18. 是否可以使用EA繪製指標
- 19. 是否可以使用Dc.js繪製分組條形圖?
- 20. 是否可以使用unicode或utf8在WxDC中繪製文本?
- 21. 是否可以在directx dc/buffer上使用opengl進行繪製?
- 22. 是否可以使用System.Windows.Shapes.Path繪製一個圓(閉合圓弧)?
- 23. 是否可以在paint()方法內繪製一個QGraphicsItem?
- 24. 當作爲隱式方法參數值使用時,隱式值是否也可以是一個惰性值?
- 25. 是否可以在PHP中以編程方式啓用擴展?
- 26. 用matplotlib繪製交互式圖像
- 27. 是否可以使用隱式類來擴展build.sbt語法?
- 28. 以編程方式使用HTML5畫布繪製此圖案
- 29. 是否可以用二進制編程?
- 30. 是否可以製作應用程序
這是一個很好的解決方案。我的解決方案是使用相同的基本概念獲取相同信息的更人工方式:將隱式方程設置爲f(x,y),使得f(x,y)= 0等價於原始隱式方程並隔離其零輪廓。 – 2010-03-20 20:53:03