Matplotlib存储.svg与屏幕上显示的不同

时间:2016-07-22 21:24:23

标签: python svg matplotlib

这是一个例子。接下来是将其作为.png存储的内容,这就是它在屏幕上的显示方式,并且是正确的

this is the image that looks right

,接下来是使用插值进行热图的.svg

enter image description here

它们分别使用下一行代码存储

plt.savefig(filename,format='png')
plt.savefig(filename,format='svg')

接下来是生成实际情节的代码

def heatmapText(data,xlabels=[],ylabels=[],cmap='jet',fontsize=7):
    '''
    Heatmap with text on each of the cells
    '''


    plt.imshow(data,interpolation='none',cmap=cmap)

    for y in range(data.shape[0]):
        for x in range(data.shape[1]):
            plt.text(x , y , '%.1f' % data[y, x],
                     horizontalalignment='center',
                     verticalalignment='center',
                     fontsize=fontsize
                     )

    plt.gca()
    if ylabels!=[]:
        plt.yticks(range(ylabels.size),ylabels.tolist(),rotation='horizontal')
    if xlabels!=[]:
        plt.xticks(range(xlabels.size),xlabels.tolist(),rotation='vertical')

对于这两个图,我使用了完全相同的功能,但以不同的格式存储它。最后,在屏幕中显示正确(如.png)。

有关如何正确存储文件的.svg的想法吗?

1 个答案:

答案 0 :(得分:2)

基于http://matplotlib.org/examples/images_contours_and_fields/interpolation_none_vs_nearest.html

What does matplotlib `imshow(interpolation='nearest')` do?

matplotlib shows different figure than saves from the show() window

我建议您使用interpolation=nearest

进行尝试

以下代码显示相同的显示并保存为svg图:

import matplotlib.pyplot as plt
import numpy as np

A = np.random.rand(5, 5)
plt.figure(1)
plt.imshow(A, interpolation='nearest')

plt.savefig('fig',format='svg')
plt.show()