LoginSignup
0
0

More than 3 years have passed since last update.

『Linuxのしくみ』の4章のグラフを自分でつくる(Python、matplotlib)

Last updated at Posted at 2021-04-10

[試して理解]Linuxのしくみの第4章プロセススケジューラで、以下のようなグラフが出てくる。
time_and_process.png
time_and_progress.png

データを採取するスクリプトは本に載っているが、グラフの作り方は載っていない。はじめはスプレッドシートで作ろうと思ったがなかなかうまく行かず、Pythonで作成したほうが楽だったので、その方法を紹介する。

import matplotlib.pyplot as plt


def create_time_and_process_graph():
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    with open("./data.txt") as f:
        for line in f:
            line = line.rstrip()
            p1, p2, p3 = map(int, line.split())
            # print(p1, p2, p3)
            ax.scatter(p2, p1, c=p3)

    fig.savefig('./time_and_process')


def create_time_and_progress_graph():
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    with open("./data.txt") as f:
        for line in f:
            line = line.rstrip()
            p1, p2, p3 = map(int, line.split())
            # print(p1, p2, p3)
            ax.scatter(p2, p3, c=['red', 'blue', 'yellow', 'green'][p1])

    fig.savefig('./time_and_progress')


if __name__ == '__main__':
    create_time_and_process_graph()
    create_time_and_progress_graph()

このスクリプトを実行すると、time_and_process.pngとtime_and_progress.pngという画像ファイルが作成される。

おしまい。

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