2010-03-20 48 views

回答

18

我不相信有這很好的支持,但你可以嘗試像

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 docscontour:如果第四個參數是那麼一個序列它指定了等高線繪製。但情節只會與你的範圍的分辨率一樣好,並且有些特徵可能永遠不會正確,通常在自我交叉點。

+0

這是一個很好的解決方案。我的解決方案是使用相同的基本概念獲取相同信息的更人工方式:將隱式方程設置爲f(x,y),使得f(x,y)= 0等價於原始隱式方程並隔離其零輪廓。 – 2010-03-20 20:53:03

6

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() 
1

許多謝謝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() 
+0

@Geddes,它看起來像支持zorder的輪廓最近才被添加到matplotlib源文件中。從他們的SVN主幹:http://matplotlib.svn.sourceforge.net/viewvc/matplotlib?view = rev&revision = 8098 – Mark 2010-03-21 14:19:21

14

既然你已經標記爲sympy這個問題,我會給這樣一個例子。

從文檔:http://docs.sympy.org/modules/plotting.html

from sympy import var, Plot 
var('x y') 
Plot(x*y**3 - y*x**3) 
+1

看起來新的語法是'plot_implicit(Eq(x ** 5 + y ** 5,1))',並且新的doc鏈接[在這裏](http://docs.sympy .ORG /最新/模塊/ plotting.html)。 – 2017-10-12 12:44:16

4

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 
+0

現在看來它已經發布。 :) – 2016-06-25 19:18:26

+0

你的'the_matplotlib_axes_instance'從哪裏來? – theV0ID 2017-02-15 14:39:18

+0

'p'是你創建的情節。 'p._backend._ax'將是axes實例,如果你希望你可以在一個新的變量中引用它,並將它用於你將使用任何matplotlib軸實例的任何東西。 – Krastanov 2017-02-15 20:51:20

相關問題