I have generated this plot enter image description here

从这个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个级别出现一次变量名(减go 大小写,有两个级别),并在每个变量组之间增加一些空格.有什么建议吗?

推荐答案

也许这会帮助你入门,我不知道如何用格式居中分类(它基本上是在文本周围包裹latex ,当你这样做的时候,你似乎失go 了理由).

这也可能有助于处理主要刻度和次要刻度,例如,将变量名称设置为主要刻度,将级别设置为次要刻度,然后您可以为主要刻度删除刻度本身,但为未成年人保留它们.

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()

enter image description here

Python相关问答推荐

已安装' owiener ' Python模块,但在导入过程中始终没有名为owiener的模块

从Python调用GMP C函数时的分段错误和内存泄漏

为什么基于条件的过滤会导致pandas中的空数据框架?

如何将Matplotlib的fig.add_axes本地坐标与我的坐标关联起来?

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

Pydantic 2.7.0模型接受字符串日期时间或无

如何在msgraph.GraphServiceClient上进行身份验证?

使用新的类型语法正确注释ParamSecdecorator (3.12)

如何让程序打印新段落上的每一行?

根据二元组列表在pandas中创建新列

Telethon加入私有频道

如何更改分组条形图中条形图的 colored颜色 ?

try 将一行连接到Tensorflow中的矩阵

在np数组上实现无重叠的二维滑动窗口

Pandas—在数据透视表中占总数的百分比

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

删除marplotlib条形图上的底边

如何在Python中使用另一个数据框更改列值(列表)

使用特定值作为引用替换数据框行上的值

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值