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

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

问题描述

要去掉图中的边框,我写

To remove frame in figure, I write

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天全站免登陆