Use pcolormesh
where you're using imshow
if you want vector output.
When using pcolor
or pcolormesh
you can't interpolate the image, however. On the other hand, you probably don't want interpolation if you're wanting vector output.
That's basically the reason for the difference between imshow
and pcolor
/pcolormesh
. imshow
produces a raster, while pcolormesh
and pcolor
produce rectangular patches.
You'll also need to slightly change the way you pass in the extent of the image. As an example based on yours:
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
surf = plt.pcolormesh(xedges, yedges, heatmap)
plt.axis('image')
plt.colorbar(surf, shrink=0.75, aspect=5)
plt.show()
When you save as an svg, the output will be vector patches. E.g.
...
<g id="QuadMesh_1">
<defs>
<path d="
M75.9063 -43.2
L82.9705 -43.2
L82.9705 -50.112
L75.9063 -50.112
L75.9063 -43.2" id="C0_0_9d1ab33858"/>
<path d="
M82.9705 -43.2
L90.0348 -43.2
L90.0348 -50.112
L82.9705 -50.112
L82.9705 -43.2" id="C0_1_d828245e6a"/>
...