0

I am trying to assign a python object attribute a variable. Here is what I have been trying to do:

from read import *
import matplotlib.pyplot as plt
import numpy as np

data=table('data.txt')

semi=data['data']['a']
ecc = data['data']['e']
incl= data['data']['i']
bias = data['data']['bias']    

fig = plt.figure()
ax1 = fig.add_axes([0.06, 0.1, 0.4, 0.35])
ax2 = fig.add_axes([0.06, 0.6, 0.4, 0.35])
ax3 = fig.add_axes([0.55, 0.6, 0.4, 0.35])
ax4 = fig.add_axes([0.55, 0.1, 0.4, 0.35])

aaa = getattr(ax1,'xaxis')


nax={'ax1':ax1,'ax2':ax2,'ax3':ax3,'ax4':ax4}

for a in sorted(nax):

  aax = {'xaxis':nax[a].get_xaxis()}
  for axis in ['bottom','left']:
     nax[a].spines[axis].set_linewidth(0.5)
  for axis in ['right','top']:
     nax[a].spines[axis].set_visible(False)
  for b in sorted(aax):
     nax[a].b.set_tick_position('bottom')

When I run my code it says:

Traceback (most recent call last):
  File "./test1.py", line 35, in <module>
    ax1.b.set_ticks_position('bottom')
AttributeError: 'Axes' object has no attribute 'b'

Originally it works when:

ax1.xaxis.set_tick_position('bottom')

but I need to make the XAxis attribute a variable for the sake of creating plotting templates. Any idea how to make the above thing work?

2
  • getattr gets an attribute. If you want to set an attribute, you need setattr. But there’s rarely a need for either—you can just do ax1.x = ax1.axis instead of setatrr(ax1, 'x', getattr(ax1, 'axis')). Commented Sep 9, 2018 at 2:59
  • @abarnert Thank you so much! But ax1.x=ax1.xaxis will be in a for loop because I have many axes e.g., ax1, ax2, ax3, ax4,.... so I want to loop the xaxis attribute over all axes e.g., nax={'ax1':ax1,'ax2':ax2,'ax3':ax3,'ax4':ax4} for a in sorted(nax): nax[a].x.set_ticks_position('bottom') Commented Sep 9, 2018 at 3:17

2 Answers 2

2

Try x.set_tick_position('bottom') instead of ax1.x.set_tick_position('bottom'). x is already an attribute off of the ax1 object, so you don't need to access ax1 again.

Furthermore, no need for getattr if you know the name of the attribute statically. You can just do x = ax1.xaxis.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That works, but my axes are variables as well. Actually, here is a larger portion of what I've been trying to accomplish: fig = plt.figure() ax1 = fig.add_axes([0.06, 0.1, 0.4, 0.35]) ax2 = fig.add_axes([0.06, 0.6, 0.4, 0.35]) ax3 = fig.add_axes([0.55, 0.6, 0.4, 0.35]) ax4 = fig.add_axes([0.55, 0.1, 0.4, 0.35]) x = getattr(ax1,'xaxis') nax={'ax1':ax1,'ax2':ax2,'ax3':ax3,'ax4':ax4} for a in sorted(nax): for axis in ['bottom','left']: nax[a].x.set_ticks_position('bottom')
Sweet. I see you've accepted the answer. Mind upvoting if you found this useful?
0

I would suggest to work on the objects directly.

nax=[ax1,ax2,ax3,ax4]

for ax in nax:

   for axis in ['bottom','left']:
       ax.spines[axis].set_linewidth(0.5)
   for axis in ['right','top']:
       ax.spines[axis].set_visible(False)

   ax.xaxis.set_tick_position('bottom')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.