python - 如何在 python 中格式化角图?

标签 python plot mcmc

我有一些像 emcee 这样的包,它为我的模型拟合运行 mcmc 算法。获得后采样链后,我使用包 corner 生成 corner 图。我的一些参数是非常大的数字,而另一些是小数字。正因为如此,剧情看起来很诡异(见附图)。 enter image description here

有没有办法用科学计数法显示大数而用 float 显示小数? x轴刻度标签已经有了这个。我希望适合的结果标题也一样。

最佳答案

使用title_fmt

您可以使用 kwarg title_fmt 格式化标题:

title_fmt : string

    The format string for the quantiles given in titles. If you explicitly
    set ``show_titles=True`` and ``title_fmt=None``, the labels will be
    shown as the titles. (default: ``.2f``)

在您的情况下,您可以设置 title_fmt=".2E"

例子

在指定title_fmt之前:

before

指定title_fmt=".2E"后:

after

代码示例

# -*- coding: utf-8 -*-
"""Corner on top of each other"""
import corner
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
import numpy as np

CORNER_KWARGS = dict(
    smooth=0.9,
    label_kwargs=dict(fontsize=16),
    title_kwargs=dict(fontsize=16),
    quantiles=[0.16, 0.84],
    levels=(1 - np.exp(-0.5), 1 - np.exp(-2), 1 - np.exp(-9 / 2.)),
    plot_density=False,
    plot_datapoints=False,
    fill_contours=True,
    show_titles=True,
    max_n_ticks=3,
    title_fmt=".2E"
)


def overlaid_corner(samples_list, sample_labels):
    """Plots multiple corners on top of each other"""
    # get some constants
    n = len(samples_list)
    _, ndim = samples_list[0].shape
    max_len = max([len(s) for s in samples_list])
    cmap = plt.cm.get_cmap('gist_rainbow', n)
    colors = [cmap(i) for i in range(n)]

    plot_range = []
    for dim in range(ndim):
        plot_range.append(
            [
                min([min(samples_list[i].T[dim]) for i in range(n)]),
                max([max(samples_list[i].T[dim]) for i in range(n)]),
            ]
        )

    CORNER_KWARGS.update(range=plot_range)

    fig = corner.corner(
        samples_list[0],
        color=colors[0],
        **CORNER_KWARGS
    )

    for idx in range(1, n):
        fig = corner.corner(
            samples_list[idx],
            fig=fig,
            weights=get_normalisation_weight(len(samples_list[idx]), max_len),
            color=colors[idx],
            **CORNER_KWARGS
        )

    plt.legend(
        handles=[
            mlines.Line2D([], [], color=colors[i], label=sample_labels[i])
            for i in range(n)
        ],
        fontsize=20, frameon=False,
        bbox_to_anchor=(1, ndim), loc="upper right"
    )
    plt.savefig("orig_corner.png")
    plt.close()


def get_normalisation_weight(len_current_samples, len_of_longest_samples):
    return np.ones(len_current_samples) * (len_of_longest_samples / len_current_samples)


def main():
    ndim, nsamples = 3, 10000
    np.random.seed(2000)
    samples = np.random.randn(ndim * nsamples).reshape([nsamples, ndim]) * 1e9

    overlaid_corner(
        [samples * 3, samples * 2, samples],
        ["samples x 3", "samples x 2", "samples"]
    )


if __name__ == "__main__":
    main()


关于python - 如何在 python 中格式化角图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590009/

相关文章:

python - 使用 PYMC3 的跟踪图跟踪各个维度?

python - 定义和绘制 Schechter 函数 : plot problems

python - SQLAlchemy 中的多对多查询

r - 针对特定条件在 R 中格式化 X Axis 标签

r - 绘制 NetCDF 的部分并覆盖形状文件 - R

python - 从 MCMC 采样器输出中找到最可能的值

python - 如何找出哪些字符被定义为给定语言环境的字母数字

python - wxPython - Linux 和 Windows 之间的 TextCtrl 不同行为

R 使用值列表作为色标

r - 并行和串行处理的系统时间