[英]How to create a circular 2D plot with matplotlib where function depends on the distance from the center of the image?
I have a function which is 1 dimensional (like in the picture below).我有一个一维的函数(如下图所示)。 I was checking all matplotlib tutorials but I couldn't find the solution for plotting a 1D data in 2D plot where all points which the same distance from the image center will have the same value.
我正在检查所有 matplotlib 教程,但我找不到在二维图中绘制一维数据的解决方案,其中与图像中心距离相同的所有点都具有相同的值。
From function like this:从这样的功能:
I would like to get something like this:我想得到这样的东西:
I was trying with Axes3D and imshow but they need (x,y) coordinates not r (distance from the center).我正在尝试使用 Axes3D 和 imshow,但它们需要 (x,y) 坐标而不是 r(距中心的距离)。 Thanks!
谢谢!
Essentially you need to evaluate your function on a cartesian grid.本质上,您需要在笛卡尔网格上评估您的函数。 So instead of calling your function with the radius
r
, func(r)
, you would call it with the radius calculated from the grid nodes x
and y
, func(sqrt(x**2+y**2))
因此,不是使用半径
r
func(r)
调用您的函数,而是使用从网格节点x
和y
计算的半径来调用它, func(sqrt(x**2+y**2))
import numpy as np
import matplotlib.pyplot as plt
func = lambda r: np.sin(r)*np.exp(-r/10.)
r = np.linspace(0,50,151)
X,Y = np.meshgrid(np.linspace(-50,50,301),np.linspace(-50,50,301))
rad = lambda x,y: np.sqrt(x**2+y**2)
image = func(rad(X,Y))
fig, (ax,ax2) = plt.subplots(ncols=2)
ax.plot(r, func(r))
ax2.imshow(image, extent=[X.min(),X.max(),Y.min(),Y.max()])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.