python - 从数据框中绘制并排堆叠条形图

标签 python python-3.x plotly

我有以下数据框:

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 :

enter image description here

代码:

# 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/

相关文章:

python - 无法访问表单字段

Python 可嵌入 zip : install Tkinter

javascript - plotly js去除标题和标题区域

python-3.x - 对于共享 IP 地址的多个 Flask 站点,Certbot 证书不适用于 Apache

python-3.x - 如何将 Azure-cli 命令的输出保存在变量中

callback - 使用 plotly express 并在一页上有多个图形时,随机出现导入错误

将等高线图限制为不同图的凸包

python - 指定要使用的库 (.so) 文件

python - 有没有办法改变 strptime() 的阈值?

Python script.py 文件 url 参数