Python 从matplotlib继承
我想创建自己的plotting类,如下所示,但在使用Python 从matplotlib继承,python,inheritance,matplotlib,Python,Inheritance,Matplotlib,我想创建自己的plotting类,如下所示,但在使用plt模块时,我找不到从Figure继承的方法(见下文)。它要么继承自图,要么更改勾选参数Figure是一个类,因此我可以继承,但是plt不是一个模块?我只是一个初学者试图找到我的方式通过 有人能告诉我它是怎么工作的吗 import matplotlib.pyplot as plt from matplotlib.figure import Figure class custom_plot(Figure): def __init__(
plt
模块时,我找不到从Figure
继承的方法(见下文)。它要么继承自图
,要么更改勾选参数
Figure
是一个类,因此我可以继承,但是plt
不是一个模块?我只是一个初学者试图找到我的方式通过
有人能告诉我它是怎么工作的吗
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()
好的,同时我自己也找到了一个解决办法。首先,我从
Figure
创建了一个继承的类custom\u plot
,它与plt.Figure(FigureClass=custom\u plot,figtitle='my title')
一起使用。我使用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()