如何从 Matplotlib 格式化等高线

发布于 2024-08-25 14:37:54 字数 1174 浏览 5 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2024-09-01 14:37:54

这相当hackish但是......

显然在当前版本中Matplotlib不支持轮廓上的zorder。不过,这种支持最近已添加到主干

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

现在,这是黑客的部分。我做了一个快速测试,如果我改变了 618 行

python/site-packages/matplotlib/contour.py

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

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

这不是正确的做事方式,但在紧要关头可能会起作用。

同样题外话,如果您接受对之前问题的一些答复,您可能会在这里获得更快的帮助。人们喜欢这些代表点:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文