LoginSignup
0
0

More than 5 years have passed since last update.

matplotlibで積み上げ式バープロット

Posted at

アミノ酸配列の組成(20xN次元)をプロットしてみたときのメモ。
積み上げ式のプロットを作ること自体は結構簡単。barbottom=hogeのように指定すればよい。
しかし、出てくるものの数が多いときに、凡例の場所を調整するので結構大変だった。プロットの幅をget_positionset_positionで調整→legendを使う際に、bbox_to_anchorで位置を調整、という流れ。

参考:
http://geetduggal.wordpress.com/2011/08/22/grabbing-individual-colors-from-color-maps-in-matplotlib/
http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot
http://matplotlib.org/users/legend_guide.html#plotting-guide-legend

plot_test.py
amino_count = ... # something; ここでは20x23次元のnp.array
accent = get_cmap('accent')  # colormapを得る
figsize(10, 8)     # 図のサイズと
rcParams['font.size'] = 14     #文字を少し大きめに
total = zeros(23)  # 積み上げるための和を保存
ax = subplot(111)
for i in xrange(20):    #20種類のアミノ酸それぞれに対して
    c = accent(i / 20.0)    # colormapは0〜1の値を入れると、対応するRGBA値を返す
    if i == 0:
        ax.bar(arange(23), amino_count[i], color=c, label=aa[i])
    else:
        ax.bar(arange(23), amino_count[i], color=c, bottom=total, label=aa[i])
        # bottomを指定することで、バーの下限を設定できる。
    total += tmd_count[i]

box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.80, box.height])
# プロットの幅を80%に設定する。
h,l = ax.get_legend_handles_labels()
ax.legend(h[::-1], l[::-1], bbox_to_anchor=(1.25, 1.05))
# get_legend_handles_labels()で、凡例のハンドルとラベルを得る。
# これを用いて、凡例を逆順で示している。
# また、bbox_to_anchorを用いることで凡例の位置を調整している。
xlabel('Position from the N-terminal side of TMD', fontsize=16)
ylabel('Count', fontsize=16)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0