【发布时间】:2017-05-03 01:29:53
【问题描述】:
我有一个随机向量(随机长度和随机角度),并想通过hist2d 或hexbin 绘制其近似 PDF(概率密度函数)。不幸的是,它们似乎不适用于极坐标图,以下代码一无所获:
import numpy as np
import matplotlib.pyplot as plt
# Generate random data:
N = 1024
r = .5 + np.random.normal(size=N, scale=.1)
theta = np.pi / 2 + np.random.normal(size=N, scale=.1)
# Plot:
ax = plt.subplot(111, polar=True)
ax.hist2d(theta, r)
plt.savefig('foo.png')
plt.close()
我希望它看起来像这样:pylab_examples example code: hist2d_demo.py 仅在极坐标中。迄今为止最接近的结果是彩色散点图为adviced here:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
# Generate random data:
N = 1024
r = .5 + np.random.normal(size=N, scale=.1)
theta = np.pi / 2 + np.random.normal(size=N, scale=.1)
# Plot:
ax = plt.subplot(111, polar=True)
# Using approach from:
# https://stackoverflow.com/questions/20105364/how-can-i-make-a-scatter-plot-colored-by-density-in-matplotlib
theta_r = np.vstack([theta,r])
z = gaussian_kde(theta_r)(theta_r)
ax.scatter(theta, r, c=z, s=10, edgecolor='')
plt.savefig('foo.png')
plt.close()
Image from the second version of the code
有没有更好的方法让它更像使用 hist2d 生成的真实 PDF? This question 似乎是相关的(生成的图像符合预期),但看起来很乱。
【问题讨论】:
标签: matplotlib histogram scatter-plot polar-coordinates