matplotlib

<Python, matplotlib> Figureの部品

matplotlibの使い方ページに、 figureの部品構成がわかりやすく載っていたので、リンク。 Usage — Matplotlib 1.5.1 documentation figure --> 下地。 Axes --> 絵。 Axis --> 軸。

<Python, seaborn, matplotlib> Titleを追加とスペースの調整。

タイトルtitleを追加する。 図形Axes間のサイズを調整する。 plt.subplots_adjust(top=0.9) g.fig.suptitle('hogehoge title') suptitle()でタイトルを追加。 subplots_adjust()で、図形Axesのスペースのマージンを調整。 参考 stackoverflow.com qiita.com …

<Python, matplotlib> 2nd Y axis

2番目のY軸Secondary Y-axisを書く方法。 In [1]: import numpy as np ...: import matplotlib.pyplot as plt ...: x = np.arange(0, 10, 0.1) ...: y1 = 0.05 * x**2 ...: y2 = -1 *y1 ...: ...: fig, ax1 = plt.subplots() ...: ...: ax2 = ax1.twinx() ..…

<Python, matplotlib> pyplot.xlim()

pyplot.xlim()を引数無しで呼び出すと、現状の値を返してくれる。 へー。。。 In [43]: plt.xlim() Out[43]: (720.0, 770.0) In [44]: plt.xticks() Out[44]: (array([ 720., 730., 740., 750., 760., 770.]), <a list of 6 Text xticklabel objects>) こいつは、ax.get_xlim()だね。 In [46]: ax.</a>…

<Python, matplotlib> 3D描画

3Dを描いてみる。 In [1]: import pandas as pd In [2]: import numpy as np In [3]: import matplotlib.pyplot as plt In [4]: from mpl_toolkits.mplot3d import Axes3D In [5]: df = pd.DataFrame(data=np.random.randn(20,3), columns=list('xyz')) In […

<Python, matplotlib, seaborn> xlim, ylimの値をゲット

seabornや、matplotlibで作ったグラフgraphのX/Yの端の座標をゲットgetする方法。 .get_xlim()の関数methodを使う。 では、一例。 seabornのFacetオブジェクトの場合、matplotlib.axes.Axesに辿り着くまでちと深い。 In [1]: import pandas as pd In [2]: im…

<Python, matplotlib> scipython.com

うろうろしてたら、こんなもの見つけた。 役にたちそうなので、リンク。 http://scipython.com/

<Python, matplotlib, pyplot> 線を描く

matplotlibで線lineを書く。 In [1]: import matplotlib.pyplot as plt In [2]: fig, ax = plt.subplots() ...: ax.hlines(y=1, xmin=0, xmax=1, colors='r', linewidths=5) ...: ax.axhline(y=1.5, xmin=0, xmax=1, color='g') ...: plt.hlines(y=[0,2,3], …

<Python, matplotlib, pyplot> Figureを作りすぎで怒られた場合。

fig, ax =plt.subplots()をループするスクリプトを作ったら、次のエラーで怒られた。 /hoge/python/lib/python3.4/site-packages/matplotlib/pyplot.py:424: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot in…

<Python, matplotlib, pyplot> 横軸、縦軸を調整する。

グラフの横軸、縦軸を調整する。 例えば、大きすぎる範囲なので、特定範囲を見せたい時とか、 pyplotの関数、xlim()とylim()を使う。 axの時は、set_xlim()らしい。 In [17]: import matplotlib.pyplot as plt ...: ...: x = range(1,20) ...: y = range(1,2…

<Python, seaborn, matplotlib> 散布図の各要素に文字を付ける(seabornの場合)。

seabornの場合。 In [1]: import seaborn as sns In [2]: import pandas as pd In [3]: df = pd.DataFrame({'X':[1,2,3],'Y':[1,2,3],'A':['a','b','c']}) In [4]: g = sns.lmplot('X','Y',data=df) ...: for k, v in df.iterrows(): ...: g.ax.annotate(v['…

<Python, matplotlib> 散布図の各要素に文字を付ける。

散布図の各要素に文字を付ける方法。ax.annotate()を使う。 キーワードは、DataFrame、scatter、annotate In [25]:import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame(np.random.randn(5,2)) In [22]:fig, ax = plt…

<Python, matplotlib, mpld3> mpld3の出力結果(hoge.html)

// \n \n \n \n X (0, 0.36581843242256057)\n \n \n \n \n a\n 0.365818\n \n \n b\n -1.027153\n \n \n c\n 0.166174\n \n \n d\n 0.943041\n \n \n e\n -0.085353\n \n \n", "\n \n \n \n X (1, 0.053902576064756076)\n \n \n \n \n a\n 0.053903\n \n \…

<Python, matplotlib, mpld3> matplotlib を d3.js へ いいねー。

matplotlibのfigureをd3.jsへ変換してくれるすげーパッケージ。 MPLD3 — Bringing Matplotlib to the Browser 作った人は、Jake VanderPlasさん。 Jake VanderPlaswww.astro.washington.edu いやー。 すごいねー。 ということで、サンプルコード。 import ma…

<Python, matplotlib, seaborn> seaborn ライクなナイスな絵

seabornライクなナイスな絵にする方法。 import seaborn; seaborn.set() あと、matplotlibの見た目を変える方法。 Customizing plots with style sheets — Matplotlib 1.4.3 documentation

<Python, matplotlib> GUIの無い環境でグラフを保存する技。

import matplotlib matplotlib.use('Agg') を最初に唱える。 他の全ての描画関係のライブラリより先に唱える必要あり。 こんか感じにしておいた。 1 #!/usr/bin/env python 2 3 from optparse import OptionParser 8 #import matplotlib.pyplot as plt 9 #im…

<Python, matplotlib> subplot(ax)の作り方

Our Favorite Recipes — Matplotlib 2.0.0 documentation subplotの作り方。 なにやら、ナイスなやり方はsubplots()を使うらしい。 # new style method 2; use an axes array fig, axs = plt.subplots(2, 2, sharex=True, sharey=True) axs[0,0].plot(x) ほ…

<Python, matplotlib> figure, axesの配列指定

matplotlibでaxesを指定するとき、、 In [305]: import matplotlib.pyplot as plt In [306]: f, ax = plt.subplots(nrows=2, ncols=2)  In [307]: ax Out[307]: array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000106C1E48>, <matplotlib.axes._subplots.AxesSubplot object at 0x000000001911DB38>], [</matplotlib.axes._subplots.axessubplot></matplotlib.axes._subplots.axessubplot>

Python, matplotlib, heatmap

matplotlib で heatmap In [1]: import pandas as pd In [3]: import matplotlib.pyplot as plt In [4]: import numpy as np In [5]: df = pd.DataFrame(data=np.random.randn(10,10)) In [6]: df.head() Out[6]: 0 1 2 3 4 5 6 \ 0 -1.402152 1.153875 0.28…

Python, seaborn, 信頼区間(confidence interval)を求めて

Pythonのプロットで、なんとかggplotライク のgeom_smoothで描画される、信頼区間 Confidence intervalを探しもとめた結果、次のライブラリがいい感じ。 Seaborn: statistical data visualization — seaborn 0.5.1 documentation pandasのページにもseaborn…

<Python, matplotlib> X軸を文字で

X軸を文字で指定する方法。 In [32]: import matplotlib.pyplot as plt In [33]: plt.plot([1,2,3,4,5,6]) ...: plt.xticks([0,1,2,3,4,5],['a','b','c','d','e','f']) なんかもうちょい簡単にならんもんかね。。

Python, plotly, ちょっとはまった件。legendがあると、、

legendを指定していると、matplotlibからplotlyへの変換に失敗するねー。 次のコードはだめ import matplotlib.pyplot as plt import numpy as np import pandas as pd import plotly.plotly as py df= pd.DataFrame(np.random.randn(100,2), columns=['a','…

Python, iPython, plotly, 散布図の作成 3種類とplotlyへ渡す

散布図の作成方法。 3種類発見したので、メモメモ。 あと、plotlyをipython中に表示させる。 import matplotlib.pyplot as plt import numpy as np import pandas as pd from ggplot import * import plotly.plotly as py df= pd.DataFrame(np.random.randn…

Python, pandas, matplotlib, matplotlibでのベーシックなplot

DataFrameと組み合わせた場合の、いいプロット方法を発見したが、念のため、 matplotlibでのやり方をメモメモしておこう。 In [1]: import pandas as pd In [2]: import numpy as np In [3]: import matplotlib.pyplot as plt In [4]: df = pd.DataFrame(np.…

Python, pandas, ggplot-likeをオンにする時、はまったのでメモ

ggplotのような感じのグラフを作る。   In [1]: import pandas as pd In [2]: import matplotlib.pyplot as plt In [3]: df = pd.DataFrame([(1,2),(3,4)]) In [4]: df.plot() Out[4]: <matplotlib.axes._subplots.AxesSubplot at 0x2b50f7a91048> In [5]: plt.show() ということで、plt.show()が効く。 で、、ggplot</matplotlib.axes._subplots.axessubplot>…

Python, matplotlib, numpy, ヒストグラム作成ではまったこと、と、データフレームで四捨五入する方法

最初に四捨五入方法。 In [18]: import pandas as pd In [19]: import numpy as np In [20]: df = pd.DataFrame(np.random.randn(5,5)) In [21]: df Out[21]: 0 1 2 3 4 0 0.526538 0.935121 -0.380063 1.887691 -1.099712 1 -0.694135 -0.701937 -0.185376 …

Python, matplotlib, グラフの色の指定

グラフの色の指定方法。 次の8色は1文字で指定可。 b: blue/青 g: green/緑 r: red/赤 c: cyan/シアン m: magenta/マゼンダ y: yellow/黄 k: black/黒 w: white/白 aaaaaとかでも指定可。 In [2]: import matplotlib.pyplot as plt In [3]: import pandas …