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

Julia使用StatsPlots绘制多子图分组柱状图如何设置底部美观图例

德国分月公共净发电量堆叠柱状图图例统一方案

原有代码问题排查

首先修复代码里的三个显性问题:

  • 4月子图a4错误引用了3月数据集df3,会导致4月数据与3月完全重复
  • a4的绘图参数中多写了一个冗余逗号,会直接触发语法报错
  • 仅在最后一个子图传入标签的写法,会导致图例与单个子图绑定,无法实现全局统一排版

具体实现代码

核心调整逻辑:

  1. 所有子图统一关闭单独标签,避免重复生成图例项
  2. 全局统一配色方案,保证四个子图中同一类发电能源颜色完全一致
  3. 图例设置在图表外底部,通过legendcolumns参数实现多列环形排布
  4. 预留底部边距,避免图例被裁切
using CSV, DataFrames, StatsPlots, Measures, Plots
gr()

# 数据读取
df1 = CSV.read("/home/browni/LasthrimProjection/csv/energy-charts_Public_net_electricity_generation_in_Germany_in_January_Excel.csv", DataFrame)
df2 = CSV.read("/home/browni/LasthrimProjection/csv/energy-charts_Public_net_electricity_generation_in_Germany_in_February_Excel.csv", DataFrame)
df3 = CSV.read("/home/browni/LasthrimProjection/csv/energy-charts_Public_net_electricity_generation_in_Germany_in_March_Excel.csv", DataFrame)
df4 = CSV.read("/home/browni/LasthrimProjection/csv/energy-charts_Public_net_electricity_generation_in_Germany_in_April_Excel.csv", DataFrame)

# 日期格式统一处理
df1.Month = join.(split.(rpad.(string.(df1.Month),4,"0"),'.'), '-')
df2.Month = join.(split.(rpad.(string.(df2.Month),4,"0"),'.'), '-')
df3.Month = join.(split.(rpad.(string.(df3.Month),4,"0"),'.'), '-')
df4.Month = join.(split.(rpad.(string.(df4.Month),4,"0"),'.'), '-')

# 统一提取能源类型标签、配色
energy_types = names(df1)[2:end]
plot_palette = palette(:tab20, length(energy_types))

# 绘制子图,全部关闭单独标签,统一配色
a1 = groupedbar(df1.Month, Matrix(df1[:,2:end]), title="January", xlabel="Year", ylabel="Energy(GWh)", 
    titlefontsize=12, bar_position=:stack, bar_width=0.6, label="", palette=plot_palette)
a2 = groupedbar(df2.Month, Matrix(df2[:,2:end]), title="February", xlabel="Year", ylabel="Energy(GWh)", 
    titlefontsize=12, bar_position=:stack, bar_width=0.6, label="", palette=plot_palette)
a3 = groupedbar(df3.Month, Matrix(df3[:,2:end]), title="March", xlabel="Year", ylabel="Energy(GWh)", 
    titlefontsize=12, bar_position=:stack, bar_width=0.6, label="", palette=plot_palette)
# 修复原代码df3引用错误、多余逗号问题
a4 = groupedbar(df4.Month, Matrix(df4[:,2:end]), title="April", xlabel="Year", ylabel="Energy(GWh)", 
    titlefontsize=12, bar_position=:stack, bar_width=0.6, label="", palette=plot_palette)

# 组合子图,预留底部图例空间
p = plot(a1, a2, a3, a4, 
    layout = @layout(grid(2,2)),
    plot_title = "German Public Net Electricity Generation 2015-2021",
    plot_titlefontsize = 14,
    margin = 5mm,
    bottommargin = 18mm # 给底部图例预留足够显示空间
)

# 全局样式调整,统一设置底部环形图例
plot!(p,
    tickfontsize=8, 
    framestyle = :box, 
    guidefontsize=9,
    legendfontsize=7,
    legend=:outerbottom,
    legendcolumns=6, # 调整列数实现环形排版,数值越大单行列数越多
    label=permutedims(energy_types)
)

参数调整说明

  • 如果觉得图例排布过密/过疏,可以修改legendcolumns的数值,能源类型共十几种时设为5-7都能得到比较美观的环形横排效果
  • 如果需要调整配色,可以替换palette(:tab20)为其他Plots支持的色板,比如:seaborn_colorblind适合色盲友好场景
  • 边距数值bottommargin可以根据最终显示效果微调,保证图例完全显示不被裁切

内容的提问来源于stack exchange,提问作者Freya the Goddess

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 14:24:20