Python module not callable - python

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

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.

AttributeError: 'FITSFigure' object has no attribute 'set_tick_labels_font'

I am a beginner in programming. I am trying to produce two plots using APLpy and subplot and simple code.
The code is as following:
import matplotlib
matplotlib.use('Agg')
import aplpy
import matplotlib.pyplot as mpl
fig = mpl.figure(figsize=(15, 7))
f1 = aplpy.FITSFigure('snr.5500-drop.fits', figure=fig, subplot=[0.1,0.1,0.35,0.8])
f1.set_tick_labels_font(size='x-small')
f1.set_axis_labels_font(size='small')
f1.show_grayscale()
f2 = aplpy.FITSFigure('snr.2100-drop.fits', figure=fig, subplot=[0.5,0.1,0.35,0.8])
f2.set_tick_labels_font(size='x-small')
f2.set_axis_labels_font(size='small')
f2.show_grayscale()
f2.hide_yaxis_label()
f2.hide_ytick_labels()
fig.canvas.draw()
It gives me the error: AttributeError: 'FITSFigure' object has no attribute 'set_tick_labels_font'
Could you please help me?
Thanks in advance
Refer the docs for FITSFigure. The error occurs because hide_yaxis_label and set_tick_labels_font methods dont exist in FITSFigure class so you cannot use them.
Change the code as follows :
f2.set_tick_labels_font to f2.tick_labels.set_font(size = 'small')
hide_yaxis_labels to axis_labels.hide_x()
hide_ytick_labels to tick_labels.hide_x()
Please read the documents for class/package before using it in your code.

Basemap error: module object is not callable

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.

pylab.imshow() not working

I've seen many times online that people use
import pylab
pylab.imshow(some_arg)
or
from pylab import *
imshow(some_arg)
but it gives the following error on my computer
NameError: name 'imshow' is not defined
however if I directly use matplotlib, it works fine
import matplotlib.pyplot as plt
plt.imshow(some_arg) # works fine
I've tried search about how pylab works, but there doesn't seem to be much information.
What should be the problem in my case?
Thank you.

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