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

Plots.jl绘图图例超出区域,如何解决或增大整体画布尺寸?

问题描述

使用Plots.jl绘制散点图时,设置图例位于outertopright后,图例超出了绘图区域。绘图代码及数据如下:

绘图代码

p = scatter(; title = "a", titlefont=font(12), 
            xlabel = "message size",
xscale = :log10, xlims, xticks, ylabel = "time [sec]",
ylims, yscale = :log10, legend=:outertopright, 
    left_margin=15Plots.mm, bottom_margin=15Plots.mm,
guidefont=font(8), xtickfont=font(5), 
   width=2000,height=2000, legend_margin=5, 
   legendfont=font(5))

scatter!( p, data1[:, 1],  data2[:, 5], 
       color=colors[index], ma=0.9   )

相关数据

# 修正了原数据中的空逗号问题
data1 = [0,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304]
data2=[3.08E-06,6.20E-06,6.26E-06,3.75E-05,4.36E-05,4.37E-05,4.52E-05,4.70E-05,5.58E-05,5.83E-05,6.67E-05,8.77E-05,0.000134397,0.000229329,0.000424682,0.000868432,0.00035778,0.000452593,0.000561347,0.000750234,0.001281027,0.002102504]
解决方案

针对图例溢出问题,有以下几种可行的解决方式:

1. 为外侧图例预留右侧边距

当前代码仅设置了左侧和底部边距,未给右上角的外侧图例留足空间。添加right_margin参数即可解决:

p = scatter(; title = "a", titlefont=font(12), 
            xlabel = "message size",
xscale = :log10, xlims, xticks, ylabel = "time [sec]",
ylims, yscale = :log10, legend=:outertopright, 
    left_margin=15Plots.mm, bottom_margin=15Plots.mm,
    right_margin=10Plots.mm, # 根据图例大小调整边距数值
guidefont=font(8), xtickfont=font(5), 
   width=2000,height=2000, legend_margin=5Plots.mm, # 给图例边距加上单位避免歧义
   legendfont=font(5))

2. 拆分图例为多列显示

如果图例条目较多,可通过legend_columns参数将图例拆分为多列,减少横向占用空间:

p = scatter(; title = "a", titlefont=font(12), 
            xlabel = "message size",
xscale = :log10, xlims, xticks, ylabel = "time [sec]",
ylims, yscale = :log10, legend=:outertopright, 
    left_margin=15Plots.mm, bottom_margin=15Plots.mm,
guidefont=font(8), xtickfont=font(5), 
   width=2000,height=2000, legend_margin=5Plots.mm, 
   legendfont=font(5),
   legend_columns=2) # 可根据图例条目数调整列数

3. 增大整体图形尺寸并适配

若需要更大的整体图形,可调整width和height数值,同时同步适配边距参数;导出图片时还可通过dpi控制分辨率:

p = scatter(; title = "a", titlefont=font(12), 
            xlabel = "message size",
xscale = :log10, xlims, xticks, ylabel = "time [sec]",
ylims, yscale = :log10, legend=:outertopright, 
    left_margin=15Plots.mm, bottom_margin=15Plots.mm,
    right_margin=10Plots.mm,
guidefont=font(8), xtickfont=font(5), 
   width=2500,height=2500, # 增大整体尺寸
   dpi=300, # 提高导出分辨率
   legend_margin=5Plots.mm, 
   legendfont=font(5))

4. 将图例移至绘图区域内侧(备选)

若无需外侧图例,可将legend=:outertopright改为legend=:topright,让图例位于绘图区域内侧,避免溢出:

p = scatter(; title = "a", titlefont=font(12), 
            xlabel = "message size",
xscale = :log10, xlims, xticks, ylabel = "time [sec]",
ylims, yscale = :log10, legend=:topright, # 改为内侧右上角
    left_margin=15Plots.mm, bottom_margin=15Plots.mm,
guidefont=font(8), xtickfont=font(5), 
   width=2000,height=2000, legend_margin=5Plots.mm, 
   legendfont=font(5))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 21:11:29