Basemap error: module object is not callable - python

I am trying to learn how to use basemap in python. I used the following site for learning http://www.datadependence.com/2016/06/creating-map-visualisations-in-python/.
but when I typed the following
import matplotlib.pyplot as plt
import matplotlib.cm
import basemap
fig,ax=plt.subplots(figsize=(10,20))
m=basemap(resolution='c',projection='merc',lat_0=54.5,lon_0=-4.36,llcrnrlon=-6.,llcrnrlat=49.5,urcrnrlon=2.,urcrnrlat=55.2)
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
m.drawcoastlines()
I get an error as TypeError: 'module' object is not callable. Why is the reason for this?

You misunderstood the example code. You have to write:
from mpl_toolkits.basemap import Basemap
m=Basemap(resolution='c',projection='merc',lat_0=54.5,lon_0=-4.36,llcrnrlon=-6.,llcrnrlat=49.5,urcrnrlon=2.,urcrnrlat=55.2)
Basemap have to be start from capital letter. It is very important for Python. Python is case sensitivity language.

Related

AttributeError: module 'matplotlib' has no attribute 'bar'

I am using python 3.6 and am a learner. Below is a simple code of a sin wave.
import numpy as np
import pandas as pd
import matplotlib as plt
import seaborn as sb
smartphone = pd.read_csv("C://Users/Admin/smartphones.csv")
count = smartphone.Ram.value_counts()
category = count.index
plt.bar(category,count)
plt.xlable('Ram')
plt.ylable('Count')
plt.xticks([1,2,3,4])
plt.yticks([1,2,3])
plt.show()`
I am receiving the error "AttributeError: module 'matplotlib' has no attribute 'bar'" Any help would be appreciated.
When you see other people's code and they have plt.bar(x,y) or plt.show() the plt they are referring to is a sub-module of the matplotlib package called pyplot.
So this is really what's going on:
import matplotlib
matplotlib.pyplot.bar(count, category)
...
...
...
matplotlib.pyplot.show()
So if you are mainly using pyplot what you want to do at the top of your script is,
import matplotlib.pyplot as plt
Then you can just:
plt.bar(count,category)
...
...
...
plt.show()
edit: Also just wanted to add, that I think you have a typo and have xlable and ylable, but it should be xlabel and ylabel. Your code will throw errors if you don't fix that.
Try using import matplotlib.pyplot as plt instead of import matplotlib as plt.

'function' object has no attribute 'fft2' error scipy

I am struggling with an error called 'function' object has no attribute 'fft2'. First of all,
I have imported the following:
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import fft
After executing the following block of code in jupyter notebook:
gainDisplay=1
InImgFFT=fft.fft2(InImg,norm="ortho")
InImgAmplSpec=np.abs(fft.fftshift(InImgFFT))
plt.figure(figsize=(W/DPI+1,H/DPI+1))
plt.imshow(np.clip(InImgAmplSpec*gainDisplay,0,255),cmap = 'rainbow')
plt.suptitle('The amplitude spectrum of the gray scale input image')
plt.show()
I get the error mentioned at the line with fft.fft2() function
Could you please help me to solve this error?
scipy version is 1.5.2
Another mention would be that I have a conda environment where i have installed all the packets, but I hope that won't be a problem.

backend_qt5.py "'figure' is an unknown keyword argument, In matplotlib

I am trying to run a simple code to plot my data using matplotlib in python2.7.10 :
import matplotlib.pyplot as plt
y=[23,35,43,54,76]
x=[2,4,5,6,7]
plt.plot(y,x)
I am getting the error:
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.py", line 239, in __init__
super(FigureCanvasQT, self).__init__(figure=figure)
TypeError: 'figure' is an unknown keyword argument
How can I fix it?
This seems to be a duplicate of: matplotlib Qt5Agg backend error: 'figure' is an unknown keyword argument, which I just posted an answer for, and duplicated below:
I had the same issue. I found the solution here
Specifically, the following now works:
import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib import pyplot as plt
plt.figure(figsize=(12,8))
plt.title("Score")
plt.show()
Just adding to Marc's answer. If you are using Spyder,
matplotlib.use('Qt4Agg')
may not work well because matplotlib has been imported when you open Spyder.
Instead you can go to (in Spyder) Tools - Preferences -IPython console - Graphics to change the backend and restart Spyder.
adding to this, i ran into this problem when i wanted to plot my values for my KNN training sets and test set results.
using TkAgg also fixes the problem
"matplotlib.use('TkAgg')"
import matplotlib
matplotlib.use('TkAgg')
#matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.title("Score")
plt.show()

Python module not callable

I searched many posts, but they don't seem to be helpful.
In folder dir1/ I have main.py and plotcluster.py. In plotcluster.py I have:
import matplotlib as plt
import itertools as it
....
def plotc():
colors = it.cycle('ybmgk')
....
plt.figure()
....
In main.py, I use plotcluster.py:
import plotcluster as plc
....
plc.plotc()
But this gives me a error saying module object is not callable.
20 linestyles = it.cycle('-:_')
21
---> 22 plt.figure()
23 # plot the most frequent ones first
24 for iter_count, (i, _) in enumerate(Counter(centerid).most_common()):
TypeError: 'module' object is not callable
It doesn't complaint about the itertools module, but the plt one bothers it. This makes me so confusing!
any help will be appreciated !! Thanks in advance!
#suhail 's answer will work. Essentially you were accessing matplotlib.figure which is the module. Also I think you are trying to access the pyplot functions (gen that is imported as plt) and its enough to import that module to access most of the standard plotting api's.
So in your plotcluster.py change the first line to
import matplotlib.pyplot as plt
It should be smooth sailing from there on and you can use stuff like
plt.plot(), plt.show() and so on.
try
plt.figure.Figure()
not
plt.figure

Use pylab to plot image returned from Scipy

I'm working to migrate from MatLab to python in Sage.
So I use these commands and I faced this error in Sage:
from scipy import misc
l = misc.lena();
import pylab as pl
pl.imshow(l)
The Error or message (i don't know) is:
matplotlib.image.AxesImage object at 0xb80198c
And it doesn't show any image
It's not an error, just print the object that method returned.
There are two ways to show the figure:
Add pl.show() after calling pl.imshow(l)
Use ipython --pylab to open your python shell,
That is an object being returned from pylab after using the "imshow" command. That is the location of the Axes image object.
documentation:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow
Looks like it says it displays the object to the current axes. If you havent already created a plot I imagine you wont see anything
Simple google search suggests this might be what you are looking for
http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.lena.html
from scipy import misc
l = misc.lena();
import pylab as pl
pl.imshow(l)
####use this
pl.show()

Categories