我有以下数据框:
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)。
希望这是有道理的。谢谢!
最佳答案
听起来您想要一个分组和堆叠的条形图,据我所知 is not available at the moment 。但是,当涉及到对特定数据帧的所有方面进行分组和可视化时,plotly 在 px.Bar()
的参数中还有其他灵活的选项:
plotly :
代码:
# 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()
关于python - 从数据框中绘制并排堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55145472/