Using Matplotlib in python, i tried this code and got below error. Pls help.
Code
plt.plot(x,y)
plt.grid(True)
plt.show()
Error
TypeError: 'bool' object is not callable
All the thing you need is to reload the pyplot module.
from importlib import reload
reload(plt)
plt.grid(True)
Have you tried plt.grid() (without True)?
Related
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.
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()
I import matplotlib:
import matplotlib.pyplot as plt
Then, at some point I try this:
plt.set_xlim(datetime.date(2011,1,1),datetime.date(2015,9,1))
And as a result I get this:
TypeError: 'list' object is not callable
My guess was that I, at some point, rewritten plt.xlim (or something like that). So, I tried
plt.clf()
It does not help. I am still getting the same error message. So, does anybody know why plt.set_xlim is a list and what to do with that?
I had the same problem too. Apparently the xlim must be set by the axis not the plot. This contradicts some of the matplotlib documentation and examples (e.g., here)
Anyways:
ax = plt.gca()
ax.set_xlim(your_xmin, your_xmax)
Edit: It appears that the matplotlib documentation has now been fixed.
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
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()