criticalth
criticalth

Reputation: 438

box around entire plot

I have the following code:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.savefig('test.png')

which produces the test png:

enter image description here

I would like for that png to have borders (a box) around the entire plot including the labels of the axis. Could not find it in the doc.

The aimed results should look more like the following plot:

enter image description here

I need the exported image to have these borders. Thank you

Upvotes: 0

Views: 403

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

plt.gcf().patch.set_edgecolor('k')
plt.gcf().patch.set_linewidth(3)

enter image description here

When saving to file though, the figure border is reverted to the value set in the rcParam 'savefig.edgecolor'. Therefore, to get the border showing in the saved file, this parameter need to be modified:

plt.rcParams['savefig.edgecolor'] = plt.gcf().patch.get_edgecolor()
plt.savefig('test.png')

Upvotes: 1

Related Questions