python导出数据成mat_使用python matplotlib将轴区域仅导出为位图

我意识到这是一个大型散点图的解决方法。

这一点也不优雅,但它解决了我的问题。

更优雅解决方案的建议非常受欢迎;)# -*- 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值