PlotlyJS question: How to change color of stacked bar

Hi all, I am using PlotlyJS.jl and trying to change color of stacked bar chart.

p_turnover_s = plot( turnover_size_df, x=:year, y=:mean_turnover, color=:size, text=:mean_turnover, kind="bar", textposition="auto", labels=Dict( :year => "Năm", :mean_turnover => "Hiệu suất vòng quay vốn (Lần)", :size => "Quy mô", ), Layout(barmode="stack") )


I need to change the color of each size: “Quy mô lớn” to color #FFC000, “Quy mô nhỏ” to color #2E5894, “Quy mô siêu nhỏ” to color #4682B4, and “Quy mô vừa” to #B4A9A0.
Some help would be very much appreciated. Thank you.

Welcome to the forum.

To make it easier to reproduce the answer, could you post the data that makes the dataframe turnover_size_df in your example?

1 Like

Here is an example:


using PlotlyJS, DataFrames

df = DataFrame([
  (String15("South Korea"), String7("gold"), 24)
(String15("China"), String7("gold"), 10)
(String15("Japan"), String7("gold"), 9)
(String15("South Korea"), String7("silver"), 13)
(String15("China"), String7("silver"), 15)
(String15("Japan"), String7("silver"), 12)
(String15("South Korea"), String7("bronze"), 11)
(String15("China"), String7("bronze"), 8)
(String15("Japan"), String7("bronze"), 12)])
rename!(df, [:nation, :medal, :count])

colour_dict = Dict("bronze" => "#be8557", "silver" => "#b4b4b4", "gold" => "#ffa500")

fig = Plot(df, kind="bar", x=:nation, y=:count, color=:medal, 
              Layout(barmode="stack") )

for trace in fig.data
  if trace[:name] in keys(colour_dict)
    trace[:marker][:color] = colour_dict[trace[:name]]
  end
end
relayout!(fig)
display(fig)