Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot is not defined

Tags:

I started to use matplotlib library to get a graph. But when I use "plot(x,y)" it returns me that "plot is not defined".

To import , I used the following command:

from matplotlib import *

Any Suggestions?

like image 680
Franco Hh Avatar asked Mar 02 '12 11:03

Franco Hh


People also ask

Why is PLT not defined Python?

The Python "NameError: name 'plt' is not defined" occurs when we use the pyplot module without importing it first. To solve the error, install matplotlib and import plt ( import matplotlib. pyplot as plt ) before using it. Open your terminal in your project's root directory and install the matplotlib module.

What does plot () do in Python?

The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram.

Why is PLT plot blank?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt.

Has no attribute plot?

It means that there is an issue with syntax or we say that there is a syntax error. Solution: This error was raised in the above example because the syntax we used to import the matplotlib library was incorrect.


2 Answers

Change that import to

from matplotlib.pyplot import *

Note that this style of imports (from X import *) is generally discouraged. I would recommend using the following instead:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
like image 123
NPE Avatar answered Sep 30 '22 17:09

NPE


If you want to use a function form a package or module in python you have to import and reference them. For example normally you do the following to draw 5 points( [1,5],[2,4],[3,3],[4,2],[5,1]) in the space:

import matplotlib.pyplot
matplotlib.pyplot.plot([1,2,3,4,5],[5,4,3,2,1],"bx")
matplotlib.pyplot.show()

In your solution

from matplotlib import*

This imports the package matplotlib and "plot is not defined" means there is no plot function in matplotlib you can access directly, but instead if you import as

from matplotlib.pyplot import *
plot([1,2,3,4,5],[5,4,3,2,1],"bx")
show()

Now you can use any function in matplotlib.pyplot without referencing them with matplotlib.pyplot.

I would recommend you to name imports you have, in this case you can prevent disambiguation and future problems with the same function names. The last and clean version of above example looks like:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[5,4,3,2,1],"bx")
plt.show()
like image 32
pacodelumberg Avatar answered Sep 30 '22 16:09

pacodelumberg