情节:如何从单个轨迹制作堆叠的条形图? [英] Plotly: How to make stacked bar chart from single trace?
问题描述
是否可以并排放置两个堆叠的条形图,每个条形图来自同一列?
这是我的df:
Field Issue
Police Budget cuts
Research Budget cuts
Police Time consuming
Banking Lack of support
Healthcare Lack of support
Research Bureaucracy
Healthcare Bureaucracy
Banking Budget cuts
我想要按字段排列的堆积条形图旁边的按问题排列的堆积条形图.
谢谢大家!
有关如何使用提供的特定数据集制作堆叠条形图的示例,请查看name = 'segment 1'这样指定:
代码段4:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = [go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[20, 14, 23, 15], name = 'segment 1'),
go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[5, 14, 4, 20], name = "segment 2")]
layout = go.Layout(barmode='stack', title = 'Stacked bar chart')
fig = go.Figure(data=data, layout=layout)
iplot(fig)
图4:添加命名迹线
如果要拆开"条形,只需将条形模式更改为分组",如下所示:
layout = go.Layout(barmode='group', title = 'Stacked bar chart')
图5:
Is it possible to have two stacked bar charts side by side each of them coming from a single column?
Here's my df:
Field Issue
Police Budget cuts
Research Budget cuts
Police Time consuming
Banking Lack of support
Healthcare Lack of support
Research Bureaucracy
Healthcare Bureaucracy
Banking Budget cuts
I want a stacked bar chart of Field next to a stacked bar chart of issues by field.
Thanks guys!
For an example on how to make a stacked bar chart with the specific dataset provided, take a look at the suggestion in Plotly-Dash: Want two stacked bar charts side by side from single df column. The following answer addresses how you can not make a stacked bar chart with a single trace. To show you why, I'll start with what may seem like redundant details. But hopefully everything will be clear in the end.
In plotly terms, trace
is a term used to describe a graph object
such as Scatter
or Bar
like in the snippet below.
Snippet 1:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = [go.Bar(
x=['CategoryA', 'CategoryB', 'CategoryC'],
y=[20, 14, 23]
)]
iplot(data)
If you look at the snippet above, you'll see that that is a bar chart with a single trace in the form of go.Bar(x=['CategoryA',...
.
Plot 1.1:
Now, how can we add something to make it a stacked chart
? If you start out by adding an element to x
like 'CategoryD'
, then nothing happens. And that's a bit interesting since you'd might expect an error message instead.
Plot 1.2: Added x value
But nothing happens before CategoryD
has a corresponding y value like 15
:
Plot 1.3: Added x and y value
Conclusion so far: Adding values to x
and y
will add another category on the x-axis and a value on the y-axis. As you can see, nothing is getting stacked
here quite yet:
But what if you add a layout term with barmode='stack'
?
Snippet 2:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = [go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[20, 14, 23, 15])]
layout = go.Layout(barmode='stack', title = 'Stacked bar chart')
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Plot 2: barmode='stack'
I can understand anyone hoping that this would stack all data within a single trace, but plotly just doesn't seem to be built that way. To get what you need, you'll have to add another trace or go.Bar()
object like this:
Snippet 3:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = [go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[20, 14, 23, 15]),
go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[5, 14, 4, 20])]
layout = go.Layout(barmode='stack', title = 'Stacked bar chart')
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Plot 3: Add trace
Notice that neither of the go.Bar()
objects have had any name assigned to them, and that plotly by default names them trace 0
and trace 1
. So I guess it's more correct to say that a trace contains or 'shows' a plotly graph object rather than calling them the same thing. If you'd like to specify other names, you can do so with, for example, name = 'segment 1'
like this:
Snippet 4:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = [go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[20, 14, 23, 15], name = 'segment 1'),
go.Bar(x=['CategoryA', 'CategoryB', 'CategoryC', 'CategoryD'],
y=[5, 14, 4, 20], name = "segment 2")]
layout = go.Layout(barmode='stack', title = 'Stacked bar chart')
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Plot 4: Add named traces
If you'd like to 'unstack' your bars, just change barmode to 'group' like this:
layout = go.Layout(barmode='group', title = 'Stacked bar chart')
Plot 5:
这篇关于情节:如何从单个轨迹制作堆叠的条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!