nano_exit

基礎的なことこそ、簡単な例が必要だと思うのです。

python: matplotlib.pyplot: インスタンスを使わない一通りのまとめ

いつも忘れるので、個人的なメモ

参考にしたサイトの皆様
グラフの体裁を整える — matplotlib 1.0 documentation
matplotlibでの凡例(ラベル)の表示場所・形式を変更する - ゆるふわめも
matplotlibで忘れがちな文法まとめ - Qiita
【Python】matplotlibによるグラフ描画時のColormapのカスタマイズ - Qiita
matplotlibの凡例を半透明に - Qiita
matplotlib入門 - りんごがでている
[Python] matplotlib: 論文用に図の体裁を整える - Qiita
【Python】matplotlibのフォントを変えた際にクリアする必要のあるキャッシュ - 俺言語。
matplotlib と Seaborn の軸の日本語設定 - Qiita
api example code: font_family_rc.py — Matplotlib 2.0.2 documentation
matplotlibでグラフの文字サイズを大きくする - Qiita
自分用メモ: matplotlibを使ったグラフの調整 - Qiita
Matplotlib 超入門(3)文字の大きさ,グリッド幅
matplotlibで忘れがちな文法まとめ - Qiita
matplotlibで色付きのベクトルを作成 - ゴルゾンの日記
matplotlib/plot - memoring
matplotlibで、系列の凡例を枠外に表示する - Symfoware
matplotlibでrectangleを描く | mwSoft
matplotlib.axes.Axes.tick_params — Matplotlib 2.1.0.post851+g2fd479a documentation
Customizing matplotlib — Matplotlib 2.0.2 documentation

import matplotlib as mpl

# change font
mpl.get_cachedir()
mpl.rcParams[ "font.family" ] = [ "Arial" ]

# change font size
mpl.rcParams[ "font.size" ] = 18

# change the line width of the frame/
mpl.rcParams[ "axes.linewidth" ] = 2.0

# adjust the positon of ticks
mpl.rcParams[ "xtick.major.pad" ] = 3.5
mpl.rcParams[ "ytick.major.pad" ] = 3.5


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# set figure size
plt.figure( figsize = ( 8, 6 ) )

# plot data
for id, data in enumerate( data_list ):
  x, y = data[0], data[1]
  #cy = "#00a0e9"
  #ma = "#e4007f"
  #gr = "#009944"
  #or = "#f39800"
  #bl = "#0068b7"
  #clolar_list = [ cy, ma, gr, or, bl ]
  #color = cm.tab10( id  )
  #color = cm.viridis( id / len( data_list )  )
  color = cm.cool( id / len( data_list )  )
  plt.plot( x, y, lw = 2, c = color, label = label_list[ id ] )

# add comments
plt.text( xt, yt, "comment", fontsize = 10 )

# add arrows
plt.quiver( x_position, y_position, \
            x_component, y_component, \
            angles = "xy", scale_units = "xy" )

# add rectangular
plt.gca().add_patch( plt.rectangle( [ x_left, y_bottom ], width, height, fill=False ) )

# add horizontal and/or vertical lines
# when you use hlines or vlines, added lines are behind the plot.
#plt.hlines( y_list, xmin, xmax, linestyle = "dashed" )
#plt.vlines( x_list, ymin, ymax, linestyle = "dashed" )
plt.plot( [ xmin, xmax ], [ y, y ], linestyle = "dashed"  )
plt.plot( [ x, x ], [ ymin, ymax ], linestyle = "dashed"  )

# change representation of labels of ticks
plt.xticks( [ x1, x2, x3 ], [ 'X1', 'X2', 'X3' ] )
plt.yticks( np.linspace( ymin, ymax, dividing_number ) )

# change parameters of ticks
plt.tick_params( direction = 'out', width = 1, length = 8, labelsize = 12)

# set each minimum and maximum
plt.xlim( xmin, xmax )
plt.ylim( ymin, ymax )

# set labels for each axis
plt.xlabel( 'horizontal', fontsize = 14 )
plt.ylabel( 'vertical', fontsize = 14 )

# allow to appear legends
#plt.legend( loc = 'upper left', bbox_to_anchor = ( 1.05, 1.00 ),  frameon = False, fontsize = 10 )
#plt.subplots_adjust( right = 0.7 )
plt.legend( loc = 'best', frameon = False, fontsize = 10 )

# avoid lacking of characters of the labels
plt.tight_layout()

# save figure
plt.savefig( 'filename.eps', format='eps', transparent=True, dpi=600 )

ちなみに調べたところ、tickという単語そのものには目盛りの意味は無く、「瞬間」という意味らしい。
tick markで目盛りという意味になるらしいので、そこから来ていると思われる。

カラーマップの実際の色は下記のサイトで見れる。
Choosing Colormaps — Matplotlib 2.0.2 documentation

色の選択には下記のサイトを参照
統計グラフの色

f:id:koideforest:20171115080506p:plain
左が通常、右がphotoshop仕様

でも matplotlib が version 2 になってからdefaultの色の設定でも十分見易くなったと思う。color map の viridis は default で設定されているが、全然悪くない。
今度論文書くときは安直だがそれで図を作ってみることにする。