AttributeError: 'module' object has no attribute 'hist' - python

I'm new to Python (and am using Spyder) and am trying to create some histograms of when the top movies from IMDB were created. I have imported the matplotlib, numpy and pandas, as well as the .txt file, but when I run the following lines of code:
plt.hist(data.year, bins=np.arange(1950, 2013), color='#cccccc')
I receive an error message:
Traceback (most recent call last):
File "stdin", line 1, in <module>
AttributeError: 'module' object has no attribute 'hist'
What am I doing wrong?

Your question provide very poor information and insight in your code. More data..
Meanwhile check if you actually import your modules correctly, should have:
import matplotlib.pyplot as plt
in order to use hist function

Related

rioaxrray open netcdf file result is a list not an xarray

I am trying to open a netcdf file using rioarray:
import rioxarray
import xarray
import raster
xds = rioxarray.open_rasterio(file, crs='+proj=latlong', masked=True)
but:
type(xds)
list
and xds has none of the attributes or methods of an xarray.
xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError Traceback (most recent call last)
in
----> 1 xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError: 'list' object has no attribute 'rio'
clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError Traceback (most recent call last)
in
----> 1 clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError: 'list' object has no attribute 'rio'
Can anyone advise?
I recently encountered this when I was opening a netCDF (with rioxarray) that had multiple variables. Since it returns a list, you would not expect it to have any of the rioxarray attributes or methods.
The documentation for the function is here: https://corteva.github.io/rioxarray/stable/rioxarray.html
One of the return types is List[xarray.Dataset], so I think this behavior is expected.
My guess is that you want one of the entries in the list like xds=xds[0], though it's hard to know without having more information about the file that you are opening.

Error when accessing synonyms in python using nltk?

I have written a very simple piece of code to try and print the synonyms associated with a word.
import nltk
from nltk.corpus import wordnet as wn
wordNetSynset = wn.synsets('small')
for synSet in wordNetSynset:
for synWords in synSet.lemma_names:
synonymList.add(synWords)
print synonymList
However, I get the following error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
for synWords in synSet.lemma_names:
TypeError: 'instancemethod' object is not iterable
Does anyone know what the problem could be?
In Nltk 3, the lemma_names has been changed to a method from an attribute.
So you have to call the method
for synWords in synSet.lemma_names():
Other minor changes required are:
synonymList is not defined
List will not have an add method even if synonymList is defined
You better name your variable synonymSet

matplotlib layout like plotly by hand

I am plotting with matplotlib and trying to get a nice layout like plotly. Especially I would like to have a bar chart/box plot with filling and contour in the same colour. Not as default black/grey contour and coloured contour but like plotly. Any suggestions without stylesheet?
The following is solved:
Because by searching for the solution I found style sheets, but I get the following error message trying to load specific stylesheets in python matplotlib following the official manual:
>>> import matplotlib.pyplot as plt
>>> plt.style.use('ggplot')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'style'
same problem with:
>>> print plt.style.available
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'style'
Information about the system:
>>> matplotlib.__version__
'1.3.1'
So it seems that something is wrong with my installation, but what can I do? Any suggestions?
As shown in the release notes, the style package was added in matplotlib 1.4. You will need to upgrade if you want to use that.

AttributeError: 'list' object has no attribute 'updateItem'

I am working on map automation using arcpy.
I need to add a legend on the map layout based on the layers added to the mxd.I am using the code below (as given on the tutorial):
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.AddLayer(df, lyrFile, "TOP")
styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "NewDefaultLegendStyle")[0]
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.updateItem(lyrFile, styleItem)
But everytime I run this code i get the following error:
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'list' object has no attribute 'updateItem'
What could cause this error to appear?
What could cause this error to appear?
Well, I am not familiar with arcpy, but it seems the 0th element of whatever ListLayoutElements() returns is a list which indeed has no updateItem() method.
You might want to .append() to the list, or you might want to have a different type of object.
Your code is the same as ArcGIS Help example,
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//00s30000006z000000
I tested the example code and it ran correctly.
By the way, I am wondering if you had pasted your own code. Otherwise you probably encounter problem in line 2,4,6 rather than the last line.
As the user2357112 suggested, you'd better try it again with clean code. Or you can confirm the type of the variable "legend" just by print type(legend)before the line
legend.updateItem(lyrFile, styleItem)

Python: Thermopy module "AttributeError: 'module' object has no attribute 'burcat'"

I've just had to try and teach myself python for a project at work and it isn't going so well.
I'm trying to use the module "Thermopy" so that I can make programs for calculating gas dynamics etc.
I thought I had installed Thermopy correctly but when I went to just have a quick test of it, I couldn't get it to work, it imports the module fine and help(thermopy) seems to show it importing the right file (I think?) but If I then try and call any of the classes that should be in the module I get the error AttributeError: 'module' object has no attribute 'psicrometry' (Or whichever class I'm trying to call
e.g.
import thermopy
help(thermopy)
print thermopy.__file__
thermopy.psicrometry.test_wark()
gives the result
Help on package thermopy:
NAME
thermopy
FILE
c:\python27\lib\site-packages\thermopy-0.3-py2.7.egg\thermopy\__init__.py
PACKAGE CONTENTS
burcat
codata
combustion
constants
iapws
psicrometry
units
DATA
__version__ = '0.3'
VERSION
0.3
C:\Python27\lib\site-packages\thermopy-0.3-py2.7.egg\thermopy\__init__.pyc
Traceback (most recent call last):
File "\\FGBD101000.wsatkins.com\GBBSB_Home$\BROW4631\My Documents\Python\thermopy testing", line 7, in <module>
thermopy.psicrometry.test_wark()
AttributeError: 'module' object has no attribute 'psicrometry'
Any help on this is greatly appreciated, I'm very new to all this and don't have any prior programming experience so apologies if I'm doing anything silly, but I couldn't understand any of the other answers I saw to similiar questions on the site.
Many thanks
James

Categories

Resources