Matplotlib set_clip_path需要绘制补丁 [英] Matplotlib set_clip_path requires patch to be plotted

查看:393
本文介绍了Matplotlib set_clip_path需要绘制补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了一种创建Matplotlib填充轮廓图的好方法,该轮廓图被裁剪到任意多边形区域.该方法要求在Matplotlib的contourf()函数返回的QuadContourSet中的每个PathCollection实例上调用set_clip_path(patch). MWE:

I've just discovered a nice way to create a Matplotlib filled contour plot clipped to an arbitrary polygonal region. The method requires calling set_clip_path(patch) on each PathCollection instance in the QuadContourSet returned by Matplotlib's contourf() function. MWE:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
import matplotlib.path as mpath

# some arbitrary data to plot
xx, yy = np.meshgrid(np.linspace(-5, 5, 20), np.linspace(-10, 10, 20), copy=False)
zz = np.sqrt(xx ** 2 + yy ** 2)

poly_verts = [
    (0, 0),
    (-4, 7),
    (-4, -7),
    (4, -7),
    (4, 7),
    (0, 0)
]
poly_codes = [mpath.Path.MOVETO] + (len(poly_verts) - 2) * [mpath.Path.LINETO] +
mpath.Path.CLOSEPOLY]

# create a Path from the polygon vertices
path = mpath.Path(poly_verts, poly_codes)

# create a Patch from the path
patch = mpatches.PathPatch(path, facecolor='none', edgecolor='k')

plt.figure()
ax = plt.gca()
cont = plt.contourf(xx, yy, zz, 50)

# add the patch to the axes
ax.add_patch(patch)  ## TRY COMMENTING THIS OUT
for col in cont.collections:
    col.set_clip_path(patch)

plt.show()

我对一个方面感到困惑:如果我注释掉绘制补丁的那一行,那么所有剪辑都不起作用,最后我得到了一个空白图表.我假设在使用PathCollection上的补丁程序调用set_clip_path方法时,该补丁程序必须已添加到轴中,但是我不明白为什么.为补丁创建设置edgecolor='none'是一个很好的解决方法,但是这样做的乐趣何在?

I'm confused about one aspect: if I comment out the line that plots the patch, then none of the clipping works and I end up with a blank plot. I presume that when calling the set_clip_path method with a patch on the PathCollection, the patch must have been added to the axes, but I don't understand why. Setting edgecolor='none' for patch creation is a fine workaround, but where's the fun in that?

有什么想法吗?

推荐答案

如果未将补片添加到轴上,则无法确定应按照哪个变换对其进行处理.通过将其添加到轴,可以隐式地将转换设置为添加到轴的数据转换.
我想如果您想在一个图形上有多个轴,这种必要性就变得很清楚.然后mpatches.PathPatch可能会用于那些轴中的任何一个.

If the patch is not added to the axes, is cannot know according to which transform it should be handled. By adding it to the axes, you implicitely set the transform to the data transformation of the axes it is added to.
I guess this necessity becomes clear if you imagine to have several axes on a figure. Then the mpatches.PathPatch could potentially be used for any of those axes.

您确实可以通过将face-和edgecolor设置为"none"

You may indeed set the patch invisible by setting the face- and edgecolor to "none"

patch = mpatches.PathPatch(path, facecolor='none', edgecolor='none')

或将其全部隐藏起来,

patch = mpatches.PathPatch(path, visible=False)

如果您真的想摆脱将补片添加到轴上的情况,可以手动设置所需的变换

In case you really want to get rid of adding the patch to the axes, you may set the required transform manually

patch = mpatches.PathPatch(path, transform=ax.transData)

for col in cont.collections:
    col.set_clip_path(patch)

在这种情况下,不需要将其添加到轴中.

In this case there would not be any need to add it to the axes.

这篇关于Matplotlib set_clip_path需要绘制补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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