Amy*_*myS 12 python matplotlib
我plot(x,y,'r')用来绘制一个红色圆圈.x和y是阵列,当配对为(x,y)并绘制时,所有点形成一条圆线.
fill(x,y,'r') 绘制一个填充(或着色)红色的红色圆圈.
如何将圆圈保持在内侧,但是在圆圈外面填充到轴边界?
我研究过使用fill_between(x_array, y1_array, y2_array, where)但是稍微玩了一下之后我认为这对我的x,y阵列不起作用.我想到fill_between()了圆圈之外,并且在一个由轴边界定义的正方形内,但我认为fill_between()没有能力...我敢肯定我可以把它变成一个整体类型的问题,delta x和delta y去零,但我不愿意.
如果有人能看到我遗失的东西fill_between()请告诉我.
我真正需要做的是屏蔽2d数组中的数字,这些数字位于用x和y创建的圆的边界之外,这样当2D数组被视为颜色图或轮廓时,圆内将是图像,外面会被白化.
这可以通过2D阵列的掩蔽技术来实现吗?喜欢用masked_where()吗?我还没有调查过,但愿意.
有任何想法吗?谢谢
编辑1:这是我有权表明我认为将解释我的问题.
from pylab import *
from matplotlib.path import Path
from matplotlib.patches import PathPatch
f=Figure()
a=f.add_subplot(111)
# x,y,z are 2d arrays
# sometimes i plot a color plot
# im = a.pcolor(x,y,z)
a.pcolor(x,y,z)
# sometimes i plot a contour
a.contour(x,y,z)
# sometimes i plot both using a.hold(True)
# here is the masking part.
# sometimes i just want to see the boundary drawn without masking
# sometimes i want to see the boundary drawn with masking inside of the boundary
# sometimes i want to see the boundary drawn with masking outside of the boundary
# depending on the vectors that define x_bound and y_bound, sometimes the boundary
# is a circle, sometimes it is not.
path=Path(vpath)
patch=PathPatch(path,facecolor='none')
a.add_patch(patch) # just plots boundary if anything has been previously plotted on a
if ('I want to mask inside'):
patch.set_facecolor('white') # masks(whitens) inside if pcolor is currently on a,
# but if contour is on a, the contour part is not whitened out.
else: # i want to mask outside
im.set_clip_path(patch) # masks outside only when im = a.pcolor(x,y,z)
# the following commands don't update any masking but they don't produce errors?
# patch.set_clip_on(True)
# a.set_clip_on(True)
# a.set_clip_path(patch)
a.show()
Run Code Online (Sandbox Code Playgroud)
Joe*_*ton 13
我真正需要做的是屏蔽2d数组中的数字,这些数字位于用x和y创建的圆的边界之外,这样当2D数组被视为颜色图或轮廓时,圆内将是图像,外面会被白化.
您有两种选择:
首先,您可以为图像使用蒙版数组.这更复杂但更安全一些.要遮罩圆外的阵列,请从中心点生成距离贴图,并遮挡距离大于半径的距离.
更简单的选择是在绘制图像后用im.set_clip_path()剪切补丁的区域.
从matplotlib库中查看此示例.不幸的是,根据我的经验,对于一些轴(非笛卡尔轴),这可能有点小问题.但在其他所有情况下,它应该完美地运作.
编辑:顺便提一下,这是你最初问的方法:绘制一个里面有洞的多边形.但是,如果你只是想掩盖一个图像,那么你最好使用上面两个选项中的任何一个.
编辑2:只是给出两种方式的快速例子......
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def main():
# Generate some random data
nx, ny = 100, 100
data = np.random.random((ny,nx))
# Define a circle in the center of the data with a radius of 20 pixels
radius = 20
center_x = nx // 2
center_y = ny // 2
plot_masked(data, center_x, center_y, radius)
plot_clipped(data, center_x, center_y, radius)
plt.show()
def plot_masked(data, center_x, center_y, radius):
"""Plots the image masked outside of a circle using masked arrays"""
# Calculate the distance from the center of the circle
ny, nx = data.shape
ix, iy = np.meshgrid(np.arange(nx), np.arange(ny))
distance = np.sqrt((ix - center_x)**2 + (iy - center_y)**2)
# Mask portions of the data array outside of the circle
data = np.ma.masked_where(distance > radius, data)
# Plot
plt.figure()
plt.imshow(data)
plt.title('Masked Array')
def plot_clipped(data, center_x, center_y, radius):
"""Plots the image clipped outside of a circle by using a clip path"""
fig = plt.figure()
ax = fig.add_subplot(111)
# Make a circle
circ = patches.Circle((center_x, center_y), radius, facecolor='none')
ax.add_patch(circ) # Plot the outline
# Plot the clipped image
im = ax.imshow(data, clip_path=circ, clip_on=True)
plt.title('Clipped Array')
main()
Run Code Online (Sandbox Code Playgroud)

编辑2:在原始绘图上绘制蒙版多边形:这里有一些关于如何绘制多边形的详细信息,该多边形在当前绘图上屏蔽其外部的所有内容.显然,没有更好的方法来剪切等高线图(无论如何......我都能找到).
import numpy as np
import matplotlib.pyplot as plt
def main():
# Contour some regular (fake) data
grid = np.arange(100).reshape((10,10))
plt.contourf(grid)
# Verticies of the clipping polygon in counter-clockwise order
# (A triange, in this case)
poly_verts = [(2, 2), (5, 2.5), (6, 8), (2, 2)]
mask_outside_polygon(poly_verts)
plt.show()
def mask_outside_polygon(poly_verts, ax=None):
"""
Plots a mask on the specified axis ("ax", defaults to plt.gca()) such that
all areas outside of the polygon specified by "poly_verts" are masked.
"poly_verts" must be a list of tuples of the verticies in the polygon in
counter-clockwise order.
Returns the matplotlib.patches.PathPatch instance plotted on the figure.
"""
import matplotlib.patches as mpatches
import matplotlib.path as mpath
if ax is None:
ax = plt.gca()
# Get current plot limits
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Verticies of the plot boundaries in clockwise order
bound_verts = [(xlim[0], ylim[0]), (xlim[0], ylim[1]),
(xlim[1], ylim[1]), (xlim[1], ylim[0]),
(xlim[0], ylim[0])]
# A series of codes (1 and 2) to tell matplotlib whether to draw a line or
# move the "pen" (So that there's no connecting line)
bound_codes = [mpath.Path.MOVETO] + (len(bound_verts) - 1) * [mpath.Path.LINETO]
poly_codes = [mpath.Path.MOVETO] + (len(poly_verts) - 1) * [mpath.Path.LINETO]
# Plot the masking patch
path = mpath.Path(bound_verts + poly_verts, bound_codes + poly_codes)
patch = mpatches.PathPatch(path, facecolor='white', edgecolor='none')
patch = ax.add_patch(patch)
# Reset the plot limits to their original extents
ax.set_xlim(xlim)
ax.set_ylim(ylim)
return patch
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
6752 次 |
| 最近记录: |