如何在以下代码中将 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/