pikesaku’s blog

個人的な勉強メモです。記載内容について一切の責任は持ちません。

Matplotlib勉強

ソース

# -*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

x = [-50, 50, 50, -50]
y = [50, 50, -50, -50]

# http://ailaby.com/matplotlib_fig/
# subplot(行数, 列数, プロット番号)
# figは画像ファイル定義、axは画像の中のグラフの定義
# http://d.hatena.ne.jp/nishiohirokazu/20111121/1321849806
# https://matplotlib.org/examples/pylab_examples/major_minor_demo1.html

fig = plt.figure(figsize=(8, 8))
majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(5)

def fillplot(x, y, pos):
    ax = fig.add_subplot(2, 2, pos)
    ax.xaxis.set_major_locator(majorLocator)
    ax.xaxis.set_major_formatter(majorFormatter)
    ax.xaxis.set_minor_locator(minorLocator)

    ax.yaxis.set_major_locator(majorLocator)
    ax.yaxis.set_major_formatter(majorFormatter)
    ax.yaxis.set_minor_locator(minorLocator)

    ax.grid(which='both')
    ax.grid(which='minor', alpha=0.2)
    ax.grid(which='major', alpha=0.8)

    # 目盛の数字を表示する時は以下をコメントアウト
    plt.yticks(color='None')
    plt.xticks(color='None')
    #

    plt.xlim([-100, 100])
    plt.ylim([-100, 100])
    ax.fill(x, y, color='r')


for i in range(4):
  print(i)
  fillplot(x, y, i + 1)

plt.show()

アウトプット

f:id:pikesaku:20181007113033p:plain