如何在 Plotly 散点图中更改组的颜色

3

假设我有以下示例代码:

using PlotlyJS
using CSV, DataFrames

df = dataset(DataFrame, "iris")
plot(
    df, x=:sepal_width, y=:sepal_length, color=:species,
    mode="markers"
)

enter image description here 如何指定每个分组的颜色,例如,如果我想将setosa设为黄色呢?

这正好是Plotly-Express:在设置按列名着色时如何修复颜色映射的问题,但我需要用julia实现。 我无法使color_discrete_map起作用...

1个回答

5

虽然不如设置color_discrete_map方便,但你可以像这样做:

julia> species_color_map = Dict("setosa"     => "yellow",
                                "versicolor" => "aqua",
                                "virginica"  => "red")
Dict{String, String} with 3 entries:
  "virginica"  => "red"
  "setosa"     => "yellow"
  "versicolor" => "aqua"

julia> plot([scatter(
           subdf,
           x = :sepal_width,
           y = :sepal_length,
           name = subdf[1, :species],
           marker_color = species_color_map[ subdf[1, :species] ],
           mode = "markers"
       )
       for subdf in groupby(df, :species)])



这基本上是 plot(df, ..., color=:species) 的调用在更通用的方式下实现的,链接中有详细说明。不幸的是,我没有找到一种方法来插入和根据值自定义颜色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接