如何在 plotly express 中将离散变量映射到连续色标
How to map a discrete variable to a continuous color scale in plotly express
我希望为分类变量定义一个顺序,并为每条线的颜色使用连续色标。以下是我当前产生错误的代码。谁能帮我实现这个,或者这是不可能的?
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'dt':['2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01'],
'group':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'val':[3, 5, 3, 2, 1, 5, 6, 7, 3]})
df['group'] = pd.Categorical(df['group'], ordered=True, categories=['a', 'b', 'c'])
px.line(df,
x='dt',
y='val',
color='group',
color_continuous_scale=px.colors.sequential.Blues)
当前错误消息:TypeError:line() 得到了意外的关键字参数 'color_continuous_scale'
如果你读the documentation,没有color_continuous_scale
,但是有color_discrete_sequence
。
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'dt':['2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01'],
'group':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'val':[3, 5, 3, 2, 1, 5, 6, 7, 3]})
df['group'] = pd.Categorical(df['group'], ordered=True, categories=['a', 'b', 'c'])
px.line(df,
x='dt',
y='val',
color='group',
color_discrete_sequence=px.colors.sequential.Blues)
我希望为分类变量定义一个顺序,并为每条线的颜色使用连续色标。以下是我当前产生错误的代码。谁能帮我实现这个,或者这是不可能的?
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'dt':['2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01'],
'group':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'val':[3, 5, 3, 2, 1, 5, 6, 7, 3]})
df['group'] = pd.Categorical(df['group'], ordered=True, categories=['a', 'b', 'c'])
px.line(df,
x='dt',
y='val',
color='group',
color_continuous_scale=px.colors.sequential.Blues)
当前错误消息:TypeError:line() 得到了意外的关键字参数 'color_continuous_scale'
如果你读the documentation,没有color_continuous_scale
,但是有color_discrete_sequence
。
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'dt':['2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-01-01', '2021-02-01', '2021-03-01'],
'group':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'val':[3, 5, 3, 2, 1, 5, 6, 7, 3]})
df['group'] = pd.Categorical(df['group'], ordered=True, categories=['a', 'b', 'c'])
px.line(df,
x='dt',
y='val',
color='group',
color_discrete_sequence=px.colors.sequential.Blues)