继承自matplotlib

时间:2016-06-29 11:17:37

标签: python inheritance matplotlib

我想创建自己的绘图类,如下所示,但在使用function day_name($i) { return date('l', $i * 86400 + 302400); } 模块时,我找不到从Figure继承的方法(见下文)。它继承自plt或更改Figuretick_params是一个类,所以我可以继承,但Figure不是模块?我只是一个试图找到自己的方式的初学者...

有人能告诉我它是如何运作的吗?

plt

1 个答案:

答案 0 :(得分: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()