[英]python inner classes how to create
我正在嘗試創建一個名為My_Axes的類。 它有一個模塊“大小”,該參數將圖形的大小,nx,ny –多少個圖和共享–(是或否)作為參數(無論是否要共享Xaxis)。
然后是一個內部類:'Style'。 它旨在改變My_Axes.size()返回的軸的外觀。 在這種情況下,我的目標是像細軸和Helvetica字體一樣的Matlab。 下面是我的代碼。 請原諒我,因為我仍在學習python。
class My_Axes:
def __init__(self):
self.style = self.Style()
def size(self,fig_siz,nx,ny,share):
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
self.fig_siz=fig_siz
self.nx = nx
self.ny = ny
self.share = share
fig = plt.figure(figsize = self.fig_siz)
x, y = self.fig_siz
self.nax = {}
self.n = 0
for i in range(self.nx):
for j in range(self.ny):
if self.share:
width = (0.9/self.nx)
height =(0.9/self.ny)
xpos = 0.08 + i*width
ypos = 0.08 + j*height
a = fig.add_axes([xpos,ypos,width,height])
self.nax['ax'+str(self.n)] = a
if j > 0: self.nax['ax'+str(self.n)].set_xticklabels([])
if i > 0: self.nax['ax'+str(self.n)].set_yticklabels([])
self.nax['ax'+str(self.n)].yaxis.set_major_locator(MaxNLocator(prune='lower'))
self.nax['ax'+str(self.n)].xaxis.set_major_locator(MaxNLocator(prune='lower'))
self.n += 1
else:
width = ((0.6+(x+y)/180.)/self.nx)
height =((0.6+(x+y)/180.)/self.ny)
xpos = 0.08 + (width + 0.1)*i
ypos = 0.08 + (height + 0.1)*j
a = fig.add_axes([xpos,ypos,width,height])
self.nax['ax'+str(self.n)] = a
self.n += 1
axx = self.nax
return axx
class Style:
def __init__(self,axx):
self.nax = axx
def matlb(self):
from matplotlib import rc, font_manager
ticks_font = font_manager.FontProperties(family='Helvetica', style='normal',
size=12, weight='normal', stretch='normal')
for a in sorted(self.nax):
aax = self.nax[a].xaxis
aay = self.nax[a].yaxis
for axis in ['bottom','left']:
self.nax[a].spines[axis].set_linewidth(0.3)
for axis in ['right','top']:
self.nax[a].spines[axis].set_visible(False)
aax.set_ticks_position('bottom')
aay.set_ticks_position('left')
lsiz = 8 + 4./self.n
self.nax[a].xaxis.set_tick_params(labelsize=lsiz)
self.nax[a].yaxis.set_tick_params(labelsize=lsiz)
return self.nax
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
aax = My_Axes().size((8,8),3,3,True)
aax.style.matlb
運行代碼時,出現錯誤:
Traceback (most recent call last):
File "/Users/aya/Desktop/test_class2.py", line 103, in <module>
aax = My_Axes().size((8,8),3,3,True)
File "/Users/aya/Desktop/test_class2.py", line 23, in __init__
self.style = self.Style()
TypeError: __init__() takes exactly 2 arguments (1 given)
[Finished in 0.5s with exit code 1]
[shell_cmd: python -u "/Users/aya/Desktop/test_class2.py"]
[dir: /Users/aya/Desktop]
[path: /usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/bin:/usr/bin:/usr/sbin:/usr/local/sbin:/bin:/sbin:/Library/TeX/texbin:/opt/X11/bin]
您的異常之所以發生,是因為Style
類沒有一個采用零個非self
aax = My_Axes()...
參數的構造函數,這就是在代碼底部調用aax = My_Axes()...
時要查找的內容。
該問題發生在您調用self.style = self.Style()
。
如果確實要在此處創建Style
對象,則需要編輯My_Axes
構造函數,以便將有效的axx
參數傳遞給self.Style(axx)
調用,例如:
class My_Axes:
def __init__(self, fig_siz,nx,ny,share):
axx = self.size(fig_siz,nx,ny,share)
self.style = self.Style(axx)
# ...
class Style:
def __init__(self, axx):
# ...
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
aax = My_Axes((8,8),3,3,True)
aax.style.matlb()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.