2010-03-21 116 views
3

我正在使用Matplotlib來生成隱式方程的圖(例如,y^x = x^y)。非常感謝我已經收到的幫助,我已經得到了很多。我使用了一條輪廓線來生成圖。我剩下的問題是格式化輪廓線,例如寬度,顏色和特別是zorder,輪廓出現在我的網格線後面。當繪製標準功能時,這些工作很好。如何從Matplotlib格式化輪廓線

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,linewidth=3,color='red') 

# draw a contour 
X,Y=np.meshgrid(x,y) 
F=X**Y 
G=Y**X 
ax.contour(X,Y,(F-G),[0],zorder=100,linewidth=3,color='green') 

#set bounds 
ax.set_xbound(-1,7) 
ax.set_ybound(-1,7) 

#add gridlines 
ax.xaxis.set_minor_locator(MultipleLocator(0.2)) 
ax.yaxis.set_minor_locator(MultipleLocator(0.2)) 
ax.xaxis.grid(True,'minor',linestyle='-',color='0.8') 
ax.yaxis.grid(True,'minor',linestyle='-',color='0.8') 

plt.show() 

回答

3

這是相當的hackish,但...

顯然,在當前版本Matplotlib不支持輪廓ZORDER。但是,這種支持,was recently added to the trunk

所以,正確的做法是等待1.0版本發佈,或者繼續從trunk中重新安裝。

現在,這裏是駭人聽聞的部分。我做了一個快速測試,如果我在

蟒蛇/站點包改線618/matplotlib/contour.py

添加ZORDER到collections.LineCollection電話,解決您的具體問題。

col = collections.LineCollection(nlist, 
    linewidths = width, 
    linestyle = lstyle, 
    alpha=self.alpha,zorder=100) 

不是正確的做事方式,但可能只是在一個捏。

同樣偏離主題,如果您接受對您以前的問題的回覆,您可能會在此處獲得更快的幫助。人們喜歡這些重點:)

+0

非常感謝馬克 - 謝謝你的回覆建議,我不確定它是如何工作的! – Geddes 2010-03-25 20:00:06