Talha Yousuf
Talha Yousuf

Reputation: 301

How to save images plotted on different figures to different locations in matplotlib?

I have plotted two different plots in separate figures. Here is the relevant part of code:

f1=plt.figure()
f2=plt.figure()

ax1=f1.add_subplot(111)
ax1.matshow(conf_mat,cmap=plt.cm.gray)
ax1.set_xticks(np.arange(len(load_label_names())))
ax1.set_xticklabels(load_label_names(),rotation = 45)
ax1.set_yticks(np.arange(len(load_label_names())))
ax1.set_yticklabels(load_label_names(),rotation = 45)
ax1.set_title('Confusion Matrix')

ax2=f2.add_subplot(111)
ax2.matshow(norm_conf_mat,cmap=plt.cm.gray)
ax2.set_yticks(np.arange(len(load_label_names())))
ax2.set_yticklabels(load_label_names(),rotation = 45)
ax2.set_xticks(np.arange(len(load_label_names())))
ax2.set_xticklabels(load_label_names(),rotation = 45)
ax2.set_title('Confusion matrix Errors')

How can I save each of these images?

Upvotes: 2

Views: 45

Answers (1)

caverac
caverac

Reputation: 1637

You could try something like this

f1=plt.figure()
ax1=f1.add_subplot(111)
ax1.plot([0, 1], [0, 1])
plt.savefig('foo.png')

f2=plt.figure()
ax2=f2.add_subplot(111)
ax2.plot([0, 1], [1, 0])
plt.savefig('var.png')

it will produce two separate files foo.png and var.png

Upvotes: 2

Related Questions