朱莉娅:如何在 plotly 散点图中更改组的颜色
Julia: How to change color of a group in plotly scatterplot
假设我有以下示例代码
using PlotlyJS
using CSV, DataFrames
df = dataset(DataFrame, "iris")
plot(
df, x=:sepal_width, y=:sepal_length, color=:species,
mode="markers"
)
我怎样才能为每个组指定颜色 例如如果我想让 setosa 变成黄色?
正是这个 但我需要它在 julia。我无法让 color_discrete_map 工作...
不如设置一个 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 以更通用的方式所做的事情。不幸的是,我看不到插入它的方法,只能根据值自定义颜色。
假设我有以下示例代码
using PlotlyJS
using CSV, DataFrames
df = dataset(DataFrame, "iris")
plot(
df, x=:sepal_width, y=:sepal_length, color=:species,
mode="markers"
)
正是这个
不如设置一个 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 以更通用的方式所做的事情。不幸的是,我看不到插入它的方法,只能根据值自定义颜色。