首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib中的散点图和组合极直方图

matplotlib中的散点图和组合极直方图
EN

Stack Overflow用户
提问于 2020-06-18 18:49:05
回答 3查看 641关注 0票数 3

我试图生成这样一个图,它结合了笛卡儿散点图和极坐标直方图。(径向线可选)

一个类似的解决方案(由尼古拉斯·列格朗)存在,用于查看x和y (代码在这里)的差异,但我们需要查看比率(即x/y)。

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

散点图本身显然不是一个问题,但极坐标直方图更高级。

我发现的最有希望的线索是matplotlib库这里中的这个中心示例。

我尝试过这样做,但遇到了材料库技能的限制。为实现这一目标所作的任何努力都将是伟大的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-18 20:58:00

好的。感谢尼古拉斯的回答和tomjn的回答,我有了一个可行的解决方案:)

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt

# Scatter data
n = 50
x = 0.3 + np.random.randn(n)*0.1
y = 0.4 + np.random.randn(n)*0.02

def radial_corner_plot(x, y, n_hist_bins=51):
    """Scatter plot with radial histogram of x/y ratios"""

    # Axis setup
    fig = plt.figure(figsize=(6,6))
    ax1 = fig.add_axes([0.1,0.1,.6,.6], label="cartesian")
    ax2 = fig.add_axes([0.1,0.1,.8,.8], projection="polar", label="polar")
    ax2.set_rorigin(-20)
    ax2.set_thetamax(90)

    # define useful constant
    offset_in_radians = np.pi/4

    def rotate_hist_axis(ax):
        """rotate so that 0 degrees is pointing up and right"""
        ax.set_theta_offset(offset_in_radians)
        ax.set_thetamin(-45)
        ax.set_thetamax(45)
        return ax

    # Convert scatter data to histogram data
    r = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    h, b = np.histogram(phi, 
                        bins=np.linspace(0, np.pi/2, n_hist_bins),
                        density=True)

    # SCATTER PLOT -------------------------------------------------------
    ax1.scatter(x,y)

    ax1.set(xlim=[0, 1], ylim=[0, 1], xlabel="x", ylabel="y")
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)


    # HISTOGRAM ----------------------------------------------------------
    ax2 = rotate_hist_axis(ax2)
    # rotation of axis requires rotation in bin positions
    b = b - offset_in_radians

    # plot the histogram
    bars = ax2.bar(b[:-1], h, width=b[1:] - b[:-1], align='edge')

    def update_hist_ticks(ax, desired_ratios):
        """Update tick positions and corresponding tick labels"""
        x = np.ones(len(desired_ratios))
        y = 1/desired_ratios
        phi = np.arctan2(y,x) - offset_in_radians
        # define ticklabels
        xticklabels = [str(round(float(label), 2)) for label in desired_ratios]
        # apply updates
        ax2.set(xticks=phi, xticklabels=xticklabels)
        return ax

    ax2 = update_hist_ticks(ax2, np.array([1/8, 1/4, 1/2, 1, 2, 4, 8]))

    # just have radial grid lines
    ax2.grid(which="major", axis="y")

    # remove bin count labels
    ax2.set_yticks([])

    return (fig, [ax1, ax2])

fig, ax = radial_corner_plot(x, y)

谢谢你的指点!

票数 1
EN

Stack Overflow用户

发布于 2020-06-18 20:06:43

我相信其他人会有更好的建议,但是有一种方法可以得到你想要的东西(不需要额外的轴艺术家),那就是使用一个极坐标投影和一个散点图和条形图。有点像

代码语言:javascript
复制
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])

它会给

它缺少斧头标签和一些美化,但它可能是一个起点。我希望这会有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2020-06-18 20:28:57

您可以使用两个轴在另一个上面:

代码语言:javascript
复制
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,6))
ax1 = fig.add_axes([0.1,0.1,.8,.8], label="cartesian")
ax2 = fig.add_axes([0.1,0.1,.8,.8], projection="polar", label="polar")

ax2.set_rorigin(-1)
ax2.set_thetamax(90)
plt.show()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62457321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档