如何在matplotlib中将轴背景颜色更改为透明

问题描述 投票:0回答:1
import matplotlib
import pandas as pd
df = pd.DataFrame({
    'name': list('AABB'),
    'open' :[51.40,40.15,757.00,27.55],
    'high' :[54.70,40.65,771.00,27.65],
    'low'  :[51.00,40.05,754.00,27.30],
    'close':[53.30,40.30,770.00,27.50]
})

fig, ax = plt.subplots(df.shape[0]+1,1)
plt.gcf().set_size_inches(0.5,(df.shape[0]+1))
i=1
ax[0].axis('off')
for index, row in df.iterrows():
    if row['close'] > row['open']:
            color = 'crimson' 
    elif row['close'] < row['open']:
        color = 'green'
    else:
        color = 'gray'
    ax[i].bar(row['name'], abs(row['close'] - row['open']), bottom=min(row['open'], row['close']), color=color)
    ax[i].vlines(row['name'], row['low'], row['high'], color=color)
    ax[i].axis('off')
    i +=1

plt.savefig('test2.jpg',transparent=True,bbox_inches='tight')
plt.close()

我用它来创建烛台图 [空,烛台,烛台,烛台,烛台] enter image description here 但是,背景颜色不透明

我尝试添加透明=True,但它不起作用。

plt.savefig('test2.jpg',transparent=True,bbox_inches='tight')

如何让所有白色背景色变成透明?

python matplotlib background-color
1个回答
0
投票

您的代码工作正常,但 JPG 不支持透明度。

您必须导出为其他格式,例如 PNG:

plt.savefig('test2.png', transparent=True, bbox_inches='tight')

输出:

© www.soinside.com 2019 - 2024. All rights reserved.