开发者

How to format contour lines from Matplotlib

I am working on using Matplotlib to produce plots of implicit equations (eg. y^x=x^y). With many thanks to the help I have already received I have got quite far with it. I have used a contour line to produce the plot. My remaining problem is with formatting the contour line eg width, color and especially zorder, where the contour appears behind my gridlines. These work fine when plotting a standard function of course.

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='-',co开发者_如何转开发lor='0.8') 

plt.show() 


This is rather hackish but...

Apparently in the current release Matplotlib does not support zorder on contours. This support, however, was recently added to the trunk.

So, the right way to do this is either to wait for the 1.0 release or just go ahead and re-install from trunk.

Now, here's the hackish part. I did a quick test and if I changed line 618 in

python/site-packages/matplotlib/contour.py

to add a zorder into the collections.LineCollection call, it fixes your specific problem.

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

Not the right way to do things, but might just work in a pinch.

Also off-topic, if you accept some responses to your previous questions, you probably get quicker help around here. People love those rep points :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜