簡體   English   中英

如何使用 matplotlib 創建圓形 2D 圖,其中函數取決於與圖像中心的距離?

[英]How to create a circular 2D plot with matplotlib where function depends on the distance from the center of the image?

我有一個一維的函數(如下圖所示)。 我正在檢查所有 matplotlib 教程,但我找不到在二維圖中繪制一維數據的解決方案,其中與圖像中心距離相同的所有點都具有相同的值。

從這樣的功能: 在此處輸入圖片說明

我想得到這樣的東西:

在此處輸入圖片說明

我正在嘗試使用 Axes3D 和 imshow,但它們需要 (x,y) 坐標而不是 r(距中心的距離)。 謝謝!

本質上,您需要在笛卡爾網格上評估您的函數。 因此,不是使用半徑r func(r)調用您的函數,而是使用從網格節點xy計算的半徑來調用它, 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM