I can't figure out how to add a border around a figure, it's my understanding that this would be the figure.edgecolor parameter or savefig(edgecolor) but this does not appear to work. I'm using matplotlib 1.1.1. I would expect this code to draw a red border around the figure:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.savefig('test.png', edgecolor='r', facecolor='g')
When I look at the figure it has a green facecolor, but I don't see a red edgecolor?
The following doesn't work either:
import matplotlib.pyplot as plt
plt.figure(edgecolor='r', facecolor='g')
plt.plot([1,2,3])
plt.show()
Again I see the green facecolor, but no red edgecolor. What am I doing wrong?
Any ideas?
savefig() As the name suggests savefig() method is used to save the figure created after plotting data. The figure created can be saved to our local machines by using this method.
plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures. The plt. show() command does a lot under the hood, as it must interact with your system's interactive graphical backend.
According to the documentation of matplotlib.figure.Figure()
, the figure's edge linewidth
is set to 0.0
by default. You can visualize the edgecolor
if you bump up this value in either of your code snippets:
import matplotlib.pyplot as plt
plt.figure(linewidth=2)
plt.plot([1,2,3])
plt.savefig('test.png', edgecolor='r', facecolor='g')
Or:
import matplotlib.pyplot as plt
plt.figure(edgecolor='r', facecolor='g', linewidth=2)
plt.plot([1,2,3])
plt.show()
linewidth=0.0
is a good default, but it should be better documented in matplotlib.pyplot.savefig().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With