Matplotlib:点在SVG中不显示

时间:2016-09-26 20:04:31

标签: python svg matplotlib

我有一个散点图,我想输出为SVG(Python 3.5)。但是,当与agg一起使用作为后端时,某些点就会丢失。查看数据以及PNG和SVG输出。这是某种错误配置还是错误?

代码:

import matplotlig
matplotlib.use('agg')
import matplotlib.pyplot as plt

x = [22752.9597858324,33434.3100283611,None,None,3973.2239542398,None,None,None
,None,None,None,None,None,960.6513071797,None,None,None,None,None,None,None
,None,None,None,None,None,749470.931292081,None,None,None,None,None,None
,None,None,None,None,None,None,None,None,23045.262784499,None,None,None
,None,None,None,None,1390.8383822667,None,None,9802.5632611025
,3803.3240362092,None,None,None,None,None,2058.1191666219,None
,3777.5383953988,None,91224.0759036624,23296.1857550166,27956.249381887
,None,237247.707648005,None,None,None,None,None,None,None,None,None
,760.3493458787,None,321687.799104496,None,None,22339.5617383239,None,None
,None,None,None,28135.0261453192,None,None,None,None,None,None,None
,1687.4387356974,None,None,29037.8494868489,None,None,None,None,None,None
,None,3937.3066755226,None,None,None,None]
y = [63557.4319306279,None,None,None,9466.0204228915,None,None,None,None,None
,None,None,None,3080.3393940948,None,None,None,None,None,None,None,None
,None,None,None,None,592184.803802073,None,None,None,None,None,None,None
,None,None,None,None,None,None,None,18098.725166318,None,None,None,None
,None,None,None,789.2710621298,None,None,7450.9539135753,4251.6033622036
,None,None,None,None,None,1277.1691956597,None,4273.5950324508,None
,51861.5572682614,19415.3369388317,2117.2407148378,None,160776.887146683
,None,None,None,None,None,None,None,None,None,1550.3003177484,None
,402333.163939038,None,None,16604.3340243551,None,None,None,None,None
,32545.0784355136,None,None,None,None,None,None,None,2567.9264180605,None
,None,45786.935597305,None,None,None,None,None,None,None,5645.5218715636
,None,None,None,None]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, '.')
fig.savefig('/home/me/test_svg', format='svg')
fig.savefig('/home/me/test_png', format='png')

结果:

PNG:

PNG output

SVG:

SVG output

2 个答案:

答案 0 :(得分:1)

更新

这看起来像是在matplotlib 2.0.0和3.1.1之间修复了一段时间的错误。升级为我解决了这个问题。

原始答案

我遇到了同样的问题,所以我创建了一个最小的示例来重现它:

import numpy as np
from matplotlib import pyplot as plt

data = np.array([1.0, np.nan, 1.0])
plt.plot(data, 'o')
plt.savefig('example.svg')
plt.savefig('example.png')

它可以用作PNG:

working example as PNG

但是,SVG缺少左点。

根据您的建议删除无效数据,我使用了numpy索引功能:

import numpy as np
from matplotlib import pyplot as plt

data = np.array([1.0, np.nan, 1.0])
indexes = np.arange(data.size)
is_valid = np.negative(np.isnan(data))
plt.plot(indexes[is_valid], data[is_valid], 'o')
plt.savefig('example.svg')
plt.savefig('example.png')

现在PNG和SVG都显示两个点。

答案 1 :(得分:0)

问题似乎与None值有关。虽然如果不存在匹配点,则根本没有包括点,但它似乎影响SVG的渲染。如果None位于一个或另一个点,则删除这两个条目可以解决问题。

data = np.array([x, y])
data = data.transpose()
# Filter out pairs of points of which at least one is None.
data = [pair for pair in data if pair[0] and pair[1]]
data = np.array(data).transpose()
x = data[0]
y = data[1]
ax.plot(x, y, '.')
fig.savefig('/home/me/test_svg', format='svg')
fig.savefig('/home/me/test_png', format='png')