ArviZ 绘图参数详解:从变量筛选到可视化定制

ArviZ 绘图参数详解:从变量筛选到可视化定制

ArviZ 是一个专注于贝叶斯统计可视化的 Python 库,它提供了丰富的绘图功能来帮助用户分析和理解贝叶斯模型的结果。本文将深入解析 ArviZ 中各类绘图函数的通用参数,帮助用户掌握数据可视化的核心技巧。

变量筛选与处理

var_names 参数

var_names 是控制绘图变量的核心参数,支持多种灵活的变量选择方式:

  • 全变量绘图:默认行为,绘制所有可用变量
az.plot_posterior(centered_eight)
  • 单变量选择:通过字符串指定单个变量
az.plot_posterior(centered_eight, var_names='mu')
  • 多变量选择:使用列表指定多个变量
az.plot_posterior(centered_eight, var_names=['mu', 'tau'])
  • 排除变量:使用~前缀排除特定变量
az.plot_posterior(centered_eight, var_names=['~mu', '~theta'])
  • 非字符串变量:支持任何可哈希对象作为变量名
mu = ("mu", "var")
data = az.dict_to_dataset({mu: samples})
az.plot_posterior(data)

filter_vars 参数

当需要基于模式匹配选择变量时,filter_vars 提供了强大的筛选能力:

  1. 子串匹配模式 (filter_vars="like"):
az.plot_posterior(centered_eight, var_names='ta', filter_vars="like")
  1. 正则表达式模式 (filter_vars="regex"):
az.plot_posterior(centered_eight, var_names="u$", filter_vars="regex")

数据维度控制

coords 参数

coords 允许用户选择特定维度的坐标值进行绘图,非常适合处理高维数据:

coords = {"school": ["Choate", "Phillips Exeter"]}
az.plot_posterior(centered_eight, var_names=["mu", "theta"], coords=coords)

combine_dims 参数

对于需要降维处理的情况,combine_dims 可以将指定维度的数据合并:

# 合并学校维度,展示全局theta分布
az.plot_posterior(centered_eight, var_names=["theta"], combine_dims={"school"})
# 二维KDE绘图示例
az.plot_pair(
    non_centered_eight, 
    var_names=["theta", "theta_t"], 
    combine_dims={"school"}, 
    kind="kde"
)

绘图样式定制

图形布局控制

  1. 网格布局 (grid):
az.plot_density([centered_eight, non_centered_eight], grid=(4, 5))
  1. 图形尺寸 (figsize):
az.plot_posterior(centered_eight, var_names=["mu", "tau"], figsize=(3, 6))
  1. 文字大小 (textsize):
az.plot_posterior(centered_eight, var_names="theta", textsize=30)

颜色控制

  1. 单色设置 (color):
az.plot_hdi(x_data, y_data, color="red")
  1. 多色设置 (colors):
az.plot_density([centered_eight, non_centered_eight], colors=["salmon", "indigo"])

统计指标可视化

hdi_prob 参数

控制最高密度区间(HDI)的概率范围:

az.plot_posterior(centered_eight, var_names="mu", hdi_prob=0.8)

rope 参数

展示实用等价区域(ROPE),支持多种配置方式:

  1. 全局ROPE设置
az.plot_forest(non_centered_eight, var_names="~theta_t", rope=[-1, 2])
  1. 变量级ROPE设置
rope = {
    "mu": [{"rope": (4, 5)}],
    "theta": [{"rope": (0, 3)}]
}
az.plot_forest(non_centered_eight, rope=rope)

高级功能

reference_values 参数

plot_pair中标记参考值:

coords = {"school": ["Choate", "Deerfield"]}
reference_values = {
    "mu": 0.0,
    "theta": np.zeros(2),
}
az.plot_pair(centered_eight, var_names=["mu", "theta"], 
            coords=coords, reference_values=reference_values)

后端选择

ArviZ支持多种绘图后端:

# 使用Bokeh后端
az.rcParams["plot.backend"] = "bokeh"

通过掌握这些核心参数,用户可以充分利用 ArviZ 的强大可视化能力,创建专业级的贝叶斯分析图表。每个参数都经过精心设计,既保证了灵活性,又维持了使用的简洁性,是进行贝叶斯统计可视化不可或缺的工具。

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

井队湛Heath

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值