2016-06-29 3 views
1

Я хотел бы создать свой собственный класс построения следующим образом, но я не могу найти способ наследования от Figure при использовании модуля plt (см. Ниже). Либо он наследует от 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

Хорошо, между тем я нашел одно решение самостоятельно. Сначала я создал унаследованный класс custom_plot от Figure, который я использую в соединении с plt.figure(FigureClass=custom_plot, figtitle='my title'). Я собираю связанную модификацию plt с cplot и получаю приемлемый результат, см. Ниже:

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() 
Смежные вопросы