pandas 在Matplotlib双向图中添加特定标签和间距

jogvjijk  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(74)

我生成了这个图

从这个df:

variable        level   margins_fluid   margins_vp
0   volume      1L         0.718          0.690
1   volume      2L         0.501          0.808
2   volume      5L         0.181          0.920
3   MAP         64         0.434          0.647
4   MAP         58         0.477          0.854
5   MAP         52         0.489          0.904
6   Exam       dry         0.668          0.713
7   exam     euvolemic     0.475          0.798
8   exam       wet         0.262          0.893
9   history    COPD        0.506          0.804
10  history   Kidney       0.441          0.778
11  history     HF         0.450          0.832
12  Case    1 (PIV)        0.435          0.802
13  Case    2 (CVC)        0.497          0.809

使用此代码:

font_color = '#525252'
hfont = {'fontname':'Calibri'}
facecolor = '#eaeaf2'
index = fluid_vp_1_2.index
column0 = fluid_vp_1_2['margins_fluid']
column1 = fluid_vp_1_2['margins_vp']
title0 = 'Fluids'
title1 = 'Vasopressors'

fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor, ncols=2, sharey=True)
fig.tight_layout()
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='lightgray', 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_yticks([])
axes[1].set_yticks([])

但是,我希望它与此图纸的最左侧面板完全匹配:

变量名在每组3个水平中出现一次(减去有两个水平的Case),并且在每个变量组之间增加一些间距。

dxpyg8gm

dxpyg8gm1#

也许这会帮助你开始,我不知道如何用格式将类别居中(它基本上是在文本周围 Package 乳胶,似乎你失去了理由时,你这样做)。
它也可能有助于发挥周围的主要和次要刻度,例如设置变量名称为主要刻度和水平为次要刻度,然后你可以删除刻度本身的主要,但保留它们的次要刻度。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

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']
column1 = fluid_vp_1_2['margins_vp']
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='lightgray', 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].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.rjust(maxWordLength)}"+r'}$' if t in variableNames else t.rjust(maxWordLength) for t in fluid_vp_1_2['level']]
axes[0].set_yticklabels(formattedyticklabels)
    
fig.tight_layout()

plt.show()

相关问题