情节:如何为类别分配特定的颜色? [英] Plotly: How to assign specific colors for categories?
问题描述
我有一个发电数据的熊猫数据框.因此,它由不同燃料产生的电能组成.我想为特定的燃料分配特定的颜色.在Matplotlib中,通过传递颜色列表(例如
I have a pandas dataframe of electricity generation mix. So it consists of electricity generation by different fuels. I want to assign a specific color to a specific fuel. In Matplotlib, it is convenient to assign a specific color to a specific category by passing a list of colors, e.g.
df.plot(种类=条",颜色= [红色",绿色",黄色"]
df.plot(kind="bar",color=["red","green","yellow"]
我无法使用Plotly为绘图分配相似的颜色.在Plotly中将特定颜色分配给特定类别的最佳方法是什么?
I wasn't able to assign color similarly for plots using Plotly. What would be the best way to assign specific color to specific categories in Plotly?
推荐答案
来自有关颜色序列的文档以绘图方式表示,您应该能够使用参数 color_discrete_sequence
显式定义颜色序列.尝试运行:
From the documentation on color sequences in plotly express, you should be able to explicitly define a color sequence using the parameter color_discrete_sequence
. Try running:
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"])
这篇关于情节:如何为类别分配特定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!