Matplotlib 不保存所有带有轴的图形

问题描述

使用 Matplotlib,我循环浏览不同设置的列表并将结果可视化。您可以在下方看到我想要可视化的两个图形以及我的 IDE (Spyder 4) 中的输出图片

def plot_svd_graph_2D(self,directory):
    ypoints0 = np.array(np.sort(self.svd_data[:,0]))
    ypoints1 = np.array(np.sort(self.svd_data[:,1]))
    
    plt.plot(np.arange(0.0,len(ypoints0),1),ypoints0,label='SV1',linestyle = 'dotted')
    plt.plot(np.arange(0.0,len(ypoints1),ypoints1,label='SV2',linestyle = 'dotted')
    
    plt.xlabel('Data points')
    # Set the y axis label of the current axis.
    plt.ylabel('singular value')
    # Set a title of the current axes.
    plt.title("Sorted sv's "+self.setting)
    # show a legend on the plot
    plt.legend()
    plt.grid()
    plt.savefig(directory+self.setting+'SSV.png')
    plt.show()
    return

以上代码输出如下所示:

enter image description here

    def plot_cluster_datapoints_graph(self,directory):
        fig=plt.figure()
        ax=fig.add_axes([0,1,1])
        ax.scatter(self.svd_data[:,0],self.svd_data[:,1],color='b',s = 0.2,label = 'Datapoints' )
        ax.scatter(self.cluster_centers[:,self.cluster_centers[:,s=8,color='r',label = 'Cluster centers')
        plt.title('Data point and clusters for 2 factors'+self.setting)
        plt.legend()
        plt.grid()
        plt.xlabel('Dimension 0')
        plt.ylabel('Dimension 1')
        plt.savefig(directory+self.setting+'-CD.png')
        plt.show()
        return

对于此代码输出如下所示:

enter image description here

调用方法的同时,我也保存了这些图。第一张图片保存为我的 IDE 中的输出所示,但是第二张图片保存如下(没有标题和轴:

enter image description here

为什么图形在我的 IDE 中显示正确(带有标签标题),但没有正确保存?我该怎么做才能正确地保存它们?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)