LoginSignup
0
0

More than 1 year has passed since last update.

pyplot

Last updated at Posted at 2022-09-06

検索するのつかれたんで、自分で学んだものを整理しておきます。
【目次】
~matplotlibで軸が見切れるときの対処法~
◯Matplotlibでx軸のラベルが見切れるときの対処
◯Matplotlibでx軸見切れなくする
◯subplot間の間隔調整

◯Matplotlibでx軸のラベルが見切れるときの対処
引用:https://qiita.com/78910jqk/items/e8bce993081c384afdba

◯見切れなくする
グラフの描写範囲の設定を変えることでx軸のラベルを見切れないようにできる。
グラフの描写範囲の下端の設定はmatplotlib.pyolot.rcParams['figure.subplot.bottom']に格納されている。デフォルトだと0.11になっていて、下から11%の領域までグラフが書かれるため、x軸のラベルが入りきらない。これを15%(0.15)くらいに変更すればいい。

qiita.rb
plt.rcParams['font.size'] = 13
#この2行を書いておく
############################
plt.rcParams['figure.subplot.bottom'] = 0.15
plt.rcParams['figure.subplot.left'] = 0.15
############################
plt.rcParams['lines.linewidth'] = 3

◯subplot間の間隔調整
Figure内のsubplotの位置や相互の間隔を調整するには、subplot_adjust()メソッドを用いる。pyplot.subplot_adjust()でもよいが、figureのメソッドとしてもよい。

引数 説明 初期値※
left 左端 0.125
right 右端 0.9
bottom 下端 0.1
top 上端 0.9
wspace subplot間の幅の間隔 0.2
hspace subplot間の高さの間隔 0.2

※(キャンバスの左上を(0,0),右下を(1,1))としたときの比率)
rcParamsの意味はよくわからん。
今後調べておく。

◯サブプロットの作りかた

qiita.rb
plot = plt.figure()
ax = plot.add_subplot(
    xlabel='Number of principal components',
    ylabel='Contribution ratio(blue),\nCumulative contribution ratio(red)',
    )
x_axis = range(1, contribution_ratios.shape[0] + 1)
ax.bar(x_axis, contribution_ratios.iloc[:, 0], align='center')  # 寄与率の棒グラフ
ax.plot(x_axis, cumulative_contribution_ratios.iloc[:, 0], 'r.-')  # 累積寄与率の線を入れたプロット図
plot.savefig('img_contribution_ratio_{0}.png'.format(y_name))
ax.show

plotの中にaxes(付箋)があり、axを呼び出すことで描画することができる。
このaxを呼び出すためには、plt.figure()を呼び出してaxの情報を引き出す必要がある。

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