Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

matplotlib - Polar histogram in Python for given r, theta and z values

I have a dataframe consisting of measurements from a particular magnetometer station over time, with columns corresponding to:

  • its latitude (which I think of as a radius)
  • its azimuthal angle
  • a measured quantity at this specific time

I was wondering of a way to plot this dataframe as a polar histogram for the measured variable: ie something like this:

Shi et al, 2018, doi:10.1029/2017JA025033

I have looked at the special histogram in physt but this allows me to only put in x,y values and I'm quite confused by it all.

Could anybody help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Calculating a histogram is easily done with numpy.histogram2d. Plotting the resulting 2D array can be done with matplotlib's pcolormesh.

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

# two input arrays
azimut = np.random.rand(3000)*2*np.pi
radius = np.random.rayleigh(29, size=3000)

# define binning
rbins = np.linspace(0,radius.max(), 30)
abins = np.linspace(0,2*np.pi, 60)

#calculate histogram
hist, _, _ = np.histogram2d(azimut, radius, bins=(abins, rbins))
A, R = np.meshgrid(abins, rbins)

# plot
fig, ax = plt.subplots(subplot_kw=dict(projection="polar"))

pc = ax.pcolormesh(A, R, hist.T, cmap="magma_r")
fig.colorbar(pc)

plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...