简体   繁体   中英

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

I'd like to save the plot as image but can't because plot immediately displays the plot

I tried

  • %matplotlib (to negate %matplotlib inline I get ModuleNotFoundError: No module named '_tkinter' error)
  • plt.ioff()
  • plt.close('all')
  • matplotlib.pyplot.close(fig)
  • 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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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