英文:
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()
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论