gpt4 book ai didi

python - matplotlib 中的散点图和组合极坐标直方图

转载 作者:行者123 更新时间:2023-12-04 00:56:06 24 4
gpt4 key购买 nike

我正在尝试生成这样的图,它结合了笛卡尔散点图和极坐标直方图。 (径向线可选) enter image description here存在类似的解决方案(通过 Nicolas Legrand )来查看 x 和 y 中的 差异( code here ),但我们需要查看 比率(即 x/y )。 enter image description here

更具体地说,当我们想要查看相对风险度量(即两个概率之比)时,这很有用。

散点图本身显然没有问题,但极坐标直方图更高级。

我发现的最有希望的线索是 matplotlib 库中的这个中心示例 here enter image description here

我曾尝试这样做,但遇到了我的 matplotlib 技能的限制。朝着这个目标迈进的任何努力都会很棒。

最佳答案

我相信其他人会有更好的建议,但一种获得您想要的东西的方法(不需要额外的轴艺术家)是使用带有散点和条形的极坐标投影一起绘制图表。类似的东西

import matplotlib.pyplot as plt
import numpy as np

x = np.random.uniform(size=100)
y = np.random.uniform(size=100)

r = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)

h, b = np.histogram(phi, bins=np.linspace(0, np.pi/2, 21), density=True)
colors = plt.cm.Spectral(h / h.max())

ax = plt.subplot(111, projection='polar')
ax.scatter(phi, r, marker='.')
ax.bar(b[:-1], h, width=b[1:] - b[:-1],
align='edge', bottom=np.max(r) + 0.2, color=colors)
# Cut off at 90 degrees
ax.set_thetamax(90)
# Set the r grid to cover the scatter plot
ax.set_rgrids([0, 0.5, 1])
# Let's put a line at 1 assuming we want a ratio of some sort
ax.set_thetagrids([45], [1])

这会给 enter image description here

它缺少轴标签和一些美化,但它可能是一个开始的地方。希望对您有所帮助。

关于python - matplotlib 中的散点图和组合极坐标直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62457321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com