python 中的双向条形图,如何删除所有背景色?

Bidirectional Bar Plot in python, how to remove all background colours?

我写了这段代码(使用 this 示例):

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import seaborn as sns
#sns.set()
#%matplotlib notebook
#plt.style.use('classic')

class_list = ['test1','test2','test3','test4','test5','test6','test7']
data = pd.DataFrame(data=zip(class_list,average_length,num_entries),columns=['Class','Lens','Nums'])
data.set_index('Class', inplace=True)

font_color = '#525252'
hfont = {'fontname':'Calibri'}
#facecolor = '#eaeaf2'
color_red = '#fd625e'
color_blue = '#01b8aa'
index = data.index
column0 = data['Lens']
column1 = data['Nums']
title0 = "Title 1"
title1 = 'Title 2'

fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor, ncols=2,sharey=True)
fig.tight_layout()

axes[0].barh(index, column0, align='center', color=color_red, zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color=color_red, **hfont)
axes[1].barh(index, column1, align='center', color=color_blue, zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color=color_blue, **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=data.index, yticklabels=data.index)
axes[0].yaxis.tick_left()
axes[0].tick_params(axis='y', colors='white') # tick color

axes[1].set_xticklabels(['0','20', '40', '60', '80', '100', '120'])


for label in (axes[0].get_xticklabels() + axes[0].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
for label in (axes[1].get_xticklabels() + axes[1].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
    
plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.18, right=0.95)
plt.show()

输出为:

我想去掉所有背景颜色(所以没有'Figure 1',没有白色网格线,背景是纯白色而不是蓝-y紫色),然后在x上加一条黑线和 y 轴。

当我尝试添加 x 和 y 轴线时:

plt.update_xaxes(showline=True, linewidth=2, linecolor='black')
plt.update_yaxes(showline=True, linewidth=2, linecolor='black')

没有错误,但也没有图表。

编辑 1: 添加

axes[0].set_facecolor('white')
axes[1].set_facecolor('white')

已成功去除背景颜色。我还尝试通过以下方式添加 x 和 y 轴线:

axes[0].axvline(0)
axes[0].axhline(0)

这会在底栏中间生成一条线:

(并且 'Figure 1' 仍然存在)。

参数facecolor负责背景色。您可以将其删除以获得白色背景。

使用 set_window_title 将新标题添加到 window。

修改后的代码如下所示:

fig, axes = plt.subplots(figsize=(10,5), ncols=2,sharey=True) # Removed facecolor param
fig.tight_layout()
fig.canvas.set_window_title('Custom Title') # Adds new title to the window