Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib can not see the effect of setting edgecolor in plt.savefig() or plt.figure()

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?

like image 716
bobl2424 Avatar asked Apr 29 '13 22:04

bobl2424


People also ask

What does Fig Savefig do?

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.

What does PLT show () do?

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.


1 Answers

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().

like image 149
fgb Avatar answered Oct 30 '22 23:10

fgb