简体   繁体   中英

matplotlib and backend 'agg' result in plot not showing up

I'm trying to use matplotlib with Pycharm Community Ed. 2019.3, Python 3.7.3, Ubuntu 19.04.

import matplotlib
import matplotlib.pyplot as plt

plt.figure()
plt.plot(3,2,'.')
plt.show()

But get the following error code: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."

I tried changing the backend to 'NbAgg':

import matplotlib
matplotlib.use('NbAgg')
import matplotlib.pyplot as plt

plt.figure()
plt.plot(3,2,'.')
plt.show()

But get the following error code:

 Traceback (most recent call last):
  File "/home/leo/.PyCharmCE2019.3/config/scratches/scratch_1.py", line 7, in <module>
    plt.show()
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 269, in show
    return _show(*args, **kw)
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 261, in show
    manager.show()
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 90, in show
    self._create_comm()
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 122, in _create_comm
    self.add_web_socket(comm)
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py", line 432, in add_web_socket
    self.resize(w, h)
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py", line 418, in resize
    size=(w / self.canvas._dpi_ratio, h / self.canvas._dpi_ratio))
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py", line 489, in _send_event
    s.send_json(payload)
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 199, in send_json
    self.comm.send({'data': json.dumps(content)})
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/ipykernel/comm/comm.py", line 123, in send
    data=data, metadata=metadata, buffers=buffers,
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/ipykernel/comm/comm.py", line 66, in _publish_msg
    self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/_pylab_helpers.py", line 73, in destroy_all
    manager.destroy()
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 126, in destroy
    self._send_event('close')
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_webagg_core.py", line 489, in _send_event
    s.send_json(payload)
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/matplotlib/backends/backend_nbagg.py", line 199, in send_json
    self.comm.send({'data': json.dumps(content)})
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/ipykernel/comm/comm.py", line 123, in send
    data=data, metadata=metadata, buffers=buffers,
  File "/home/leo/PycharmProjects/Coursera/venv/lib/python3.7/site-packages/ipykernel/comm/comm.py", line 66, in _publish_msg
    self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'

I understand that the backend and showing the plots are incompatible, but I don't understand what I need to do to match the backend and showing the plots.

I was able to change the backend to 'TkAgg', but I had to install tk via the command line using:

sudo apt-get install python3-tk

However, still need to use plt.show() for the graph to pop up

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