我有以下数据:
Position Tenure Location Concept
a 0-3 d Y
a 4-7 e N
b 4-7 f M
c 8-11 d M
d 0-3 g Y
b 12-14 f N
我想要的是一个并排堆叠的图表的位置,任期和位置的每一个概念。例如,对于概念Y,我想要一个位置的条形图(高度为2,叠加的a和d),在保有权图(高度为2,在这种情况下只有0-3)和位置图(高度为2,1表示d和1表示g在这里)。
希望这是有意义的。谢谢!
发布于 2020-01-17 14:08:37
听起来你想要一个分组和堆叠的条形图,据我所知,目前不可用。但是,当涉及到分组和可视化特定数据的所有方面时,px.Bar()
的参数中有其他灵活的选项。
情节:
代码:
# imports
import plotly.express as px
import pandas as pd
# data input
df = pd.DataFrame({'Position': {0: 'a', 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'b'},
'Tenure': {0: '0-3', 1: '4-7', 2: '4-7', 3: '8-11', 4: '0-3', 5: '12-14'},
'Location': {0: 'd', 1: 'e', 2: 'f', 3: 'd', 4: 'g', 5: 'f'},
'Concept': {0: 'Y', 1: 'N', 2: 'M', 3: 'M', 4: 'Y', 5: 'N'}})
# plotly express
fig = px.bar(df, x="Concept", y="Tenure", color="Tenure", barmode="group",
facet_col="Location",color_discrete_sequence=px.colors.diverging.Spectral[-2::-1])
# Remove duplicate x-axis names:
for axis in fig.layout:
if type(fig.layout[axis]) == go.layout.XAxis:
fig.layout[axis].title.text = ''
fig['layout']['xaxis2']['title']['text']='Concept'
fig.show()
https://stackoverflow.com/questions/55145472
复制相似问题