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

Julia Plots中是否有等效于Python PyPlot的tight_layout()方法?

Julia Plots中存在和matplotlib tight_layout()等效的功能,以下是具体解决方法:

方案1:启用内置自动紧凑布局

直接在绘图参数中添加tight_layout = true即可,大多数常规场景下可以自动调整画布边距,避免标签、标注溢出显示区域。

方案2:手动调整边距(适配标注溢出特殊场景)

你当前的场景属于柱状图右侧有额外文字标注,自动布局可能还是预留空间不足,可以额外搭配边距参数手动加宽对应方向的画布边距,需要提前导入Plots.Measures调用长度单位。

修改后的可运行代码如下:

using Plots, Printf
using Plots.Measures

d = Dict(
  "C"                   => 10.7406,
  "C ffast-math"        => 7.28659,
  "Python- Built-in"    => 1631.21,
  "Python numpy"        => 4.74571,
  "Python hand-written" => 1942.86,
  "Julia built-in"      => 6.45389,
  "Julia hand-written"  => 10.7421
)

l_vec = sort(collect(d), by=x->x[2], rev = true)

x = map(x->x[1], l_vec)
y = map(x->x[2], l_vec)

y_annotations = [@sprintf(" %.2f ms",i) for i in y]  

Plots.bar(x ,y ,
    xaxis = ("time (ms)",false, nothing), 
    yaxis = ("Languages",false),
    orientation= :horizontal,
    label = "" , 
    series_annotation = (y_annotations, font(7, :left)),
    tight_layout = true,  # 启用自动紧凑布局
    right_margin = 15mm   # 额外加宽右侧边距容纳标注
)

如果你的标注长度有变化,自行调整right_margin的数值即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 17:15:03