简体   繁体   English

Matplotlib,防止plot显示

[英]Matplotlib, prevent plot from showing

df_rm_count[['count']].sort_values(by='count', ascending=False).plot.barh()

The above line shows the graph (without plt.show() )上面的行显示了图表(没有plt.show()

I'd like to save the plot as image but can't because plot immediately displays the plot我想将 plot 保存为图像,但不能因为plot立即显示 plot

I tried我试过了

  • %matplotlib (to negate %matplotlib inline I get ModuleNotFoundError: No module named '_tkinter' error) %matplotlib (要否定%matplotlib inline我得到ModuleNotFoundError: No module named '_tkinter'错误)
  • plt.ioff() plt.ioff()
  • plt.close('all') plt.close('全部')
  • matplotlib.pyplot.close(fig) matplotlib.pyplot.close(图)
  • matplotlib.interactive(False) matplotlib.interactive(False)

none of the above worked..以上都没有工作..

  • edit编辑

Agg didn't work either..阿格也没用。。

import matplotlib as mpl
mpl.use('Agg')

ipykernel_launcher.py:2: UserWarning:  This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called
*before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.

You can use a different backend that cannot display a figure, for example Agg您可以使用无法显示图形的其他后端,例如Agg

import matplotlib as mpl
mpl.use('Agg')

In order to not restart the kernel, put plt.switch_backend('Agg') right below the lines that create fig and ax , for example为了不重新启动 kernel,请将plt.switch_backend('Agg')放在创建figax的行下方,例如

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)
plt.switch_backend('Agg')

df = pd.DataFrame()
df['a'] = [1,2,3]
df['a'].plot.barh(ax=ax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM