完成子批次图的步骤

2024-03-29 00:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在完成这个子地块时遇到了一些麻烦。在这一点上,我所需要做的就是去掉底部混乱的文本,然后为每个子批次中的每个条添加单独的X标签(所有子地块共享同一组X值)

这是我到目前为止所拥有的

dictlist = []

for i in quarters:
    d = defaultdict(int)
    x = df['Activity'][df['QuarterYear']==i].tolist()
    for j in x:
        d[j] += 1
    dictlist.append(d)

这将返回一个包含4个字典的列表。每一个有钥匙和价值的每一个季度都会计数。这是口述列表[0]

defaultdict(int, {“护理协调”:11052, “综合护理管理”:1403, “健康促进”:6575, “患者和家庭支持”:9731, “社会支持转介”:1888年, “过渡期护理”:239})

这是我的图形代码

f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True, sharey=True)

axes[0,0].bar(dictlist[0].keys(), dictlist[0].values())
axes[0,0].set_xlabel(quarters[0])
#axes[0,0].set_xticks(range(0,7))
#axes[0,0].set_xticklabels([*dictlist[0]], fontdict=None, minor=False)
for p in axes[0,0].patches:
    axes[0,0].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))

axes[0,1].bar(dictlist[1].keys(), dictlist[1].values())
axes[0,1].set_xlabel(quarters[1])
for p in axes[0,1].patches:
    axes[0,1].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))

axes[1,0].bar(dictlist[2].keys(), dictlist[2].values())
axes[1,0].set_xlabel(quarters[2])
for p in axes[1,0].patches:
    axes[1,0].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))

axes[1,1].bar(dictlist[3].keys(), dictlist[3].values())
axes[1,1].set_xlabel(quarters[3])
for p in axes[1,1].patches:
    axes[1,1].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))

fig = plt.gcf()
fig.set_size_inches( 16, 16)
fig.savefig('output5.png')

enter image description here

我不确定到底是什么原因导致了底部的混乱文本,我想以某种方式创建一个主图例。任何帮助都将不胜感激。这是Python3.6


Tags: inforgetbarkeysvaluesheightset
1条回答
网友
1楼 · 发布于 2024-03-29 00:27:22

好吧,我想起来了。这是我的代码和图表的代码。我不得不重复每一个记号,这让我很困惑

f, axes = plt.subplots(2, 2, figsize=(7, 7), sharey=True, sharex=True)

axes[0,0].bar([*dictlist[0].keys()], [*dictlist[0].values()])
axes[0,0].set_xlabel(quarters[0])
for p in axes[0,0].patches:
    axes[0,0].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))
for tick in axes[0,0].get_xticklabels():
    tick.set_rotation(90)


axes[0,1].bar([*dictlist[1].keys()], [*dictlist[1].values()])
axes[0,1].set_xlabel(quarters[1])
for p in axes[0,1].patches:
    axes[0,1].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))
for tick in axes[0,1].get_xticklabels():
    tick.set_rotation(90)


axes[1,0].bar([*dictlist[2].keys()], [*dictlist[2].values()])
axes[1,0].set_xlabel(quarters[2])
for p in axes[1,0].patches:
    axes[1,0].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))
for tick in axes[1,0].get_xticklabels():
    tick.set_rotation(90)


axes[1,1].bar([*dictlist[3].keys()], [*dictlist[3].values()])
axes[1,1].set_xlabel(quarters[3])
for p in axes[1,1].patches:
    axes[1,1].annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))
for tick in axes[1,1].get_xticklabels():
    tick.set_rotation(90)


fig = plt.gcf()
fig.set_size_inches( 16, 16)

plt.tight_layout()

enter image description here

相关问题 更多 >