使用 matplotlib 仅在圆圈内绘制绘图
Draw plot only inside a circle with matplotlib
我有分散的数据,我正在将其插值到网格化数据中。之后我用 contourf 函数绘制所有内容。最后我只想在定义的圆圈内绘制所有内容。其他的都应该是白色的,但我不知道如何实现。有没有简单的方法可以做到这一点?
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
def func(x, y):
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
plt.contourf(grid_x,grid_y,grid_z0)
plt.savefig("plot_data_in_circle.png")
编辑:我附上了我的代码中的情节:
它应该是这样的:
这可以通过创建一个 Circle
补丁,然后将其设置为 clip_path
。
要使用 clip_path
,通常会存储要应用剪辑的艺术家,然后在其上使用 .set_clip_path
。在这种情况下,因为您使用的是 contourf
, there is no method .set_clip_path
, so we have to iterate over the PathCollections
stored with the QuadContourSet
returned from contourf
, and set the clip path on each of them using the set_clip_path
method。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import matplotlib.patches as patches
def func(x, y):
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
fig, ax = plt.subplots()
cs = ax.contourf(grid_x,grid_y,grid_z0)
circ = patches.Circle((0.6, 0.4), 0.3, transform=ax.transData)
for coll in cs.collections:
coll.set_clip_path(circ)
plt.show()
我有分散的数据,我正在将其插值到网格化数据中。之后我用 contourf 函数绘制所有内容。最后我只想在定义的圆圈内绘制所有内容。其他的都应该是白色的,但我不知道如何实现。有没有简单的方法可以做到这一点?
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
def func(x, y):
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
plt.contourf(grid_x,grid_y,grid_z0)
plt.savefig("plot_data_in_circle.png")
编辑:我附上了我的代码中的情节:
它应该是这样的:
这可以通过创建一个 Circle
补丁,然后将其设置为 clip_path
。
要使用 clip_path
,通常会存储要应用剪辑的艺术家,然后在其上使用 .set_clip_path
。在这种情况下,因为您使用的是 contourf
, there is no method .set_clip_path
, so we have to iterate over the PathCollections
stored with the QuadContourSet
returned from contourf
, and set the clip path on each of them using the set_clip_path
method。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import matplotlib.patches as patches
def func(x, y):
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
fig, ax = plt.subplots()
cs = ax.contourf(grid_x,grid_y,grid_z0)
circ = patches.Circle((0.6, 0.4), 0.3, transform=ax.transData)
for coll in cs.collections:
coll.set_clip_path(circ)
plt.show()