Monday 29 July 2013

matplotlib (2)


ゆるゆると前の続き. 殆ど内容ないけど.

前回,numpy の array を使って
import matplotlib.pyplot as plt
import numpy as np
plotrange = np.arange(0, 1.01, 0.05)
f = lambda n, plotrange=plotrange : plotrange ** n
plt.plot(plotrange,f(0.2))
plt.show()
のように書いていたが(そして実際の plot にはこれが甚だ便利だろうけれど),
もうちょっと分解して書いてみよう.
以下
import matplotlib.pyplot as plt
import math
とした上で書いていく.
まずは plot する y 座標だけ与える場合.
plt.plot([math.sin(x*math.pi/100.0) for x in xrange(200)])
plt.show()
でこうなる. x は 0 から始まるらしい.

こんどは x,y 与える,というのは前回やったことなのだが, numpy 抜きで.
どうせ内部的には numpy の array として処理されるらしいけど.
trange = [math.pi*n/100 for n in xrange(200)]
plt.plot(map(lambda x: math.sin(2*x), trange),
         map(lambda x: math.cos(3*x), trange))
plt.show()
でこう.

gnuplot でいう set xrangeset yrange
plt.axis([x0,x1,y0,y1])
という記法でかけるらしい.
そしてファイルに保存するときは
plt.savefig('foo.png')
細かくオプション書くと
plt.savefig(filename, dpi=None, facecolor='w', edgecolor='w',
            orientation='portrait', papertype=None, format=None,
            transparent=False, bbox_inches=None, pad_inches=0.1)
  • filename : そのまま.
  • dpi これもそのまま.None か scalar > 0 を与えます.デフォルトは matplotlibrc の savefig.dpi の値.
  • facecolor, edgecolor 色.
  • orientation : landscape or portrait. 今のところ postscript のみ.
  • papertype : postscript で出力するときに. 'letter', 'legal', 'executive', 'ledger', 'a0' から 'a10', 'b0' - 'b10'
  • format : どのフォーマットで出力するか.たいてい png, pdf, ps, eps, svg はサポートされてる由.None だったら filename から判断してくれる. filename に拡張子がなくて None なら savefig.format で.
  • pyplot — Matplotlib 1.2.1 documentation を見てくださいな.
とりあえず今回はここまで.次は縦横比の設定とかやりたいですね.
おまけ: pyplot ってどんなん入ってるの
>>> import matplotlib.pyplot as plt
>>> dir(plt)
['Annotation', 'Arrow', 'Artist', 'AutoLocator', 'Axes', 'Button', 'Circle', 'Figure', 'FigureCanvasBase', 'FixedFormatter', 'FixedLocator', 'FormatStrFormatter', 'Formatter', 'FuncFormatter', 'GridSpec', 'IndexLocator', 'Line2D', 'LinearLocator', 'Locator', 'LogFormatter', 'LogFormatterExponent', 'LogFormatterMathtext', 'LogLocator', 'MaxNLocator', 'MultipleLocator', 'Normalize', 'NullFormatter', 'NullLocator', 'PolarAxes', 'Polygon', 'Rectangle', 'ScalarFormatter', 'Slider', 'Subplot', 'SubplotTool', 'Text', 'TickHelper', 'Widget', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_autogen_docstring', '_backend_mod', '_backend_selection', '_imread', '_imsave', '_interactive_bk', '_pylab_helpers', '_setp', '_setup_pyplot_info_docstrings', '_show', '_string_to_bool', 'acorr', 'annotate', 'arrow', 'autoscale', 'autumn', 'axes', 'axhline', 'axhspan', 'axis', 'axvline', 'axvspan', 'bar', 'barbs', 'barh', 'bone', 'box', 'boxplot', 'broken_barh', 'cla', 'clabel', 'clf', 'clim', 'close', 'cm', 'cohere', 'colorbar', 'colormaps', 'colors', 'connect', 'contour', 'contourf', 'cool', 'copper', 'csd', 'dedent', 'delaxes', 'disconnect', 'docstring', 'draw', 'draw_if_interactive', 'errorbar', 'figaspect', 'figimage', 'figlegend', 'fignum_exists', 'figtext', 'figure', 'fill', 'fill_between', 'fill_betweenx', 'findobj', 'flag', 'gca', 'gcf', 'gci', 'get', 'get_backend', 'get_cmap', 'get_current_fig_manager', 'get_figlabels', 'get_fignums', 'get_plot_commands', 'get_scale_docs', 'get_scale_names', 'getp', 'ginput', 'gray', 'grid', 'hexbin', 'hist', 'hist2d', 'hlines', 'hold', 'hot', 'hsv', 'imread', 'imsave', 'imshow', 'interactive', 'ioff', 'ion', 'is_numlike', 'is_string_like', 'ishold', 'isinteractive', 'jet', 'legend', 'locator_params', 'loglog', 'margins', 'matplotlib', 'matshow', 'minorticks_off', 'minorticks_on', 'mlab', 'new_figure_manager', 'normalize', 'np', 'over', 'pause', 'pcolor', 'pcolormesh', 'pie', 'pink', 'plot', 'plot_date', 'plotfile', 'plotting', 'polar', 'print_function', 'prism', 'psd', 'pylab_setup', 'quiver', 'quiverkey', 'rc', 'rcParams', 'rcParamsDefault', 'rcdefaults', 'register_cmap', 'rgrids', 'savefig', 'sca', 'scatter', 'sci', 'semilogx', 'semilogy', 'set_cmap', 'setp', 'show', 'silent_list', 'specgram', 'spectral', 'spring', 'spy', 'stackplot', 'stem', 'step', 'streamplot', 'subplot', 'subplot2grid', 'subplot_tool', 'subplots', 'subplots_adjust', 'summer', 'suptitle', 'switch_backend', 'sys', 'table', 'text', 'thetagrids', 'tick_params', 'ticklabel_format', 'tight_layout', 'title', 'tricontour', 'tricontourf', 'tripcolor', 'triplot', 'twinx', 'twiny', 'vlines', 'waitforbuttonpress', 'warnings', 'winter', 'xcorr', 'xlabel', 'xlim', 'xscale', 'xticks', 'ylabel', 'ylim', 'yscale', 'yticks']

No comments:

Post a Comment