如何从 Matplotlib 格式化轮廓线 [英] How to format contour lines from Matplotlib

查看:33
本文介绍了如何从 Matplotlib 格式化轮廓线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Matplotlib 生成隐式方程图(例如 y^x=x^y).非常感谢我已经得到的帮助,我已经走得很远了.我使用了等高线来生成绘图.我剩下的问题是格式化轮廓线,例如宽度、颜色,尤其是 zorder,其中轮廓出现在我的网格线后面.当然,这些在绘制标准函数时效果很好.

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='-',color='0.8') 

plt.show() 

推荐答案

这有点陈旧,但...

显然在当前版本中,Matplotlib 不支持轮廓上的 zorder.但是,此支持最近添加到主干.

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

因此,正确的方法是等待 1.0 版本发布,或者直接从主干重新安装.

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

python/site-packages/matplotlib/contour.py

将 zorder 添加到 collections.LineCollection 调用中,它可以解决您的特定问题.

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 :)

这篇关于如何从 Matplotlib 格式化轮廓线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆