【问题标题】:Julia:如何在散点图中更改组的颜色
【发布时间】:2022-01-23 11:14:43
【问题描述】:

假设我有以下示例代码

using PlotlyJS
using CSV, DataFrames

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

我该如何为每个组指定颜色,例如如果我想让 setosa 变成黄色?

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

【问题讨论】:

    标签: plotly julia uicolor plotly.js


    【解决方案1】:

    不像设置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) 调用对underneath 所做的,以更通用的方式。不幸的是,我没有办法插入它,只是根据值自定义颜色。

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多