[英]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 ![]() |
|
---|---|---|---|
0 ![]() |
accounting![]() |
jr![]() |
1 ![]() |
1 ![]() |
accounting![]() |
mid![]() |
2 ![]() |
2 ![]() |
accounting![]() |
sr ![]() |
1 ![]() |
3 ![]() |
business development![]() |
jr![]() |
2 ![]() |
4 ![]() |
business development![]() |
mid![]() |
1 ![]() |
5 ![]() |
business development![]() |
sr ![]() |
1 ![]() |
6 ![]() |
engineering![]() |
jr![]() |
2 ![]() |
7 ![]() |
engineering![]() |
mid![]() |
1 ![]() |
8 ![]() |
human resources![]() |
jr![]() |
1 ![]() |
9 ![]() |
legal![]() |
jr![]() |
1 ![]() |
10 ![]() |
legal![]() |
mid![]() |
1 ![]() |
11 ![]() |
legal![]() |
sr ![]() |
1 ![]() |
12 ![]() |
marketing![]() |
jr![]() |
2 ![]() |
13 ![]() |
marketing![]() |
mid![]() |
1 ![]() |
14 ![]() |
research and development![]() |
jr![]() |
1 ![]() |
15 ![]() |
sales![]() |
sr ![]() |
1 ![]() |
16 ![]() |
services![]() |
jr![]() |
2 ![]() |
17 ![]() |
services![]() |
mid![]() |
1 ![]() |
18 ![]() |
support![]() |
jr![]() |
3 ![]() |
19 ![]() |
support![]() |
mid![]() |
2 ![]() |
20 ![]() |
training![]() |
mid![]() |
2 ![]() |
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)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.