3

科学会議の場合、通常、論文のファイル サイズは制限されています。プロットを PDF として含めるのが好きなので、テキストと線が鮮明に保たれます。ただし、大量のデータを含む疑似カラー プロットまたは散布図を作成すると、エクスポートされた pdf は、完全な紙よりも簡単に大きくなります。

軸領域のみをビットマップにエクスポートして、後でベクトル軸に含める方法はありますか? (または、いくつかの要素がビットマップとして埋め込まれた PDF を取得するより良い方法はありますか?)

ここで誰かが私を助けてくれることを願っています。初めての投稿なので、質問を改善する方法についてコメントをいただければ幸いです。

4

2 に答える 2

5

個々Artistの をベクトル出力でラスターとしてエクスポートするように指定できます。

img = plt.imshow(...)
img.set_rasterized(True)

(ドキュメント)

于 2013-05-30T14:58:47.883 に答える
0

大きな散布図のこの回避策に気付きました。それは少しもエレガントではありませんが、私にとっては問題を解決します。よりエレガントなソリューションに関する推奨事項は大歓迎です ;)

    # -*- 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
于 2013-06-07T23:01:07.347 に答える