朱莉娅:如何更改散点图中组的颜色

Win*_*ore 3 uicolor julia plotly plotly.js

假设我有以下示例代码

using PlotlyJS
using CSV, DataFrames

df = dataset(DataFrame, "iris")
plot(
    df, x=:sepal_width, y=:sepal_length, color=:species,
    mode="markers"
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 我该如何指定每个组的颜色,例如,如果我希望 setosa 为黄色?

这正是Plotly-Express: How to fix the color mapping when set color by column name ,但我在 julia 中需要它。我无法让 color_discrete_map 工作...

sun*_*ica 5

不像设置 a 那样方便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)])


Run Code Online (Sandbox Code Playgroud)

这几乎就是下面的调用以更通用的方式plot(df, ..., color=:species)所做的事情。不幸的是,我没有找到一种方法来插入它并仅根据值自定义颜色。