python - Plotly 表达条形图颜色变化

标签 python plotly

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

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()

最佳答案

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

Not sure when this functionality was added, for anyone coming here looking for solutions to grouped bar charts, what most people will want in this case is not color_discrete_sequence but instead color_discrete_map. For example:


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'
    }
)

关于python - Plotly 表达条形图颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60681437/

相关文章:

python - 在 matplotlib 中绘制 10 负幂的 xscale

R:具有小倍数/子图的绘图纵横比

python - 当有人单击自定义按钮时,我可以触发什么输入事件?

python - 如何将文件夹中的多个 CSV 文件合并到 Azure 上的单个文件?

r - 在R中的plotly饼图中使用自定义图标

javascript - plotly js去除标题和标题区域

r - 如何使用 plotly 在 R 中将每个带有数据子集的多条轨迹/趋势线添加到单个散点图

python - 对组应用操作而不聚合

python - 根据某些条件从模型中过滤数据

python - 如何查找要在其他文件中导入的类的完整模块路径