简体   繁体   中英

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.

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). 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))

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM