简体   繁体   English

如何为 Plotly 条形图设置 static x 轴值和图例集?

[英]How to set a static set of x-axis values and legend for a Plotly Bar Graph?

The Plotly Bar graphs below show a count of employees in three different seniority classes for a given dept (engineering, accounting, etc..)下面的 Plotly 条形图显示了给定部门(工程、会计等)三个不同资历级别的员工数量。

If one of the many values for the x-axis is not in the current data frame used to construct the graph, no bar/x-axis/color is shown.如果 x 轴的多个值之一不在用于构建图形的当前数据框中,则不会显示条形/x 轴/颜色。 Below accounting has employees in all three so they show but accounting doesn't possess all there.下面的会计部门在这三个方面都有员工,所以他们显示出来,但会计部门并不具备所有这些。

Is there something I can do to make sure this graph consistently shows all three values and that a missing x-axis value will assume a default y-axis value of 0.我可以做些什么来确保此图表始终显示所有三个值,并且缺少的 x 轴值将假定默认 y 轴值为 0。

I want the engineering graph to show an bar for sr as well but default the value to 0.我希望工程图也显示 sr 条,但默认值为 0。

Ploty Graph showing Count by Seniority (Junior, Mid, Senior) for Engineering Dept显示工程部按资历(初级、中级、高级)计数的情节图

Ploty Graph showing Count by Seniority (Junior, Mid, Senior) for Accounting Dept显示会计部门按资历(初级、中级、高级)计数的绘图图

Here is a snippet of the code use to generate the graph above as well as snapshot of the master Dataframe.这是用于生成上述图表的代码片段以及主 Dataframe 的快照。

fig = px.bar(g[g['dept']=='accounting']
         , x='level', y='id', color='level', labels = {
             'level' : 'Job Seniority',
             'id' : 'Employee Count'
         }, title='Count by Seniority (Junior, Mid, Senior) for Accounting Dept')

In code snippet above, g points to a Dataframe that looks like this:在上面的代码片段中,g 指向一个 Dataframe,如下所示:

dept部门 level等级 id ID
0 0 accounting会计 jr年少者 1 1
1 1 accounting会计 mid 2 2
2 2 accounting会计 sr sr 1 1
3 3 business development业务发展 jr年少者 2 2
4 4 business development业务发展 mid 1 1
5 5 business development业务发展 sr sr 1 1
6 6 engineering工程 jr年少者 2 2
7 7 engineering工程 mid 1 1
8 8 human resources人力资源 jr年少者 1 1
9 9 legal合法的 jr年少者 1 1
10 10 legal合法的 mid 1 1
11 11 legal合法的 sr sr 1 1
12 12 marketing营销 jr年少者 2 2
13 13 marketing营销 mid 1 1
14 14 research and development研究与开发 jr年少者 1 1
15 15 sales销售量 sr sr 1 1
16 16 services服务 jr年少者 2 2
17 17 services服务 mid 1 1
18 18 support支持 jr年少者 3 3
19 19 support支持 mid 2 2
20 20 training训练 mid 2 2
  • prepare the data for plotting by organising index and columns通过组织索引和列来准备用于绘图的数据
  • used graph_objects so make_subplots() can be used so it's a simple looping process to generate a graph per department (not plotly is 1-indexed, python 0-indexed)使用了 graph_objects所以可以使用make_subplots()所以这是一个简单的循环过程来生成每个部门的图(不是plotly是 1-indexed,python 是 0-indexed)
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots


df = pd.DataFrame({"dept": ["accounting", "accounting", "accounting", "business development", "business development", "business development", "engineering", "engineering", "human resources", "legal", "legal", "legal", "marketing", "marketing", "research and development", "sales", "services", "services", "support", "support", "training"], 
 "level": ["jr", "mid", "sr", "jr", "mid", "sr", "jr", "mid", "jr", "jr", "mid", "sr", "jr", "mid", "jr", "sr", "jr", "mid", "jr", "mid", "mid"], 
 "id": [1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 3, 2, 2]})

dfp = df.groupby(["dept","level"]).sum().unstack("level").fillna(0).droplevel(0,1).T

fig = make_subplots(rows=(len(dfp.columns)+1)//3, cols=3, subplot_titles=dfp.columns)
for i, c in enumerate(dfp.columns):
    fig.add_trace(go.Bar(x=dfp.index, y=dfp[c], showlegend=False, 
                         marker_color=np.select([dfp.index=="jr",dfp.index=="mid"],["red","blue"],"green")), 
                  row=(i//3)+1, col=(i%3)+1)
    
fig.update_layout({"margin":{"b":0,"t":20,"l":0,"r":0}})


在此处输入图像描述

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

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