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?
getattrgets an attribute. If you want to set an attribute, you needsetattr. But there’s rarely a need for either—you can just doax1.x = ax1.axisinstead ofsetatrr(ax1, 'x', getattr(ax1, 'axis')).ax1.x=ax1.xaxiswill be in a for loop because I have many axes e.g.,ax1, ax2, ax3, ax4,....so I want to loop thexaxisattribute 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')