2016-06-29 6 views
1

다음과 같이 자신의 플로팅 클래스를 만들고 싶습니다. 모듈을 사용하는 동안 Figure에서 상속하는 방법을 찾을 수 없습니다 (아래 참조). Figure에서 상속되거나 tick_params이 변경됩니다. Figure은 클래스이므로 상속받을 수 있지만 plt은 모듈이 아닙니다. 나는 처음부터 끝까지 내 길을 찾으려고 노력하는 초보자입니다 ...matplotlib에서 상속

누군가가 어떻게 작동하는지 보여 줄 수 있습니까?

import matplotlib.pyplot as plt 
from matplotlib.figure import Figure 

class custom_plot(Figure): 
    def __init__(self, *args, **kwargs): 
     #fn = os.path.join(os.path.dirname(__file__), 'custom_plot.mplstyle') 
     self.fig = plt 

    self.fig.tick_params(
     axis='x', # changes apply to the x-axis 
     which='both', # both major and minor ticks are affected 
     bottom='off', # ticks along the bottom edge are off 
     top='off', # ticks along the top edge are off 
     labelbottom='off') # labels along the bottom edge are off 

    # here id like to use a custom style sheet: 
    # self.fig.style.use([fn]) 

    figtitle = kwargs.pop('figtitle', 'no title') 
    Figure.__init__(self, *args, **kwargs) 
    self.text(0.5, 0.95, figtitle, ha='center') 

# Inherits but ignores the tick_params 
# fig2 = plt.figure(FigureClass=custom_plot, figtitle='my title') 
# ax = fig2.add_subplot(111) 
# ax.plot([1, 2, 3],'b') 

# No inheritance and no plotting 
fig1 = custom_plot() 
fig1.fig.plot([1,2,3],'w') 

plt.show() 

답변

1

좋아, 한편으로는 하나의 해결책을 찾았습니다. 먼저 plt.figure(FigureClass=custom_plot, figtitle='my title')과 함께 사용할 Figure에서 상속 된 클래스 custom_plot을 만들었습니다. 내가 cplot으로 plt 관련 수정을 수집 및 수용 가능한 결과를 얻을, 아래 참조 :

import os 
import matplotlib.pyplot as plt 
from matplotlib.figure import Figure 

class custom_plot(Figure): 
    def __init__(self, *args, **kwargs): 

     figtitle = kwargs.pop('figtitle', 'no title') 
     super(custom_plot,self).__init__(*args, **kwargs) 
     #Figure.__init__(self, *args, **kwargs) 
     self.text(0.5, 0.95, figtitle, ha='center') 

    def cplot(self,data): 

     self.fig = plt 
     fn = os.path.join(os.path.dirname(__file__), 'custom_plot.mplstyle') 
     self.fig.style.use([fn]) 
     self.fig.tick_params(
      axis='x', # changes apply to the x-axis 
      which='both', # both major and minor ticks are affected 
      bottom='off', # ticks along the bottom edge are off 
      top='off', # ticks along the top edge are off 
      labelbottom='off') # labels along the bottom edge are off 
     self.fig.plot(data) 

fig1 = plt.figure(FigureClass=custom_plot, figtitle='my title') 
fig1.cplot([1,2,3]) 
plt.show() 
관련 문제