如何在使用StatsPlots的GroupedBar时在X轴上添加自定义图例

huangapple go评论57阅读模式
英文:

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:
如何在使用StatsPlots的GroupedBar时在X轴上添加自定义图例

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 如何在使用StatsPlots的GroupedBar时在X轴上添加自定义图例
(no change to the actual plotting command that you used so I do not copy paste it here).

如何在使用StatsPlots的GroupedBar时在X轴上添加自定义图例

huangapple
  • 本文由 发表于 2023年4月6日 20:11:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定