matplotlib极性2d直方图

matplotlib极性2d直方图

本文介绍了matplotlib极性2d直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在极轴上绘制一些直方图数据,但它似乎无法正常工作.下面是一个示例,我使用找到的自定义投影它适用于散点图,所以我认为我的问题在于直方图功能.这整天让我发疯,有谁知道我在做什么错...........

I am trying to plot some histogrammed data on a polar axis but it wont seem to work properly. An example is below, I use the custom projection found How to make the angles in a matplotlib polar plot go clockwise with 0° at the top? it works for a scatter plot so I think my problem is with the histogram function. This has been driving me nuts all day, does anyone know what I am doing wrong...........

import random
import numpy as np
import matplotlib.pyplot as plt

baz = np.zeros((20))
freq = np.zeros((20))
pwr = np.zeros((20))
for x in range(20):
    baz[x] = random.randint(20,25)*10
    freq[x] = random.randint(1,10)*10
    pwr[x] = random.randint(-10,-1)*10
baz = baz*np.pi/180.

abins = np.linspace(0,2*np.pi,360)      # 0 to 360 in steps of 360/N.
sbins = np.linspace(1, 100)
H, xedges, yedges = np.histogram2d(baz, freq, bins=(abins,sbins), weights=pwr)

plt.figure(figsize=(14,14))
plt.subplot(1, 1, 1, projection='northpolar')
#plt.scatter(baz, freq)
plt.pcolormesh(H)
plt.show()

推荐答案

如果您将mgrid(具有与垃圾箱和垃圾箱类似的特征)显式传递给pcolormesh命令,则您的代码将起作用.

Your code works if you explicitly pass a mgrid (with similar characteristics than your a bins and sbins) to the pcolormesh command.

下面是一个受您的代码启发的示例:

Below is an example inspired by your code:

import matplotlib.pyplot as plt
import numpy as np

#Generate the data
size = 200

baz = 10*np.random.randint(20, 25, size)*np.pi/180.
freq = 10*np.random.randint(1, 10, size)
pwr = 10*np.random.randint(-10, -1, size)

abins = np.linspace(0, 2*np.pi, 360)      # 0 to 360 in steps of 360/N.
sbins = np.linspace(1, 100, 50)
H, xedges, yedges = np.histogram2d(baz, freq, bins=(abins,sbins), weights=pwr)

#Grid to plot your data on using pcolormesh
theta, r = np.mgrid[0:2*np.pi:360j, 1:100:50j]

fig, ax = plt.subplots(figsize=(14,14), subplot_kw=dict(projection='northpolar'))
ax.pcolormesh(theta, r, H)

ax.set_yticklabels([]) #remove yticklabels

plt.show()

这篇关于matplotlib极性2d直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!