Plotly:如何使用 go.Bar 为组指定颜色? [英] Plotly: How to specify colors for a group using go.Bar?

查看:172
本文介绍了Plotly:如何使用 go.Bar 为组指定颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 plotly.graph_objs 以类似于 plotly.express 的方式绘制 Pandas 数据 - 特别是为各种数据类型着色?

根据 Pandas 列中的值对数据类型进行分组的 plotly express 功能非常有用.不幸的是,我无法在我的系统中使用 express(因为我需要将图形对象发送到

# 导入将熊猫导入为 pd导入 plotly.express 作为 px导入 plotly.graph_objs as go# 数据d = {'场景': [1, 2, 3, 1, 2,3],'类型':[A",A",A",B",B",B"],'VAL_1': [100, 200, 300, 400, 500, 600],'VAL_2':[1000、2000、3000、4000、5000、6000]}df = pd.DataFrame(data=d)# 使用字典为类型指定颜色颜色 = {'A':'steelblue','B':'耐火砖'}# 情节图fig=go.Figure()对于 df['Type'].unique() 中的 t:dfp = df[df['类型']==t]fig.add_traces(go.Bar(x=dfp['Scenario'], y = dfp['VAL_1'], name=t,标记颜色=颜色[t]))图.show()

How to use plotly.graph_objs to plot pandas data in a similar way to plotly.express - specifically to color various data types?

The plotly express functionality to group data types based on a value in a pandas column is really useful. Unfortunately I can't use express in my system (as I need to send the graph object to orca)

I'm able to get the same functionality by specifically mapping Type to colours (full_plot in the example below), however I have types A-Z, is there a better way of mapping each possible Type in the dataframe to a color?

import pandas as pd
import plotly.express as px
import plotly.graph_objs as go

d = {'Scenario': [1, 2, 3, 1, 2,3],
     'Type': ["A", "A", "A", "B", "B", "B"],
     'VAL_1': [100, 200, 300, 400 , 500, 600],
     'VAL_2': [1000, 2000, 3000, 4000, 5000, 6000]}
df = pd.DataFrame(data=d)


def quick_plot(df):

    fig = px.bar(df, y='VAL_1',  x='Scenario',  color="Type", barmode='group')
    fig['layout'].update(title = "PX Plot",
                     width = 600, height = 400,
                     xaxis = dict(showgrid=False))
    fig.show()


def full_plot(df):

    colors = {'A': 'blue',
          'B': 'red'}
    s0=df.query('Type=="A"')
    s1=df.query('Type=="B"')

    fig = go.Figure()
    fig.add_trace(go.Bar(
        name='A',
         y=s0['VAL_1'],x=s0['Scenario'], marker={'color': colors['A']}))
    fig.add_trace(go.Bar(
        name='B',
         y=s1['VAL_1'],x=s1['Scenario'], marker={'color': colors['B']}))

    fig['layout'].update(title = "Full Plot",
                     width = 600, height = 400)

    fig.update_layout(barmode='group')
    fig.show()

quick_plot(df)
full_plot(df)

解决方案

You could simply use a dictionary like this:

colors = {'A':'steelblue',
          'B':'firebrick'}

The only challenge lies in grouping the dataframe for each unique type and adding a new trace for each type using a for loop. The code snippet below takes care of that to produce this plot:

# imports
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go

# data
d = {'Scenario': [1, 2, 3, 1, 2,3],
     'Type': ["A", "A", "A", "B", "B", "B"],
     'VAL_1': [100, 200, 300, 400 , 500, 600],
     'VAL_2': [1000, 2000, 3000, 4000, 5000, 6000]}
df = pd.DataFrame(data=d)

# assign colors to type using a dictionary
colors = {'A':'steelblue',
          'B':'firebrick'}

# plotly figure
fig=go.Figure()
for t in df['Type'].unique():
    dfp = df[df['Type']==t]
    fig.add_traces(go.Bar(x=dfp['Scenario'], y = dfp['VAL_1'], name=t,
                         marker_color=colors[t]))

fig.show()

这篇关于Plotly:如何使用 go.Bar 为组指定颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆