0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Day6. CSV → グラフ自動生成ツール - 勝手にChatGPTチャレンジ (Python)

Last updated at Posted at 2025-12-06

前提

本日のお題


6. CSV → グラフ自動生成ツール

何を作る?
data.csv の特定カラムを折れ線グラフや棒グラフにして PNG 出力するスクリプト。

学べること

  • pandas での CSV 読み込み
  • matplotlib での簡単な可視化
  • コマンドライン引数で「どのカラムを使うか」指定

面白いところ

  • 手元のログや家計簿をすぐ可視化できて気持ちいい
  • 少しずつグラフの装飾を変えて「見栄え改善」を楽しめる

準備

データセットは定番?のIrisを使用します。
ダウンロードしたcsvファイルを06_iris-dataset.csvとして保存しておきます。
Irisデータセットについては例えば以下を参照ください。

回答

コード

06_csv2graph.py
import argparse
import pandas as pd
import matplotlib.pyplot as plt

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("x", type=str)
    parser.add_argument("y", type=str)
    return parser.parse_args()

def load_iris():
    path = "06_iris-dataset.csv"
    df = pd.read_csv(path)
    return df


def plot_scatter(df, x_name, y_name):
    plt.scatter(df[x_name], df[y_name])
    plt.xlabel(x_name)
    plt.ylabel(y_name)
    plt.savefig("scatter.png")


def main():
    df = load_iris()
    args = get_args()
    plot_scatter(df, args.x, args.y)


if __name__ == "__main__":
    main()

実行例

$python 06_csv2graph.py sepal_length sepal_width

scatter.png

感想

  • 実家のような安心感のあるタスクでした
  • 細かい調整はまぁいいかの気持ちでさぼりました
  • subplotsとかで全カラム分の散布図を出すくらいはやっても良かったのかもしれない…。

また明日。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?