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

Julia Plots.jl叠加plot()到heatmap()时出现多余空白问题

解决Julia Plots绘图区与坐标轴间多余空白的问题

问题成因

Plots库默认会为绘图区域添加边距,且叠加plot!时可能自动微调轴范围以适配所有元素,导致绘图区和坐标轴之间出现空白。

解决方案

以下几种方法可解决该问题:

  • 手动锁定轴范围并清除边距
    强制将轴范围固定为热力图的数据集范围(1到100),同时清除所有边距:

    using Plots
    heatmap(rand(100,100))
    plot!(1:100,1:100,color=:white,lw=2,legend=false)
    xlims!(1, 100)
    ylims!(1, 100)
    margin!(0, 0, 0, 0) # 顺序:左、右、上、下边距
    
  • 使用自动布局调整函数
    调用tight_layout!()让Plots自动优化布局,消除多余空白:

    using Plots
    heatmap(rand(100,100))
    plot!(1:100,1:100,color=:white,lw=2,legend=false)
    tight_layout!()
    
  • 绘图时直接设置样式参数
    在heatmap中指定framestyle=:box并关闭边距,从源头避免空白:

    using Plots
    heatmap(rand(100,100), framestyle=:box, margin=0)
    plot!(1:100,1:100,color=:white,lw=2,legend=false)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 18:22:06