微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。