我有这个代码:
s = """level,cvc
Vasopressor dose,0
Low, 0.263
Med, .563
High, 0.777
Vasopressor trend,0
Decreasing, 0.367
Stable, 0.521
Increasing, 0.714
Duration,0
8 hours, 0.478
24 hours, 0.591
PIV Location,0
Upper Arm, 0.528
Forearm, 0.541
Case,0
7, 0.541
8, 0.526
9, 0.507
10, 0.564"""
data = np.array([a.split(',') for a in s.split("\n")])
#print(data)
cvc = pd.DataFrame(data[1:], columns=data[0])
cvc['cvc'] = cvc['cvc'].apply(float)
#print(cvc)
variableNames = {'Vasopressor dose', 'Vasopressor trend', 'Duration', 'PIV Location', 'Case'}
font_color = '#525252'
hfont = {'fontname':'DejaVu Sans'}
facecolor = '#eaeaf2'
index = cvc.index#['level']
column0 = cvc['cvc']*100
title0 = 'Central Line Placed'
fig, axes = plt.subplots(figsize=(8, 12), facecolor=facecolor)
axes.barh(index, column0, align='center', color='darkslategray', zorder=10)
axes.set_title(title0, fontsize=18, pad=15, color='black', **hfont)
grid_params = dict(
zorder=0,
axis='x'
)
# If you have positive numbers and want to invert the x-axis of the left plot
axes.invert_xaxis()
# To show data from highest to lowest
plt.gca().invert_yaxis()
axes.set(xlim = [0,100])
axes.yaxis.tick_left()
axes.set_yticks(range(len(cvc)))
formattedyticklabels = [r'$\bf{'+f"{t}"+r'}$'
if t in variableNames else t for t in cvc['level']]
#print(formattedyticklabels)
axes.set_yticklabels(formattedyticklabels)
axes.grid(**grid_params)
fig.supxlabel("adjusted proportion of respondents", x = 0.62, size = 15)
axes.tick_params(axis='both', labelsize=15)
axes.tick_params(left = False)
fig.patch.set_facecolor('white')
plt.savefig("cvc.jpg")
fig.tight_layout()
plt.show()
对于集合
variableNames
中提到的带有空格的y-tick标签,当我添加粗体格式时,这些空格将被删除。任何想法如何解决这个问题?我已经提到了这个question但不确定如何在我的列表理解中实现它:
formattedyticklabels = [r'$\bf{'+f"{t}"+r'}$'
if t in variableNames else t for t in cvc['level']]
有什么建议吗?