简体   繁体   中英

Save figure with its frame, matplotlib

How should I save a figure containing also its frame?

From this code;

import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib as mpl

my_list = [random.uniform(0.75, 1.25) for i in range(1000)]

mpl.rcParams['axes.linewidth'] = 2.5
my_fig = plt.figure(num=None, figsize=(12, 7), linewidth=5, facecolor='gold', edgecolor='gray')

plt.plot(my_list, label = 'My label',color = 'r',  lw = 2.5)

plt.grid()
plt.gca().invert_xaxis()

plt.legend(loc='upper right', fontsize=14)
plt.ylabel('\n Y_axis [kPa] \n\n',fontsize=14)
plt.xlabel('\n X_axis [w] \n',fontsize=14)
plt.ylim([0,2])
plt.tick_params(axis='x', labelsize=12)
plt.tick_params(axis='y', labelsize=12)

plt.savefig("test.png")

plt.show()

I expect this figure;

在此处输入图片说明

But I get it without frame;

在此处输入图片说明

Where I'm wrong?

savefig will not honour the figure's face and edge colors but you can set them explicitly in the call.

plt.savefig('test.png', facecolor=my_fig.get_facecolor(),         
            edgecolor=my_fig.get_edgecolor())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM