我意识到这是一个大型散点图的解决方法。
这一点也不优雅,但它解决了我的问题。
更优雅解决方案的建议非常受欢迎;)# -*- coding: utf-8 -*-
"""
Created on Sat Jun 1 17:21:53 2013
@author: roel
"""
import pylab as pl
from matplotlib._png import read_png
def bitmappify(ax, dpi=None):
fig = ax.figure
# safe plot without axes
ax.set_axis_off()
fig.savefig('bitmap.png', dpi=dpi, transparent=True)
ax.set_axis_on()
# remeber geometry
xl = ax.get_xlim()
yl = ax.get_ylim()
xb = ax.bbox._bbox.corners()[:,0]
xb = (min(xb), max(xb))
yb = ax.bbox._bbox.corners()[:,1]
yb = (min(yb), max(yb))
# compute coordinates to place bitmap image later
xb = (- xb[0] / (xb[1] - xb[0]),
(1 - xb[0]) / (xb[1] - xb[0]))
xb = (xb[0] * (xl[1] - xl[0]) + xl[0],
xb[1] * (xl[1] - xl[0]) + xl[0])
yb = (- yb[0] / (yb[1] - yb[0]),
(1 - yb[0]) / (yb[1] - yb[0]))
yb = (yb[0] * (yl[1] - yl[0]) + yl[0],
yb[1] * (yl[1] - yl[0]) + yl[0])
# replace the dots by the bitmap
del ax.collections[:]
del ax.lines[:]
ax.imshow(read_png('bitmap.png'), origin='upper',
aspect= 'auto', extent=(xb[0], xb[1], yb[0], yb[1]))
# reset view
ax.set_xlim(xl)
ax.set_ylim(yl)
# create a plot
f, a = pl.subplots(1,1)
n = 1e4
a.scatter(pl.random(n)*2+6, pl.random(n)*3-12,
c=pl.random(n), s=100*pl.random(n))
# safe as large pdf file for comparison
f.savefig('vector.pdf') # gives a large file: 3.8 MB
bitmappify(a, 144)
# save as smaller pdf
f.savefig('hybrid.pdf', dpi=144) # reasonably sized file: 0.5 MB