如何在Plotly中构建按值排序的多值列?

huangapple go评论62阅读模式
英文:

How to build columns in Plotly with multiple values sorted by value?

问题

import pandas as pd
import numpy as np

data = [('2022-10-01','Pay1',644), ('2022-10-01','Pay2',1460), ('2022-10-01','Pay3',1221), ('2022-10-01','Pay4',1623),
 ('2022-10-01','Pay5',1904), ('2022-10-01','Pay6',1853), ('2022-10-01','Pay7',1826), ('2022-10-01','Pay8',247),
 ('2022-10-01','Pay9',713), ('2022-10-01','Pay10',1159), ('2022-10-02','Pay1',755), ('2022-10-02','Pay2',786),
 ('2022-10-02','Pay3',623), ('2022-10-02','Pay4',1766), ('2022-10-02','Pay5',1141), ('2022-10-02','Pay6',362),
 ('2022-10-02','Pay7',1097), ('2022-10-02','Pay8',655), ('2022-10-02','Pay9',1569), ('2022-10-02','Pay10',796)]

data = pd.DataFrame(data, columns=['date', 'system', 'number'])

import plotly.express as px
fig = px.bar(data, x='date', y='number', color='system', title="Bar Graph Sorted by Value")
fig.update_xaxes(categoryorder='total ascending')
fig.show()
英文:

I have a dataframe with data, the code is below, in which there are 3 columns - date, system and number, building a bar graph in Plotly I get two bars in which I cannot set the sorting by values, they are atomatically sorted by name.

import pandas as pd
import numpy as np


data = [('2022-10-01','Pay1',644), ('2022-10-01','Pay2',1460), ('2022-10-01','Pay3',1221), ('2022-10-01','Pay4',1623),\
 ('2022-10-01','Pay5',1904), ('2022-10-01','Pay6',1853), ('2022-10-01','Pay7',1826), ('2022-10-01','Pay8',247),\
 ('2022-10-01','Pay9',713), ('2022-10-01','Pay10',1159), ('2022-10-02','Pay1',755), ('2022-10-02','Pay2',786),\
 ('2022-10-02','Pay3',623), ('2022-10-02','Pay4',1766), ('2022-10-02','Pay5',1141), ('2022-10-02','Pay6',362),\
    ('2022-10-02','Pay7',1097), ('2022-10-02','Pay8',655), ('2022-10-02','Pay9',1569), ('2022-10-02','Pay10',796)]

data = pd.DataFrame(data,columns=['date','system','number'])

import plotly.express as px
fig = px.bar(data, x='date', y='number', 
            color='system')
fig.show()

如何在Plotly中构建按值排序的多值列?

I want to get a bar that will be sorted by value, from smallest to largest in each case

答案1

得分: 0

以下是代码部分的翻译:

# 导入所需的库
import plotly.graph_objects as go
import plotly.express as px

# 获取绘图颜色
colors = px.colors.qualitative.Plotly
# 获取数据中的系统名称
system_name = data['system'].unique()
# 创建一个字典将系统名称映射到颜色
colors_dict = {k: v for k, v in zip(system_name, colors)}

# 创建一个绘图对象
fig = go.Figure()

# 选择日期为"2022-10-01"的数据
dff = data.query('date == "2022-10-01"')
# 根据'number'列的值降序排序数据框
dff = dff.sort_values('number', ascending=False)
# 将颜色映射到系统名称并添加到数据框中
dff['color'] = dff['system'].map(colors_dict)
# 遍历数据框的每一行
for row in dff.itertuples():
    # 添加柱状图轨迹
    fig.add_trace(go.Bar(x=[row.date], y=[row.number], name=row.system, marker_color=row.color))
    # 更新布局以进行堆叠
    fig.update_layout(barmode='stack')

# 选择日期为"2022-10-02"的数据
dfm = data.query('date == "2022-10-02"')
# 根据'number'列的值降序排序数据框
dfm = dfm.sort_values('number', ascending=False)
# 将颜色映射到系统名称并添加到数据框中
dfm['color'] = dfm['system'].map(colors_dict)
# 遍历数据框的每一行
for row in dfm.itertuples():
    # 添加柱状图轨迹
    fig.add_trace(go.Bar(x=[row.date], y=[row.number], name=row.system, marker_color=row.color))
    # 更新布局以进行堆叠
    fig.update_layout(barmode='stack')

# 创建一个集合来跟踪已经添加的名称
names = set()
fig.for_each_trace(
    lambda trace:
        trace.update(showlegend=False)
        if (trace.name in names) else names.add(trace.name))

# 显示绘图
fig.show()

这是代码部分的翻译,没有包括原始代码中的注释。

英文:

The expected graph is a stacked graph using the same color for categorical variables, and the order of the graphs is in order of increasing numerical value. To make the categorical variables the same color, create a dictionary of default discrete to maps and system columns. Add a column of colors to each data frame. Extract data frames by date, sort them in numerical order of size, and loop through them row by row.

import plotly.graph_objects as go
import plotly.express as px

colors = px.colors.qualitative.Plotly
system_name = data['system'].unique()
colors_dict = {k:v for k,v in zip(system_name, colors)}
# print(colors_dict)

fig = go.Figure()

dff = data.query('date =="2022-10-01"')
dff = dff.sort_values('number',ascending=False)
dff['color'] = dff['system'].map(colors_dict)
for row in dff.itertuples():
    fig.add_trace(go.Bar(x=[row.date], y=[row.number], name=row.system, marker_color=row.color))
    fig.update_layout(barmode='stack')

dfm = data.query('date =="2022-10-02"')
dfm = dfm.sort_values('number',ascending=False)
dfm['color'] = dfm['system'].map(colors_dict)
for row in dfm.itertuples():
    fig.add_trace(go.Bar(x=[row.date], y=[row.number], name=row.system, marker_color=row.color))
    fig.update_layout(barmode='stack')
    
names = set()
fig.for_each_trace(
    lambda trace:
        trace.update(showlegend=False)
        if (trace.name in names) else names.add(trace.name))

fig.show()

如何在Plotly中构建按值排序的多值列?

huangapple
  • 本文由 发表于 2023年2月8日 18:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定