我有一些像 emcee
这样的包,它为我的模型拟合运行 mcmc
算法。获得后采样链后,我使用包 corner
生成 corner
图。我的一些参数是非常大的数字,而另一些是小数字。正因为如此,剧情看起来很诡异(见附图)。
有没有办法用科学计数法显示大数而用 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
之前:
指定title_fmt=".2E"
后:
代码示例
# -*- 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/