LoginSignup
7
21

More than 5 years have passed since last update.

matplotlibチートシート

Last updated at Posted at 2017-09-20

自分用メモ

まず必要なこと

importとinline

test.py
import matplotlib.pyplot as plt
%matplotlib inline

import : ライブラリを使えるようにする
inline : グラフをjupyter上で使えるようにする

グラフのサイズ、フォントのサイズを指定したい

グラフのサイズを指定する

test.py
plt.figure(figsize = [10,10])

フォントのサイズを指定する

test.py
plt.rcParams["font.size"] = 18

グラフに表示する情報(凡例とか)

軸ラベル

test.py
plt.xlabel('hoge')
plt.ylabel('fuga')

凡例

① 引数にlabel(リスト系をいれる)を追加

test.py
labels = ['りんご', 'みかん', 'バナナ']
plt.bar(list_x,list_y, label = labels)

② plt.legend()を追加

test.py
plt.legend()

データに名前つける

引数にtick_label(リスト系を入れる)を追加。

test.py
labels = [
  'Canned Soft Drink'
  , 'Steak Burrito'
  , 'Chips and Guacamole'
  , 'Chicken Burrito'
  , 'Chicken Bowl'
]
plt.bar(list_x,list_y, tick_label = labels)

test.png

円グラフ(pie chart)

jupyter上で円が楕円になるとき

jupyter上でパイチャートを描くと楕円になる
image.png

plt.axis('equal')で解決
image.png

参考

上記以外にも円グラフのあれこれが載っている
http://pythondatascience.plavox.info/matplotlib/%E5%86%86%E3%82%B0%E3%83%A9%E3%83%95

savefigについて

公式ドキュメント

透過PNGにしたい

plt.savefig('hoge.png',transparent=True)
7
21
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
7
21