为了去掉图中的框架,我写了
frameon=False
与pyplot完美结合。图,但与matplotlib。图只移除了灰色背景,帧保持不变。此外,我只想显示线条,其余的图形都是透明的。
用pyplot我可以做我想做的事情,我想用matplotlib做一些很长的原因,我宁愿不提扩展我的问题。
为了去掉图中的框架,我写了
frameon=False
与pyplot完美结合。图,但与matplotlib。图只移除了灰色背景,帧保持不变。此外,我只想显示线条,其余的图形都是透明的。
用pyplot我可以做我想做的事情,我想用matplotlib做一些很长的原因,我宁愿不提扩展我的问题。
当前回答
正如我在这里回答的,你可以通过样式设置(样式表或rcParams)从所有的图中删除棘线:
import matplotlib as mpl
mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False
其他回答
在新版本的matplotlib中,摆脱丑陋帧的最简单方法是:
import matplotlib.pyplot as plt
plt.box(False)
如果你真的必须总是使用面向对象的方法,那么执行:ax.set_frame_on(False)。
这里有另一个解决方案:
img = io.imread(crt_path)
fig = plt.figure()
fig.set_size_inches(img.shape[1]/img.shape[0], 1, forward=False) # normalize the initial size
ax = plt.Axes(fig, [0., 0., 1., 1.]) # remove the edges
ax.set_axis_off() # remove the axis
fig.add_axes(ax)
ax.imshow(img)
plt.savefig(file_name+'.png', dpi=img.shape[0]) # de-normalize to retrieve the original size
首先,如果你正在使用savefig,请注意在保存时它将覆盖图形的背景颜色,除非你另有指定(例如fig.savefig('blah.png', transparent=True))。
然而,要在屏幕上删除坐标轴和人物的背景,你需要设置这两个坐标轴。Patch和fig.patch不可见。
E.g.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
for item in [fig, ax]:
item.patch.set_visible(False)
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
(当然,在SO的白色背景下你看不出区别,但一切都是透明的…)
如果你不想显示除直线以外的任何内容,也可以使用ax.axis('off')关闭轴:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
fig.patch.set_visible(False)
ax.axis('off')
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
在这种情况下,你可能想让坐标轴占据整个图形。如果您手动指定坐标轴的位置,您可以告诉它占用整个图形(或者,您可以使用subplots_adjust,但对于单个坐标轴的情况,这更简单)。
import matplotlib.pyplot as plt
fig = plt.figure(frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.plot(range(10))
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
我过去常常这样做:
from pylab import *
axes(frameon = 0)
...
show()
plt.box(False)
plt.xticks([])
plt.yticks([])
plt.savefig('fig.png')
应该能行。