5

I have a strange problem with matplotlib under Eclipse I've tested this code and this works fine in the command line:

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

But when I execute this code under Eclipse, there is nothing shown. I see that pydev understood all my imports and there is no error message.

nam
  • 3,542
  • 9
  • 46
  • 68
  • This anwsered my question http://stackoverflow.com/questions/7534453/matplotlib-does-not-show-my-drawings-although-i-call-pyplot-show – nam Nov 20 '12 at 13:37
  • If you found an answer, you should answer and accept your own answer. This makes it easier both for people looking to answer questions (as they can skip questions that are answered) and people looking for answers (as they might not click on questions (even if they seem relevant) that have no responses). – tacaswell Nov 21 '12 at 15:47
  • because it is a comment. Use the big text box at the bottom of the page. (there should be a button that says 'Post Your Answer' just below it) – tacaswell Nov 21 '12 at 15:57

3 Answers3

1

Check your Matplotlib backend: Matplotlib can use different "backends", which are like engines that handle rendering the plot and interfacing with the operating system. You might be using a backend that does not support interactivity or displaying plots. You can set the backend to "TkAgg", for example, which is generally good for interactive use:

import matplotlib
matplotlib.use('TkAgg')  # put this before importing pyplot
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

Be sure to place matplotlib.use('TkAgg') before importing pyplot. Also note that this solution requires the Tkinter package to be installed in your Python environment.

Ensure interactive mode is on: Matplotlib's interactive mode allows you to update a figure after it's been shown. If interactive mode is off, the system might just be waiting for more commands and not showing the plot. Enable interactive mode as follows:

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.ion()  # turn on interactive mode
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

Try using plt .draw() or plt.pause(): Sometimes, you need to explicitly tell Matplotlib to redraw the figure, or to pause for a moment to ensure that the plot has time to display:

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.draw()
plt.pause(0.001)  # pause for a short moment to allow the plot to display

Please try these solutions and see if any of them resolve your issue.

0

using Ubuntu 12.04 and i have installed python3.3 This is what worked for me.

You need to have freetype. sudo apt-get install freetype*

You need to have freetype. sudo apt-get install python3.3-dev

You need to have g++. sudo apt-get install g++

You need to have libevent-dev sudo apt-get install libevent-dev

You need to have libpng-dev sudo apt-get install libpng-dev

You need to have libjpeg8-dev sudo apt-get install libjpeg8-dev

You need to have python3.3-tk sudo apt-get install python3.3-tk

You need to have tk-dev sudo apt-get install tk-dev

You need to have python-gtk2-dev sudo apt-get install python-gtk2-dev

Get the tar file from the website and untar it in the dist-packages /usr/local/lib/python3.3/dist-packages/matplotlib-1.3.1. sudo python3.3 setup.py clean (this might be optional) sudo python3.3 setup.py install

NOTE: I had to change the matplotlibrc file in the dist-packages in the mpl-data directory. The backend line was changed from agg to TkAgg and interactive was set to True.

0

In my case, the Python Interpreters doesn't recognize matplotlib installing. Try to apply Python Interpreters again.

Window -> Preferences -> PyDev -> Interpreters -> Python Interpreters -> Apply -> Select python interpreters -> OK -> Apply and Close.

Hope it works.

DungNH
  • 344
  • 2
  • 9