Skip to main content
 首页 » 编程设计

python之使用 matplotlib 仅在圆圈内绘制绘图

2024年09月07日10yxwkf

<分区>

我有分散的数据,我正在将其插值到网格数据中。之后我用 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") 

编辑:我附上了我的代码中的情节:

它应该是这样的: