如果你想要为特定的燃料指定特定的颜色,那么只要你的数据集结构不变,color_discrete_sequence
就可以很好地工作。但是指定每个类别的颜色最好通过以下方式实现:
color_discrete_map = <dict>
而在您的情况下:
fig = px.bar(df, color_discrete_map= {'Coal': 'black',
'Oil': 'grey',
'Gas': 'blue'}
)
这样做可以避免依赖变量的顺序来匹配所需颜色的顺序。
import plotly.express as px
import pandas as pd
df = px.data.stocks().set_index('date')
fig = px.bar(df, color_discrete_map= {'GOOG': 'black',
'AAPL': 'grey',
'AMZN': 'blue',
'FB': 'green',
'NFLX': 'red',
'MSFT':'firebrick'}
)
fig.show()
如需其他选项,请查看Plotly:如何使用plotly.graph_objects和plotly.express在图中定义颜色? 在那你将发现如何将颜色序列更改为您想要的任何内容,并通过color_discrete_map
指定一些例外情况。
从plotly express的颜色序列文档中,您应该能够使用参数color_discrete_sequence
来明确定义颜色序列。尝试运行:
fig=px.bar(df1, title="Electricity generation mix of Germany in TwH (2000-2019)", color_discrete_sequence=["black","grey","lightgrey","red","rosybrown","blue","limegreen","yellow","forestgreen"])