AttributeError: module 'matplotlib' has no attribute 'plot' - python

I am using python 3.6 and a learner. Below is a simple code of a sin wave.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10 , 10, 100)
y = np.sin(x)
plt.plot(x, y, marker="x")
I am receiving the error "AttributeError: module 'matplotlib' has no attribute 'plot'" Any help would be appreciated.

Have you installed matplotlib properly? I added an extra line to your code to show the plot. This code works properly in Visual Studio after installing the matplotlib library.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10 , 10, 100)
y = np.sin(x)
plt.plot(x, y, marker="x")
plt.show()

Try this simple step.
use pyplot using below import statement when importing matplotlib library.
import matplotlib.pyplot as plt

Related

Using Matplotlib for plotting 3D data - warning/error importing module [duplicate]

When using the typical 3D plot like so:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
flake8 reports the expected error:
./tools.py:62:9: F401 'mpl_toolkits.mplot3d.Axes3D' imported but unused
I know it can be avoided using the # NOQA comment. But is there a different way to formulate the projection in the figure so that the Axes3D object is used?
If this is only about actually using the import at least once, you can do
ax = fig.gca(projection=Axes3D.name)
as "3d" is the name of the Axes3D class by which it is registered to the projection list.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
However, if I understand the documentation well, it is not the preferred way anymore since version 1.0.0. I still mention it for completeness.

Exporting SVG files from plots created in Python

So I'm trying to export some plots I created using matplotlib and seaborn.
I create my plot with:
import pandas as pd
import matplotlib as plt
import matplotlib.pyplot as plt
from matplotlib import pyplot as plty
import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
df = pd.read_excel('test.xlsx', sheetname='IvT')
sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
If I try to export it with
matplotlib.pyplot.savefig("test.svg", format="svg")
I get an error message saying
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) in ()
1
----> 2 matplotlib.pyplot.savefig("test.svg", format="svg")
NameError: name 'matplotlib' is not defined
You seem to be overcomplicating the importing of modules. In the code, you have imported matplotlib and matplotlib.pyplot as plt. In addition, having already imported matplotlib.pyplot, you try and do it again using from matplotlib import pyplot
When you try and save your file you have then done matplotlib.pyplot.savefig, but you have already imported matplotlib.pyplot as plt.
The specific error you have shown is because, while you have import matplotlib itself, you have imported it as plt, which is why the error says that matplotlib is not defined.
In order to fix this, you need to clean up the imports like so:
import pandas as pd
import matplotlib # if abbreviating this, use "as mpl"
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
df = pd.read_excel('test.xlsx', sheetname='IvT')
sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
Then in order to save your figure use:
plt.savefig("test.svg", format="svg")
Remember to call this before plt.show()

Error while building a network graph using matplotlib

File "C:/Users/Vrushab PC/Downloads/Dissertation/untitled0.py", line 125, in
matplotlib.rcParams['figure.figsize'] = (50, 50)
NameError: name 'matplotlib' is not defined
I have imported matplotlib.pyplot but still getting this? what could be the reason?
Try this:
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (50, 50)
From matplotlib docs.
You are importing pyplot framework from matplotlib(a part of matplotlib), you don't have access to the full matplotlib library until you do the above import as mpl. Now, you can use mpl alias to access rcParams.

How to directly use Axes3D from matplotlib in standard plot to avoid flake8 error

When using the typical 3D plot like so:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
flake8 reports the expected error:
./tools.py:62:9: F401 'mpl_toolkits.mplot3d.Axes3D' imported but unused
I know it can be avoided using the # NOQA comment. But is there a different way to formulate the projection in the figure so that the Axes3D object is used?
If this is only about actually using the import at least once, you can do
ax = fig.gca(projection=Axes3D.name)
as "3d" is the name of the Axes3D class by which it is registered to the projection list.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
However, if I understand the documentation well, it is not the preferred way anymore since version 1.0.0. I still mention it for completeness.

Python "'module' object is not callable"

I'm trying to make a plot:
from matplotlib import *
import sys
from pylab import *
f = figure ( figsize =(7,7) )
But I get this error when I try to execute it:
File "mratio.py", line 24, in <module>
f = figure( figsize=(7,7) )
TypeError: 'module' object is not callable
I have run a similar script before, and I think I've imported all the relevant modules.
The figure is a module provided by matplotlib.
You can read more about it in the Matplotlib documentation
I think what you want is matplotlib.figure.Figure (the class, rather than the module)
It's documented here
from matplotlib import *
import sys
from pylab import *
f = figure.Figure( figsize =(7,7) )
or
from matplotlib import figure
f = figure.Figure( figsize =(7,7) )
or
from matplotlib.figure import Figure
f = Figure( figsize =(7,7) )
or to get pylab to work without conflicting with matplotlib:
from matplotlib import *
import sys
import pylab as pl
f = pl.figure( figsize =(7,7) )
You need to do:
matplotlib.figure.Figure
Here,
matplotlib.figure is a package (module), and `Figure` is the method
Reference here.
So you would have to call it like this:
f = figure.Figure(figsize=(7,7))
To prevent future errors regarding matplotlib.pyplot, try doing this:
import matplotlib.pyplot as plt
If you use Jupyter notebooks and use: %matplotlib inline, make sure that the "%matplotlib inline FOLLOWS the import matplotlib.pyplot as plt
Karthikr's answer works but might not eliminate errors in other lines of code related to the pyplot module.
Happy coding!!
Instead of
from matplotlib import *
use
import matplotlib.pyplot as plt
For Jupyter notebook I solved this issue by importig matplotlib.pyplot instead.
import matplotlib.pyplot as plt
%matplotlib notebook
Making plots with f = plt.figure() now works.
Wrong Library:
import matplotlib as plt
plt.figure(figsize=(7, 7))
TypeError: 'module' object is not callable
But
Correct Library:
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 7))
Above code worked for me for the same error.

Categories