'function' object has no attribute 'plot' - python

I was following this tutorial https://www.kaggle.com/residentmario/univariate-plotting-with-pandas
and trying to do the exercise mentioned with the pokemon database but whenever I try to implement the code below I get the error mentioned below and don't understand what to do. I am using matplotlib.use('agg') because I was getting an error related to Tkinter. I am using pycharm, python 3.6 and I am on ubuntu 18.04
Here is my code:
import pandas as pd
import matplotlib
matplotlib.use('agg')
from matplotlib.pyplot import plot
df=pd.read_csv("/home/mv/PycharmProjects/visualization/pokemon.csv")
df['type1'].value_counts.plot(kind='bar')
error
Traceback (most recent call last):
File "/home/mv/PycharmProjects/visualization/univariate plotting.py",
line 9, in <module>
df['type1'].value_counts.plot(kind='bar')
AttributeError: 'function' object has no attribute 'plot'

The error states that df['type1'].value_counts is a function.
To plot the result of the function change:
df['type1'].value_counts.plot(kind='bar')
into
df['type1'].value_counts().plot(kind='bar')

Related

Matplotlib can't find documented function set_cmap

I have the following code:
import matplotlib.pyplot as plt
plt.cm.set_cmap("Blues")
This gives me an error:
Traceback (most recent call last):
File ".\lorenz_explorer.py", line 12, in <module>
plt.cm.set_cmap("Blues")
AttributeError: module 'matplotlib.cm' has no attribute 'set_cmap'
My matplotlib version is 3.3.1, and the function certainly exists in the documentation for 3.3.1: Link
Then am I doing something wrong or is this a bug? Do I need to import matplotlib.cm separately or something along those lines?
As the documentation link you provide shows, the name of the function is matplotlib.pyplot.set_cmap, not matplotlib.pyplot.cm.set_cmap. So you can call it with plt.set_cmap("Blues").
In other words, the function is not part of the cm library, which is somewhat counter-intuitive.

Issue with Pandas and circular reference in import

I am trying to use pandas for the first time and I have copied a very simple program
import pandas as pd
series1 = pd.Series([1,2,3,4])
print(series1)
The issue I am having is that when I try and run the program I am getting the following error
Traceback (most recent call last):
File "C:\Users\faintr\AppData\Local\Programs\Python\Python38-32\pandas.py", line 1, in <module>
import pandas as pd
File "C:\Users\faintr\AppData\Local\Programs\Python\Python38-32\pandas.py", line 2, in <module>
series1 = pd.Series([1,2,3,4])
AttributeError: partially initialized module 'pandas' has no attribute 'Series' (most likely due to a circular import)
Pandas version(1.14.0) is installed.
I have looked into circular imports and whilst I think I understand the concept if this is the cause I do not know how to fix it. I have tried downgrading pandas to an earlier version with no look
Can anyone help please

Why would Plotly not recognise attribute 'offline'?

I'm following the tutorial plotly timeseries, here. I've also amended the code to allow for offline charts in Jupyter, here.
I'm trying to plot a timeseries in Jupyter Notebook. I get the following error.
AttributeError: module 'plotly.plotly' has no attribute 'offline'
As far as I can see I've carried out all the instructions but can't get it to work with the method they suggest.
import plotly.plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode()
data = [go.Scatter(x=dataload.date, y=dataload.spend)]
py.offline.iplot(data)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-c9b2e8d8907c> in <module>()
2 import plotly.graph_objs as go
3
----> 4 py.offline.init_notebook_mode()
5
6 data = [go.Scatter(x=dataload.date, y=dataload.spend)]
AttributeError: module 'plotly.plotly' has no attribute 'offline'
Does anyone have a suggestion to why I might be getting this error, could it be a local setup issue?
Just use:
import plotly
plotly.offline.init_notebook_mode()
don't use: import plotly.plotly as py
You are referring different documentation. Use https://plot.ly/python/getting-started/#initialization-for-offline-plotting
Here it also provides more information regarding how to use help function.
For tutorial in offline mode: https://github.com/SayaliSonawane/Plotly_Offline_Python

Renderer problems using Matplotlib from within a script

I've narrowed down to this call:
fig.canvas.tostring_argb() #fig=matplotlib.pyplot.figure()
this function raises an AttributeError when I run the code as a python script.
AttributeError: 'FigureCanvasGTKAgg' object has no attribute 'renderer'
However, this code works properly if run in the ipython --pylab command line.
As far as I can tell from the documentation, the Agg renderer should work OK.
The context is that I'm trying to make a movie from figures, without saving the frames
to disk; as per this question. I'm using the approach that streams the pixel arrays
to ffmpeg (running as a separate process) to do this, I need the argb array of values from the frame.
Is there some configuration setting I can make to get matplotlib to work correctly from within a script?
Edit
Tried use('Agg') as per a comment; still fails; this is a minimal working example.
[dave#dave tools]$ python -c "import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot; fig=matplotlib.pyplot.figure(); fig.canvas.tostring_argb()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 416, in tostring_argb
return self.renderer.tostring_argb()
AttributeError: FigureCanvasAgg instance has no attribute 'renderer'
I suspect that you have missed out the call to:
fig.canvas.draw()
before
fig.canvas.tostring_argb()
as
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.tostring_argb()
fails for me, but
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.draw()
fig.canvas.tostring_argb()
works.
I ended up installing and using the WXAgg backend; the Agg,and default GTKAgg, didn't work for me.

AttributeError: 'module' object (scipy) has no attribute *** Why does this error occur?

In scipy, the error occurs quite often.
>>> import scipy
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'integrate'
>>>
I figure out how to solve this problem by doing the following:
>>>
>>> import scipy.integrate
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
>>> 1.2
My question:
Why does the error occur?
Why would that fix the error?
Most possibly because scipy is a library (package) that contains modules and to import a specific module from the scipy library, you need to specify it and import the module itself. As it's a separate module (sub-package), once you import it, it's attributes are available to you by using the regular scipy.module.attribute
In order to fix the error, add the following line at the top of your script
from scipy import integrate
Just simply use
import scipy.constants as spc
and then
C = spc.c #speed of light m/s
pi = spc.pi

Categories