Python for Data Analysis
かわの まこと
可視化のお話
可視化すごい
最終目標:d3.js(http://d3js.org/)
• pythonにも結構ある
- PyQwt, Veusz, gnuplot-py, bigglesなど
• とはいえ,Webベースの可視化が流行り
• けどmatplotlibのお話
2
Figures and Subplots
plt.figure()でウィンドウを作成
• subplotで場所を作る
3
Argument Description
nrows subplotの行数
ncols subplotの列数
sharex 全てのsubplotで同じx軸を使う
sharey 全てのsubplotで同じy軸を使う
subplot_kw subplotの名前をつける(たぶん)
**fig_kw figureに名前をつける
Colors, Markers, and Line Styles
plt.plot関数
• x配列とy配列で緑の点線描く
- ax.plot(x, y, ‘g--‘)かax.plot(x, y, linestyle=‘--‘, color=‘g’)
• 色はRGB値つかえる
• マーカーもいろんなのがある
4
Ticks, Labels, and Lagends
pyplot: MATLABっぽいインターフェース
• xlim: 引数があればその範囲で,なければよしなな範囲を描く
• xticks
• xticlabels
5
凡例の追加
引数labelを追加するのが一番簡単
• legend(loc=‘best’)ってやるとよしなに配置してくれる
6
Annotations and Drawing on a Subplot
グラフに注釈つけたい
• text: 文字の注釈
• arrow: 矢印を入れる
• annotation: text + arrow
図形もかけちゃう
7
保存
plt.savefig(‘figpath.svg’)
• diskに保存しなくてもfile-likeなオブジェクトに保存もできる
8
Argument Description
fname ファイルパスかfile-likeなオブジェクトを渡して,保存
dpi 解像度
facecolor, edge, color 図の外側の色.デフォルト白
format 拡張子を指定(png, pdf, svg, ps, eps)
bbox_inches 図保存時の扱い.tightなら無駄なスペースを削除
matplotlibの設定
matplotlib/mpl-data/matplotlibrcに書くか,

matplotlibrcに書くか,rc関数を使う
• plt.rc(‘figure’, figsize=(10, 10))
• font_option = {‘family’: ‘monospace’

‘weight’: ‘bold’,

‘size’: ‘small’}
• plt.rc(‘font’, **font_options)
9
Plotting Functions in pandas
Series, DataFrameの構造が活きてくる
10
Argument Description
label 凡例
ax 描くsubplot指定
style スタイル’k--‘とか
alpha 透明度(0~1)
kind line’, ‘bar’, ‘barh’, ‘kde’
logy y軸で対数化する
use_index indexオブジェクトを目盛りにする
rot 目盛の角度(0~360)
xticks x軸の目盛
yticks y軸の目盛
xlim x軸の範囲
ylim y軸の範囲
grid 目盛格子を表示
Argument Description
subplots 分割されたsubplotに描く
sharex 同じx軸を使う
sharey 同じy軸を使う
figsize figureの大きさ
title タイトル
legend 凡例
sort_columns アルファベット順に列を描くか
Series.plot()の引数 DataFrame.plot()固有な引数
棒グラフ
縦棒: bar, 横棒: barh
曜日ごとのパーティ別の割合を表示
• tips.csvのヘッダsize->sizesにする(sizeは予約語っぽい)
11
ヒストグラムとDensity plot
Density plot: 連続的確率分布推定
• 正規分布→カーネル密度推定と呼ばれてたりする
12
散布図
13
ハイチの地震分析
2010年に起きたハイチでの地震災害と影響
• 出来事名,出来事の日付,場所,説明,カテゴリ,緯度,経
度,改善,確認,
• その前にBasemapをインストール
• ここ見よう
14

Pythonデータ分析 第3回勉強会資料 8章