如何修改Julia条形图内标签的大小、加粗及对齐属性?
调整Julia Plots条形图标签的大小、加粗与对齐方式
要实现标签的大小调整、加粗和对齐控制,不能直接用字符串数组传递给series_annotations,需要将每个标签转换为Plots.text()对象,在该对象内定义完整的文本样式。
修改后的完整代码
using Plots numbers = [2, 4, 2] colors_1 = ["white", "red", "white"] legend_text = "Graphic" letters = ["A", "B", "C"] # 生成带样式的标签数组:包含大小、颜色、加粗、对齐参数 annotations = [ Plots.text(letter, 20, :yellow, :bold, halign=:center, valign=:middle) for letter in letters ] Plots.bar(numbers, color = colors_1, background_color_legend = "white", foreground_color_axis = "white", foreground_color_border = "black", foreground_color_grid = "white", foreground_color_legend = "black", framestyle = :box, framevisible = false, label = legend_text, legendfontcolor = "yellow", legendfontsize = 20, linecolor = "white", markercolor = "yellow", markershape = :rect, markershapecolor = "yellow", markerstrokecolor = "yellow", markerstrokestyle = ":solid", markerstrokesize = 10, seriestype = :bar, series_annotations = annotations, # 替换为带样式的标签数组 size = (1000, 1000), textposition = "auto", xtickfont = "white", ytickfont = "white", width = [0, 0, 0])
关键样式参数说明
- 标签大小:
text()函数的第二个参数设置字号,示例中20代表20号字体,可根据需求修改数值 - 加粗效果:添加
:bold参数(或显式写fontweight=:bold)即可让文本加粗 - 对齐控制:
halign:控制水平对齐,可选值:center(居中)、:left(左对齐)、:right(右对齐)valign:控制垂直对齐,可选值:middle(居中)、:top(顶部对齐)、:bottom(底部对齐)
- 颜色设置:
text()的第三个参数指定文本颜色,示例中保留了你需要的黄色
内容的提问来源于stack exchange,提问作者Diego Rodrigues
相关产品推荐
相关产品推荐

