如何从matplotlib中删除框架(pyplot.figure与matplotlib.figure)(frameon = False在matplotlib中有问题) [英] How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)

查看:492
本文介绍了如何从matplotlib中删除框架(pyplot.figure与matplotlib.figure)(frameon = False在matplotlib中有问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要删除图中的框,我写

frameon=False

pyplot.figure完美搭配,但对于matplotlib.Figure,它仅去除灰色背景,边框保持不变.另外,我只希望显示线条,其余所有图都是透明的.

works perfect with pyplot.figure, but with matplotlib.Figure it only removes the gray background, the frame stays . Also, I only want the lines to show, and all the rest of figure be transparent.

使用pyplot我可以做我想做的事,我想用matplotlib来做,这是有很长的原因的,我不想提起我的问题.

with pyplot I can do what I want, I want to do it with matplotlib for some long reason I 'd rather not mention to extend my question.

推荐答案

首先,如果使用的是savefig,请注意,除非另作说明,否则保存时它将覆盖图形的背景颜色(例如,fig.savefig('blah.png', transparent=True) ).

First off, if you're using savefig, be aware that it will override the figure's background color when saving unless you specify otherwise (e.g. fig.savefig('blah.png', transparent=True)).

但是,要在屏幕上删除轴和图形的背景,您需要将ax.patchfig.patch都设置为不可见.

However, to remove the axes' and figure's background on-screen, you'll need to set both ax.patch and fig.patch to be invisible.

例如

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的白色背景的区别,但是一切都是透明的...)

(Of course, you can't tell the difference on SO's white background, but everything is transparent...)

如果您不想显示线条以外的任何东西,也可以使用ax.axis('off')关闭轴:

If you don't want to show anything other than the line, turn the axis off as well using 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,但是对于单轴来说更简单).

In that case, though, you may want to make the axes take up the full figure. If you manually specify the location of the axes, you can tell it to take up the full figure (alternately, you can use subplots_adjust, but this is simpler for the case of a single axes).

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)

这篇关于如何从matplotlib中删除框架(pyplot.figure与matplotlib.figure)(frameon = False在matplotlib中有问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆