You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CairoMakie中为密度图设置坐标轴范围?

如何在CairoMakie密度图中固定坐标轴范围

要固定CairoMakie密度图的x轴和y轴范围,有两种直接的实现方式:

方法一:先获取轴对象再调整范围

调用density时拆解返回的FigureAxisPlot对象,拿到轴实例后直接修改坐标轴范围:

# 获取图、轴和绘图对象
fig, ax, plt = CairoMakie.density(ages_obtention, 
    color = (:blue, 0.3),
    strokecolor = :blue, 
    strokewidth = 2, 
    strokearound = true
)

# 固定x轴范围为10到100
ax.xlims = (10, 100)
# 设置y轴从0开始,上限自动适配数据(消除x轴上方间隙)
ax.ylims = (0, nothing)

方法二:绘图时直接通过axis参数指定范围

在调用density函数时,通过axis关键字参数一次性设置坐标轴属性:

fig = CairoMakie.density(ages_obtention, 
    color = (:blue, 0.3),
    strokecolor = :blue, 
    strokewidth = 2, 
    strokearound = true,
    axis = (xlims=(10, 100), ylims=(0, nothing))
)

说明:ylims=(0, nothing)中的nothing表示保留系统自动计算的y轴上限,这样既保证y轴从0开始消除间隙,又不会截断密度曲线的顶部。

内容的提问来源于stack exchange,提问作者JeanJouX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:18:12