Becca
Becca

Reputation: 21

Plotting 1-D dataset with radial symmetry around the origin in Python

This is likely a very basic question, but my python knowledge is somewhat limited and I've had some trouble deciphering other questions+answers, so I'm hoping someone here can help...
I have a set of 1-D data points (the number of molecules at each point along a radius, essentially) that I want to plot as a 2-D radially symmetrical image. I want to rotate it around an origin (0,0). Basically, I have something likethis but I want something likethis

I've seen this done relatively easily with functions or defined vectors (i.e. with scipy's interpolate module), but I don't have a function or randomly-generated data -- I have a list. I know there must be a simple way to do this, but I'd really appreciate it if someone could point me in the right direction!

I've included a really really small example dataset (plotted as a line in log scale) for people to play with if they feel so inclined:

import numpy as np
import matplotlib.pyplot as plt

rad25 = np.array([25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1])
O_rad25 = np.array([1.01E+15,3.00E+14,1.20E+14,5.63E+13,2.90E+13,1.59E+13,9.21E+12,5.53E+12,3.43E+12,2.18E+12,1.42E+12,9.44E+11,6.38E+11,4.37E+11,3.03E+11,2.13E+11,1.51E+11,1.08E+11,7.77E+10,5.60E+10,4.02E+10,2.84E+10,1.94E+10,1.20E+10,5.78E+09])

plt.plot(rad25,O_rad25)
plt.yscale('log')
plt.xlabel('Radius (distance from center in um)')
plt.ylabel('Number of molecules')
plt.show()

Upvotes: 1

Views: 2617

Answers (2)

Jon
Jon

Reputation: 431

You need to create an array of values from 0 - 360 degrees, and then create a meshgrid from this array and your array of values. This can then be plotted on a radially projected subplot.

Something like this, but with your data of cause:

import numpy as np

import matplotlib.pyplot as plt

# Swap out with your data
radialVals = np.linspace(0,1)

azm = np.linspace(0, 2 * np.pi)
r, th = np.meshgrid(radialVals, azm)
z = (r ** 2.0) / 4.0

plt.subplot(projection="polar")

plt.pcolormesh(th, r, z)

plt.plot(azm, r, ls='none') 
plt.grid()

plt.show()

Upvotes: 2

JaminSore
JaminSore

Reputation: 3936

I borrowed some from Jon's answer (his is very nearly correct), and added a colorbar to illustrate, but this should get you what you want. Since you know your radius and that your data has radial symmetry, a polar plot is the natural choice.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors


fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))

azm = np.linspace(0, 2 * np.pi)
r, th = np.meshgrid(rad25, azm)
z = np.tile(O_rad25, (r.shape[0], 1))

plt.pcolormesh(th, r, z, norm=colors.LogNorm(O_rad25.min(), O_rad25.max()))
plt.colorbar(label='Number of Molecules')

enter image description here

Upvotes: 1

Related Questions