matplotlib has no attribute 'pyplot' - python

I can import matplotlib but when I try to run the following:
matplotlib.pyplot(x)
I get:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
matplotlib.pyplot(x)
AttributeError: 'module' object has no attribute 'pyplot'

pyplot is a sub-module of matplotlib which doesn't get imported with a simple import matplotlib.
>>> import matplotlib
>>> print matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pyplot'
>>> import matplotlib.pyplot
>>>
It seems customary to do: import matplotlib.pyplot as plt at which time you can use the various functions and classes it contains:
p = plt.plot(...)

Did you import it? Importing matplotlib is not enough.
>>> import matplotlib
>>> matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pyplot'
but
>>> import matplotlib.pyplot
>>> matplotlib.pyplot
works.
pyplot is a submodule of matplotlib and not immediately imported when you import matplotlib.
The most common form of importing pyplot is
import matplotlib.pyplot as plt
Thus, your statements won't be too long, e.g.
plt.plot([1,2,3,4,5])
instead of
matplotlib.pyplot.plot([1,2,3,4,5])
And: pyplot is not a function, it's a module! So don't call it, use the functions defined inside this module instead. See my example above

Related

AttributeError: 'SearchEngine' object has no attribute 'ses'

I am trying to use the zipcodes package but was getting a ses error. Here is a brief snippt of my code.
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
import geopandas as gpd
import requests
import zipfile
import io
import os
import pyproj
from shapely.ops import transform
from functools import partial
import json
from uszipcode import SearchEngine
search = SearchEngine(simple_zipcode=True)
I am getting the following error
Exception ignored in: <function SearchEngine.__del__ at 0x7fcd6087eca0>
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.8/site-packages/uszipcode/search.py", line 195, in __del__
if self.ses:
AttributeError: 'SearchEngine' object has no attribute 'ses'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-325ac6f8df7d> in <module>
----> 1 search = SearchEngine(simple_zipcode=True)
2 search.ses.scalar(SimpleZipcode)
TypeError: __init__() got an unexpected keyword argument 'simple_zipcode'
Any pointers?

dir(sns) not listing plt

I am trying to use `dir(sns.plt) but this results in an error:
dir(sns.plt)
Traceback (most recent call last):
File "<ipython-input-3-8072046646b0>", line 1, in <module>
dir(sns.plt)
AttributeError: module 'seaborn' has no attribute 'plt'
How do get 'plt' to show up or add it?
Please review this posting for more information on plt used with seaborn:
Seaborn plots not showing up

matplotlib error: from inspect import Parameter ImportError: cannot import name 'Parameter'

I just installed matplotlib using Anaconda because I need to plot a graph. I just imported it and literally I wrote:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.suptitle("Error rates / tree depth")
fig, ax_lst = plt.subplots(2,2)
When I run the file, it says:
Traceback (most recent call last):
File "/Users/xxxxxxxx/Documents/xxxxxxx/ASSIGNMENTS/hw2/handout/decisionTree_working_not_cleaned.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/Users/xxxxxxxxxxx/anaconda3/envs/ML35/lib/python3.5/site-packages/matplotlib/__init__.py", line 125, in <module>
from inspect import Parameter
ImportError: cannot import name 'Parameter'
Any help?
Thanks!

AttributeError: '_process_plot_var_args' object has no attribute 'get_next_color'

I have tried to run the following code. but it gives an argument required error in lifelines/plotting.py file. i can't fix it .
import pandas as pd
from lifelines.datasets import load_dd
import matplotlib.pyplot as plt
data = load_dd()
print data.sample(6)
from lifelines import KaplanMeierFitter
kmf = KaplanMeierFitter()
T = data["duration"]
E = data["observed"]
kmf.fit(T, event_observed=E)
kmf.survival_function_.plot()
plt.title('Survival function of political regimes');
kmf.plot()
plt.show()
but it gives the following error
Traceback (most recent call last): File "/Users/rabindra/PycharmProjects/SurvivalAnalysis/sources/main.py", line 17, in <module>
kmf.plot() File "/Library/Python/2.7/site-packages/lifelines/plotting.py", line 331, in plot
set_kwargs_color(kwargs) File "/Library/Python/2.7/site-packages/lifelines/plotting.py", line 223, in set_kwargs_color
kwargs["ax"]._get_lines.get_next_color()) AttributeError: '_process_plot_var_args' object has no attribute 'get_next_color'
I was facing the same issue.
Upgraded lifelines to 0.14.0 and matplotlib to 2.2.2 and it works.

How to bring back library functions which have been deleted?

Suppose I do this
import cmath
del cmath
cmath.sqrt(-1)
I get this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cmath' is not defined
But when I import cmath again, I am able to use sqrt again
import cmath
cmath.sqrt(-1)
1j
But when I do the following
import cmath
del cmath.sqrt
cmath.sqrt(-1)
I get this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sqrt'
Even when I import cmath again, I get the same error.
Is it possible to get cmath.sqrt back?
Thanks!
You'd need reload
reload(cmath)
... will reload definitions from the module.
import cmath
del cmath.sqrt
reload(cmath)
cmath.sqrt(-1)
... will correctly print ..
1j

Categories