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

14
Python for Data Analysis かわの まこと 可視化のお話

Upload: makoto-kawano

Post on 20-Jul-2015

167 views

Category:

Engineering


6 download

TRANSCRIPT

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

Python for Data Analysisかわの まこと

可視化のお話

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

可視化すごい最終目標:d3.js(http://d3js.org/)

• pythonにも結構ある

- PyQwt, Veusz, gnuplot-py, bigglesなど

• とはいえ,Webベースの可視化が流行り

• けどmatplotlibのお話

2

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

Figures and Subplotsplt.figure()でウィンドウを作成

• subplotで場所を作る

3

Argument Description

nrows subplotの行数

ncols subplotの列数

sharex 全てのsubplotで同じx軸を使う

sharey 全てのsubplotで同じy軸を使う

subplot_kw subplotの名前をつける(たぶん)

**fig_kw figureに名前をつける

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

Colors, Markers, and Line Stylesplt.plot関数

• x配列とy配列で緑の点線描く

- ax.plot(x, y, ‘g--‘)かax.plot(x, y, linestyle=‘--‘, color=‘g’)

• 色はRGB値つかえる

• マーカーもいろんなのがある

4

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

Ticks, Labels, and Lagendspyplot: MATLABっぽいインターフェース

• xlim: 引数があればその範囲で,なければよしなな範囲を描く

• xticks • xticlabels

5

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

凡例の追加引数labelを追加するのが一番簡単

• legend(loc=‘best’)ってやるとよしなに配置してくれる

6

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

Annotations and Drawing on a Subplotグラフに注釈つけたい• text: 文字の注釈

• arrow: 矢印を入れる

• annotation: text + arrow

図形もかけちゃう

7

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

保存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なら無駄なスペースを削除

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

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

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

Plotting Functions in pandasSeries, DataFrameの構造が活きてくる

10

Argument Descriptionlabel 凡例

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()固有な引数

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

棒グラフ縦棒: bar, 横棒: barh

曜日ごとのパーティ別の割合を表示• tips.csvのヘッダsize->sizesにする(sizeは予約語っぽい)

11

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

ヒストグラムとDensity plotDensity plot: 連続的確率分布推定• 正規分布→カーネル密度推定と呼ばれてたりする

12

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

散布図

13

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

ハイチの地震分析2010年に起きたハイチでの地震災害と影響• 出来事名,出来事の日付,場所,説明,カテゴリ,緯度,経度,改善,確認,

• その前にBasemapをインストール

• ここ見よう

14