如何改变matplotlib图表的背景颜色?

问题描述 投票:1回答:1

我不知道为什么会出现在我的图表将自动为灰框。我想设置我的图表全白和一个黑色的边框和网格。

fig = plt.figure()
fig.set_facecolor('none')
plt.rcParams['axes.facecolor'] = 'none'
ax1 = fig.add_subplot(111)
ax1.set_title('Druck')
plt.xlabel('L/D')
plt.ylabel('$Q_{CPT}/Q_{API}$')
plt.ylim((0,4.5))
plt.xlim((0,40))
plt.scatter(x1,y1, c='b', label='UWA-05')
plt.scatter(x2,y2,c='r', label='ICP-05')
plt.grid(axis='y', c='black',alpha=0.8)
plt.grid(axis='x', c='black',alpha=0.8)
plt.legend()
leg = plt.legend()
leg.get_frame().set_edgecolor('none')
plt.show()

chart with the weird grey frame

python matplotlib
1个回答
1
投票

添加这些行,明显改变背景颜色,以任何你想要的

 bg_color = 'black'
 ax1.patch.set_facecolor(bg_color)

也改变这一点:

leg.get_frame().set_edgecolor('Black') #<---- change 'none' to 'Black'
© www.soinside.com 2019 - 2024. All rights reserved.