matplotlib メモ
インポート
import matplotlib.pyplot as plt import matplotlib.image as mpimg import matplotlib as mpl import numpy as np
matplotlib.rc の編集
mpl.rc('lines', markersize=2, markeredgewidth=0.5) mpl.rc('mathtext', fontset='stixsans') mpl.rc('font', size=6) mpl.rc('xtick.major', size=2, pad=2) mpl.rc('ytick.major', size=2, pad=2) mpl.rc('axes', linewidth=0.5)
データの読み込み(テキストファイル)
txtfile = 'hoge.txt' data = np.genfromtxt(txtfile, skiprows=1, delimiter='\t')
データの読み込み(画像ファイル)
imgfile = 'hoge.png'
img = mpimg.imread(imgfile)
図のサイズ設定
fig = plt.figure(figsize=(40/25.4,60/25.4))
横 40 mm、縦 60 mm。
画像ファイルの表示
plt.imshow(img, aspect='auto', origin='upper', interpolation='None', extent=(-0.5, 200.5, -305, 305))
プロット
plt.plot(data[:,0], data[:,1], 'o')
JessyInk でトランジション効果を使う
環境は Windows 7 Home Premium + Inkscape 0.48.2。
スライド名に日本語を使うと、トランジションの設定で文字コード関係のエラーが起きる。
エラーメッセージ:
Traceback (most recent call last): File "jessyInk_transitions.py", line 74, in <module> effect.affect() File "C:\Program Files (x86)\Inkscape\share\extensions\inkex.py", line 215, in affect self.effect() File "jessyInk_transitions.py", line 53, in effect nodes = self.document.xpath(unicode("//*[@inkscape:groupmode='layer' and @inkscape:label='" + self.options.layerName + "']", 'utf-8'), namespaces=inkex.NSS) UnicodeDecodeError: 'utf8' codec can't decode byte 0x81 in position 53: unexpected code byte
self.options.layerName が Shift_JIS で入っているようで、これを utf-8 に変えないといけない。そのため Shift_JIS → unicode → utf-8 という変換をする。unicode というのは Python の変数の型。
具体的には、Inkscape\share\extensions\jessyInk_transitions.py の 53 行目
self.options.layerName
を
self.options.layerName.decode('Shift_JIS').encode('utf-8')
と変える。.decode('Shift_JIS') で unicode に変わり、.encode('utf-8') で utf-8 に変わる。これをこの後 unicode 型に変換しているんだから……もっとシンプルなコードにできそうだけど、まあいいや。
Matplotlib で背景黒のグラフを描く
rc parameters をいじる。
import matplotlib as mpl mpl.rc('lines', color='white') mpl.rc('patch', edgecolor='white') mpl.rc('text', color='white') mpl.rc('axes', facecolor='black', edgecolor='white', labelcolor='white') mpl.rc('xtick', color='white') mpl.rc('ytick', color='white') mpl.rc('grid', color='white') mpl.rc('figure', facecolor='black', edgecolor='white') mpl.rc('savefig', facecolor='black', edgecolor='white')
参考:
EPS を PDF に変換するバッチファイル
Windows 用。ps2pdf で変換したら画像が JPEG 圧縮になったので作った。eps2pdf.bat のような名前で保存して使うべし。
画像を FlateEncode で圧縮。BoundingBox を用紙サイズに設定する(-dEPSCrop)。
eps2pdf.bat hoge.eps
で、hoge.eps と同じディレクトリに hoge.pdf ができる。
eps2pdf.bat hoge.eps foo.pdf
で、カレントディレクトリに foo.pdf ができる。たぶん。
@echo off setlocal if "%1" == "" goto END echo %~dpn1 if "%2" == "" ( set OUT=%~dpn1.pdf ) else ( set OUT=%2.pdf ) echo %OUT% echo gswin32c.exe -q -dNOPAUSE -dBATCH -dEPSCrop -sDEVICE=pdfwrite -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile=%OUT% -f %1 gswin32c.exe -q -dNOPAUSE -dBATCH -dEPSCrop -sDEVICE=pdfwrite -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile=%OUT% -f %1 :END endlocal