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()
I use it to create a candlestick graph.
![empty, candlestick, candlestick, candlestick, candlestick]
However, the background color isn't transparent.
I try add transparent=True
but it doesn't work.
plt.savefig('test2.jpg',transparent=True,bbox_inches='tight')
How can I let all white background color into transparent?
Your code works fine but JPG doesn't support transparency.
You have to export to another format, like PNG:
plt.savefig('test2.png', transparent=True, bbox_inches='tight')
Output:
Ironically, imgur has converted the above output to JPG, thus losing the transparency.