Matplotlib: Attributeerror: 'Axessubplot' Object Has No Attribute 'Add_Axes'

AttributeError: 'AxesSubplot' object has no attribute 'Circle' Embedding matplotlib in tkinter

Axes instances do not have a Circle method. That is part of matplotlib.patches, and can be imported separately.

Also, when you add the patch to the ax, you don't need to do ax.gca(), since you already have a handle for the current Axes (i.e. no need to .gca(), which stands for get current axes).

Finally, Axes do not have a show() method. That is a function from the pyplot module, that can be called as plt.show()

If you don't have them already, add the following imports:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle

And then modify your function to:

def placeObject(self):
drawObject = Circle(self.position, radius=self.radius, fill=False, color="black")
ax.add_patch(drawObject)
plt.show()

subplot AttributeError: 'AxesSubplot' object has no attribute 'get_extent'

Your problem is the way you are creating your axes, as "plain" matplotlib axes instead of axes equipped with a projection. You can pass on additional arguments to plt.subplots() using the subplot_kws= argument, which will then be passed on to each individual Axes object.

The following is adapted from the answer you provided, with the function scale_bar() used verbatim

import cartopy.crs as ccrs
from math import floor
import matplotlib.pyplot as plt
from matplotlib import patheffects


def scale_bar(ax, proj, length, location=(0.5, 0.05), linewidth=3,
units='km', m_per_unit=1000):
"""
http://stackoverflow.com/a/35705477/1072212
(...)
"""
(...)


fig, axs = plt.subplots(nrows=4, ncols=3, sharex='col', sharey='row',
subplot_kw={'projection': ccrs.Mercator()}) # <--- Here is the missing piece
fig.suptitle('Cyprus')
for ax in axs.flatten():
ax.set_extent([31, 35.5, 34, 36], ccrs.Geodetic())
ax.coastlines(resolution='10m')
scale_bar(ax, ccrs.Mercator(), 100)
plt.show()

enter image description here

Threshold Otsu: AttributeError: 'AxesSubplot' object has no attribute 'ravel'

axes is just a single figure with 1 column so there is nothing to ravel or flatten. It will work if you have more than one sub plot. Nevertheless, you can do the following without ravel if you have only a single row or a single column.

fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True)

ax[0].imshow(img[20,:,:], cmap=plt.cm.gray)
ax[0].set_title('Original Breast Delineation')
ax[0].axis('off')

ax[1].hist(thresh, bins=256)
ax[1].set_title('Histogram ')
ax[1].axvline(thresh, color='r')

ax[2].imshow(binary[20,:,:], cmap=plt.cm.gray)
ax[2].set_title('Thresholded')
ax[2].axis('off')

In case you want a 2d matrix of subplot instances, you can use Thomas Kühn's suggestion.

fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True, squeeze=False)

and then you can access the subplots as

ax[0][0].imshow()
ax[0][1].imshow()
......

Tkinter AttributeError: 'AxesSubplot' object has no attribute 'canvas' and matplotlib graphs don't update until I resize the tkinter window

Problem solved by updating the functions

def plot_utm():
sbpt_1 = fig.add_subplot(1, 3, 1)
content.plot (color = '#3BB9FF', ax = sbpt_1)
display.draw_idle()

def plot_tm():
sbpt_2 = fig.add_subplot(1, 3, 2)
shp_TM.plot(color = '#4CC417', ax = sbpt_2)
display.draw_idle()

def plot_om():
sbpt_3 = fig.add_subplot(1, 3, 3)
shp_TMOM.plot(color = '#FFA62F', ax = sbpt_3)
display.draw_idle()

AttributeError while adding colorbar in matplotlib

Note: I am using python 2.6.2. The same error was raised with your code and the following modification solved the problem.

I read the following colorbar example: http://matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html

from matplotlib import pylab as pl
import numpy as np

data = np.random.rand(6,6)
fig = pl.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
img = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
fig.colorbar(img)

pl.show()

Not sure why your example didn't work. I'm not that familiar with matplotlib.

Tight_Layout: Attribute Error . 'AxesSubplot' object has no attribute 'tight_layout'

My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:

Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:

import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()

Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()

What is the proper way of re-adding axes to a matplotlib figure while maintaining a constrained layout?

Using the current development version of matplotlib the error would not appear. But one would need to make sure to exclude the respective axes from the constrained layout mechanism.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(constrained_layout=True)
ax2 = fig.add_subplot(111, projection="polar")
ax2.set_visible(False)
ax2.set_in_layout(False)

def swap(evt):
if evt.key == "h":
b = ax.get_visible()
ax.set_visible(not b)
ax.set_in_layout(not b)
ax2.set_visible(b)
ax2.set_in_layout(b)
fig.canvas.draw_idle()

cid = fig.canvas.mpl_connect("key_press_event", swap)

plt.show()

With any current matplotlib version, one could use tight_layout instead of constrained layout, and call it manually at each resize event.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax2 = fig.add_subplot(111, projection="polar")
ax2.set_visible(False)


def swap(evt):
if evt.key == "h":
b = ax.get_visible()
ax.set_visible(not b)
ax2.set_visible(b)
fig.tight_layout()
fig.canvas.draw_idle()

def onresize(evt):
fig.tight_layout()

cid = fig.canvas.mpl_connect("key_press_event", swap)
cid = fig.canvas.mpl_connect("resize_event", onresize)
fig.tight_layout()
plt.show()


Related Topics



Leave a reply



Submit