英文:
How to have a customized legend in the x axis when using groupedbar by StatsPlots
问题
如前所述,我想使用Julia中的`StatsPlots`绘制一个`groupedbar`。以下是我的代码:
legends = repeat(["myopic baseline", "vfa", "lvfa", "nnvfa", "nnvfa with attention"], outer=2)
training_time_per_step = [0., 9.817792895793914, 6.380393509864807, 15.770121607780457, 31.79437662124634]
eval_time_per_step = [2.756498346328735, 6.823106627861659, 6.184429474310442, 13.286595929535952, 20.297245191227304]
ctg = repeat(["training time per step", "eval time per step"], inner = 5)
bar1 = groupedbar(legends, vcat(training_time_per_step, eval_time_per_step), group=ctg, xlabel="Methods", ylabel="Time",
title="Time Evaluation", legend=:topleft, reuse=false)
display(bar1)
我希望图例的顺序与我定义的顺序相同,但我发现它会自动按每个图例的名称排序。
英文:
As mentioned, I wanted to plot a groupedbar
using StatsPlots
in Julia. Here is my code:
legends = repeat(["myopic baseline", "vfa", "lvfa", "nnvfa", "nnvfa with attention"], outer=2)
training_time_per_step = [0., 9.817792895793914, 6.380393509864807, 15.770121607780457, 31.79437662124634]
eval_time_per_step = [2.756498346328735, 6.823106627861659, 6.184429474310442, 13.286595929535952, 20.297245191227304]
ctg = repeat(["training time per step", "eval time per step"], inner = 5)
bar1 = groupedbar(legends, vcat(training_time_per_step, eval_time_per_step), group=ctg, xlabel="Methods", ylabel="Time",
title="Time Evaluation", legend=:topleft, reuse=false)
display(bar1)
I want the legends to have the same order as I defined, but I find it is automatically ordered by the name of each legend:
How can I solve this problem?
答案1
得分: 1
你需要将`ctg`设置为`CategoricalArray`而不是字符串向量。
Categorical Arrays保持类的顺序,并得到StatsPlots支持。
使用 CategoricalArrays
levlabels = ["每步训练时间", "每步评估时间"]
ctg = CategoricalArray(repeat(levlabels, inner = 5))
levels!(ctg, levlabels)
现在你的标签将不会按字母顺序自动排序 :)
(对于你实际使用的绘图命令没有更改,所以我不在这里复制粘贴它)。
英文:
You need your ctg
to be a CategoricalArray
rather than a Vector of Strings.
Categorical Arrays are maintaining the order of classes and are supported by StatsPlots.
using CategoricalArrays
levlabels = ["training time per step", "eval time per step"]
ctg=CategoricalArray(repeat(levlabels, inner = 5))
levels!(ctg, levlabels)
Now your labels will not get auto-sorted alphabetically
(no change to the actual plotting command that you used so I do not copy paste it here).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论