Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import matplotlib in python

I am new to python and I am working on a graph problem and I want to draw this graph to have a better understanding of it. I learnt that matplotlib module is supposed to be imported for this but I dont know how to add it to the project.(I am a java developer and It is pretty much like adding jar to your classpath)

When I try doing

import matplotlib

I get the following error:

File "/Library/Python/2.7/site-packages/networkx-1.7rc1-py2.7.egg/networkx/drawing/nx‌​_pylab.py", line 114, in draw
    raise ImportError("Matplotlib required for draw()")
ImportError: Matplotlib required for draw()
ImportError: No module named matplotlib.pyplot

Can anyone help me with that?Do I need to download anything to make it run as in the module?

like image 505
Dude Avatar asked Aug 05 '12 10:08

Dude


People also ask

Why do we import matplotlib Pyplot in Python?

import matplotlib. pyplot as plt gives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.

Can I use matplotlib in Python?

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible. Create publication quality plots. Make interactive figures that can zoom, pan, update.


1 Answers

module: new

As David Robinson points out in a comment on another answer you may have posted an incomplete error message, in which case it is possible that the inbuilt module new is being shadowed by new.py in the gis module. - if that is the case the suggested fix is to rename the gis version.

Incomplete Matplotlib install

Alternatively, given your output you may be trying to import networkx and you don't seem to have matplotlib (correctly) installed.

Could you make sure that matplotlib is correctly installed, either if you're on Ubuntu by using

sudo apt-get install python-matplotlib

or if you prefer pip or easy_install,

pip install matplotlib

or

easy_install matplotlib
like image 59
jmetz Avatar answered Sep 19 '22 14:09

jmetz