2010-03-21 93 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