LoginSignup
4
3

More than 3 years have passed since last update.

matplotlib のまとめ 軸幅 軸間隔 斜めラベル 

Last updated at Posted at 2020-08-21

matplotlibのめっちゃまとめの「よく考えたら自分が普段使うようなメソッドなんて限られているじゃないか。もう自分でまとめるわ。自分のために。」に共感しました。そこで、私のmatplotlibのまとめです。

ソース

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

#テスト用のデータの定義
dt={
       "y": [1 , 2 , 3 ,4 ,5 ,6 ,7 ,8 ,15 , 25 ]  ,
      "x1": [10,20,30,40,50,60,70,80,90,80 ] , 
      "x2": [105,205,305,405,505,pd.np.NAN, 705,805,905,905 ] 
      }
df=pd.DataFrame(dt)

print(df)

#大きさ(figsize)が(10,5)の画用紙に上下に3つのグラフを書く
#sharex="col" x軸を共有する。
fig ,ax = plt.subplots(3 , 1, sharex="col", figsize=(10,5) )


#3つのグラフを書く
ax[0].plot(df["y"] , df["x1"], "*-" ,label="x1")
ax[1].plot(df["y"] , df["x2"], "*-" ,label="x2")
ax[2].plot(df["y"] , df["x2"], "*-" ,label="x2")


#y軸の描画範囲の調整
ax[2].set_ylim([500,800])

#y軸目盛間隔を調整
start, end = ax[2].get_ylim()
stepsize=25
ax[2].yaxis.set_ticks(pd.np.arange(start, end, stepsize))


#タイトルを書く
ax[2].set_title("set_title")

#x軸のラベルを書く
ax[2].set_xlabel("set_xlabel")

#凡例の位置
ax[2].legend(loc='upper right' )

#グリッドの表示
ax[2].grid(True)

#x軸のラベル文字の角度をつける
plt.setp(ax[2].get_xticklabels(), rotation=60, ha="right")



#画面表示
plt.show()

#ファイルへ書込み
plt.savefig("out.png")


結果

image.png

参考

matplotlibのめっちゃまとめ

4
3
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
4
3