我生成了这个条形图
使用此代码:
s = """level,margins_fluid,margins_vp
Volume,0,0
1L*,0.718,0.690
2L,0.501,0.808
5L,0.181,0.920
MAP,0,0
64*,0.434,0.647
58,0.477,0.854
52,0.489,0.904
Exam,0,0
dry,0.668,0.713
euvolemic*,0.475,0.798
wet,0.262,0.893
History,0,0
COPD*,0.506,0.804
Kidney,0.441,0.778
HF,0.450,0.832
Case,0,0
1 (PIV),0.435,0.802
2 (CVC)*,0.497,0.809"""
data = np.array([a.split(',') for a in s.split("\n")])
fluid_vp_1_2 = pd.DataFrame(data[1:], columns=data[0])
fluid_vp_1_2['margins_fluid'] = fluid_vp_1_2['margins_fluid'].apply(float)
fluid_vp_1_2['margins_vp'] = fluid_vp_1_2['margins_vp'].apply(float)
fluid_vp_1_2
variableNames = {'Volume', 'MAP', 'Exam', 'History', 'Case'}
font_color = '#525252'
hfont = {'fontname':'DejaVu Sans'}
facecolor = '#eaeaf2'
index = fluid_vp_1_2.index#['level']
column0 = fluid_vp_1_2['margins_fluid']*100
column1 = fluid_vp_1_2['margins_vp']*100
title0 = 'Fluids'
title1 = 'Vasopressors'
fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor, ncols=2, sharey=True)
axes[0].barh(index, column0, align='center', color='dimgray', zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color='black', **hfont)
axes[1].barh(index, column1, align='center', color='silver', zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color='black', **hfont)
# If you have positive numbers and want to invert the x-axis of the left plot
axes[0].invert_xaxis()
# To show data from highest to lowest
plt.gca().invert_yaxis()
axes[0].set(xlim = [100,0])
axes[1].set(xlim = [0,100])
axes[0].yaxis.tick_right()
axes[0].set_yticks(range(len(fluid_vp_1_2)))
maxWordLength = fluid_vp_1_2['level'].apply(lambda x: len(x)).max()
formattedyticklabels = [r'$\bf{'+f"{t}"+r'}$'
if t in variableNames else t for t in fluid_vp_1_2['level']]
axes[0].set_yticklabels(formattedyticklabels, ha='center', position=(1.12, 0))
axes[0].tick_params(right = False)
axes[1].tick_params(left = False)
fig.tight_layout()
plt.savefig("fluid_vp_1_2.jpg")
plt.show()
然而,我想修改这个图表,使其更接近下面的例子,其中y轴标签是在左手边,双向酒吧是在中心接触,白色背景,更垂直的形状(缩小x轴),添加x轴标签(“调整后的答复者比例”)、但我仍然希望保持变量的顺序和由粗体标题标签(如Volume
,MAP
等)引起的条形图中的间隙。
有什么建议吗?
2条答案
按热度按时间smdnsysy1#
有一些简化/因子分解,你可以处理,使你的图更容易样式化。但你基本上是在那里。只需设置刻度标签,并删除与
fig.subplots_adjust(wspace=0)
图之间的空格(你必须删除fig.tight_layout()
):编辑:你可以通过改变
figsize
得到一个“更长”的图。figsize=(8,10)
的输出:qij5mzcb2#
在这个answer中,我将使用plotly。
为了实现你正在寻找的东西,你主要需要玩你的数据框。
数据
整理数据
首先要更改 volfuid 和 pmh 的名称。如果需要,可以为此创建单独的列。我将覆盖原来的列
然后如果你想保持给定的顺序,你可以将这个列设置为
category
dtype。其中顺序由输入
categories
给出。我们排序时要记住,在图中顺序是相反的最后我们让它大胆
Plot