有许多问题和答案以一种或另一种方式涉及这个主题。有了这个贡献我想清楚地说明为什么一个简单的计算策略,如marker = {'color' : 'red'}对工作的意愿plotly.graph_objects (go),但color='red'不会为了plotly.express (px)虽然颜色是两者的属性px.Line和px.Scatter。而且我想说明为什么它的真棒,它没有。
所以,如果px应该是制作情节图的最简单方法,那么为什么像color='red'返回错误这样明显的东西
ValueError: 'color' 的值不是 'data_frame' 中列的名称。
让我applyig一个展示gapminder数据集和显示的散点图Life expectancy对GDP per capita所有(至少大多数世界各地的)国家作为2007。像下面这样的基本设置将产生以下图
go:颜色由名为plotly的循环设置,但在此处指定使用 marker = {'color' : 'red'}
import plotly.graph_objects as go
df = px.data.gapminder()
df=df.query("year==2007")
fig = go.Figure()
fig.add_traces(go.Scatter(x=df['gdpPercap'], y=df["lifeExp"],
mode = 'markers',
marker = …Run Code Online (Sandbox Code Playgroud) 我认为默认颜色循环将是(某些变体)['blue', 'red', 'green', 'purple', 'orange']如下图所示:
情节 1:
代码 1:
import plotly
import plotly.express as px
gapminder = px.data.gapminder()
gapminder2007=gapminder.query("year==2007")
px.scatter(gapminder2007, x='gdpPercap', y='lifeExp', color="continent")
Run Code Online (Sandbox Code Playgroud)
至少这似乎是循环的顺序,因为Oceania从大陆中省略会给你这个:
现在其余颜色的顺序是相同的,除了最后一个橙色的外观(只是表明颜色的应用不是随意的)。
我认为这可以使用plotly.colors.DEFAULT_PLOTLY_COLORS它来检索,这会给你:
['rgb(31, 119, 180)',
'rgb(255, 127, 14)',
'rgb(44, 160, 44)',
'rgb(214, 39, 40)',
'rgb(148, 103, 189)',
'rgb(140, 86, 75)',
'rgb(227, 119, 194)',
'rgb(127, 127, 127)',
'rgb(188, 189, 34)',
'rgb(23, 190, 207)']
Run Code Online (Sandbox Code Playgroud)
但这些颜色的顺序是 ['blue', 'orange', 'green', 'red']...
那么,当设置为in时, plotly 表达遵循什么颜色循环?color="continent"px.scatter(gapminder2007, x='gdpPercap', y='lifeExp', color="continent")
我也试着往下看help(px.colors) …
假设我有以下示例代码
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 工作...