绘制快递条形图颜色变化

时间:2020-03-14 09:41:27

标签: python plotly

如何在以下代码中将绘图表达的条形图的颜色更改为绿色?

import plotly.express as px
import pandas as pd

# prepare the dataframe
df = pd.DataFrame(dict(
        x=[1, 2, 3],
        y=[1, 3, 2]
    ))


# prepare the layout
title = "A Bar Chart from Plotly Express"

fig = px.bar(df, 
             x='x', y='y', # data from df columns
             color= pd.Series('green', index=range(len(df))), # does not work
             title=title,
             labels={'x': 'Some X', 'y':'Some Y'})
fig.show()

2 个答案:

答案 0 :(得分:4)

从字面上复制@reservoirinvest 发布的社区回复中的答案。这就是我要找的...

<块引用>

不确定何时添加此功能,对于来这里寻找分组条形图解决方案的任何人来说,在这种情况下,大多数人想要的不是 color_discrete_sequence 而是 color_discrete_map。例如:

fig = px.bar(
    df, 
    x='x', 
    y='y', 
    color='z',   # if values in column z = 'some_group' and 'some_other_group'
    color_discrete_map={
        'some_group': 'red',
        'some_other_group': 'green'
    }
)

答案 1 :(得分:2)

可以做到: -使用color_discrete_sequence =['green']*3,或 -在实例化条之后使用fig.update_traces(marker_color='green')

参考:Community response