【问题标题】:using fig,axes in a loop matplotlib在循环 matplotlib 中使用 fig,axes
【发布时间】:2018-04-26 20:30:45
【问题描述】:

我有一个关于在 matplotlib 的循环中使用 fig、axes 函数的问题。 我正在尝试在一个循环中创建一些带有多个子图的图(子图的数量不固定),如下所示:

def start_plot(self):
    if self.running:
        run_fig, run_ax = plt.subplots(*self.matrix)
    if self.histogram:
        hist_fig, hist_ax = plt.subplots(*self.matrix)

def create_signal_plots(self, iwindow, window_name):
    if self.running:
        run_ax[iwindow+1].plot(running_perf, label=window_name) # throws error run_ax not recognized
    if self.histogram:
        hist_ax[iwindow+1].hist(timeseries, label=window_name) 

plot = plot_class(run =1, hist =1, matrix = get_matrix(*args)) # e.g. matrix = (3,2)

for istrat, strat in enumerate(strats):
    plot.start_plot()
    for iwindow, window in enumerate(windows):
        plot.create_plots(iwindow, window)

有没有办法让这项工作无需在函数中返回轴并传递它?如果我使用 plt.figure 而不是 fix,axes,那么我可以简单地使用 plt.figure(fig_no) 更新任何图形。

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    您可以将run_figrun_ax 作为实例属性存储在您的对象中,然后从该对象的任何其他方法访问它们。这将使用self 完成。

    start_plotcreate_signal_plots中使用self.run_fig等,如:

    def start_plot(self):
        if self.running:
            self.run_fig, self.run_ax = plt.subplots(*self.matrix)
        if self.histogram:
            self.hist_fig, self.hist_ax = plt.subplots(*self.matrix)
    
    def create_signal_plots(self, iwindow, window_name):
        if self.running:
            self.run_ax[iwindow+1].plot(running_perf, label=window_name) # throws error run_ax not recognized
        if self.histogram:
            self.hist_ax[iwindow+1].hist(timeseries, label=window_name) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多