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.
Related
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)?
I know the question has been asked before regarding using matplotlib in pyspark. I wanted to know how to do the same through shell. I used the same code as given in this solution
How to use matplotlib to plot pyspark sql results
However I am getting this when I run in the shell.
<matplotlib.axes.AxesSubplot object at 0x7f1cd604b690>
I am not entirely sure I do understand your question, so please provide more details in case I got you wrong.
If you happen to see an instance of a Subplot-Class such as given by you, rather have the previous command's return value assigned to a variable, because otherwise you cannot use it. I will give a simple example of a subplot below:
In essence, instead of:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(111)
do the following:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x_values, y_values)
plt.show()
If this didnt answer your question, please provide a minimal working example of what you are doing.
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.
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