简体   繁体   English

python plotly 图形对象 - 类似于 plotly 中的离散颜色映射

[英]python plotly graph objects - discrete_color_map like in plotly express

i am looking for a solution to use a dict like:我正在寻找一种使用 dict 的解决方案,例如:

colors={
    "Oxygen": "#bf230f",
    "Hydrogen": "#19848c",
    "Carbon_Dioxide": "#d94f3d",
    "Nitrogene": "#8c2d20"}

instead of the list colors in the following example而不是以下示例中的列表 colors

import plotly.graph_objects as go
colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen']

fig = go.Figure(data=[go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'],
                             values=[4500,2500,1053,500])])
fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20,
                  marker=dict(colors=colors, line=dict(color='#000000', width=2)))
fig.show()

in plotly express this is possible with "color_discrete_map=", but i have to use graph.objects.在 plotly 中,用“color_discrete_map =”表示这是可能的,但我必须使用 graph.objects。 thanks for help!感谢帮助!

You could convert the dictionary into a pandas series and use the series:您可以将字典转换为 pandas 系列并使用该系列:

import plotly.graph_objects as go
import pandas as pd

colors = {
    "Oxygen": "#bf230f",
    "Hydrogen": "#19848c",
    "Carbon_Dioxide": "#d94f3d",
    "Nitrogene": "#8c2d20"
}

s = pd.Series(colors)

fig = go.Figure(data=[go.Pie(labels=s.index, values=[4500, 2500, 1053, 500])])
fig.update_traces(hoverinfo='label+percent',
                  textinfo='value',
                  textfont_size=20,
                  marker=dict(colors=s, line=dict(color='#000000', width=2)))
fig.show()

Of course, then it is sensible to include the values in a pandas structure as well:当然,将值包含在 pandas 结构中也是明智的:

import plotly.graph_objects as go
import pandas as pd

colors = {
    "Oxygen": "#bf230f",
    "Hydrogen": "#19848c",
    "Carbon_Dioxide": "#d94f3d",
    "Nitrogene": "#8c2d20"
}

df = pd.DataFrame([colors], index=["colors"]).T
df["values"] = [4500, 2500, 1053, 500]

fig = go.Figure(data=[go.Pie(labels=df.index, values=df["values"])])
fig.update_traces(hoverinfo='label+percent',
                  textinfo='value',
                  textfont_size=20,
                  marker=dict(colors=df["colors"],
                              line=dict(color='#000000', width=2)))
fig.show()

In either case, the result is as follows:无论哪种情况,结果如下:

饼形图

based on bb1 post, the solution could be to merge the column value with the color already in the dataframe.基于 bb1 帖子,解决方案可能是将列值与 dataframe 中已有的颜色合并。

import plotly.express as px
import pandas as pd
colors = {
    "Oxygen": "#bf230f",
    "Hydrogen": '##8c2d20',
    "Carbon_Dioxide": "#d94f3d",
    "Nitrogene": "#8c2d20"
}

df = pd.DataFrame()
df["values"] = [4500, 2500, 1053, 500]
df['Kategorie_B'] = ['Oxygen', 'Hydrogen', 'Carbon_Dioxide', 'Nitrogene']

df["Color"] = df["Kategorie_B"].apply(lambda x: colors.get(x)) #to connect Column value to Color in Dict

fig = go.Figure(data=[go.Pie(labels=df.index, values=df["values"])])
fig.update_traces(hoverinfo='label+percent',
                  textinfo='value',
                  textfont_size=20,
                  marker=dict(colors=df["colors"],
                              line=dict(color='#000000', width=2)))
plotly.io.write_image(fig, file='pie.png', format='png')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM