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?

片対数グラフをCSVファイルからプロットするPythonコード

Posted at

はじめに

本記事では、**RLC直列回路の周波数特性(電流と位相差)**をPythonを用いてグラフ化する手順を紹介します。

実験やシミュレーションで得られた周波数応答データ(電流[mA]と位相差[deg])をもとに、以下の2種類のグラフを作成します:

  • 線形スケールのグラフ:直感的に周波数と電流・位相差の関係がわかる
  • 片対数スケール(X軸)グラフ:低周波から高周波まで広い範囲を一目で把握できる

また、データ入力のための CSVファイルの作成方法、および Google Colab を利用したアップロードと可視化も紹介しています。これにより、誰でも簡単に実験データの可視化が可能になります。

参考文献

Pythonコード

import matplotlib.pyplot as plt
import numpy as np

# ================================================
# ダミーデータの生成 / Generate Dummy Data
# ================================================

# 周波数 [kHz] / Frequency in kHz
freq_kHz = np.linspace(0.1, 100, 30)  # 0.1 kHz ~ 100 kHz の範囲で30点

# 電流 [mA]:共振周波数でピークを持つようなデータ / Simulate resonance curve
resonance_freq = 10  # 共振周波数 [kHz]
current_mA = 10 / np.sqrt((1 - (freq_kHz / resonance_freq)**2)**2 + (0.1 * freq_kHz)**2)
current_mA *= 0.8  # スケーリング

# 位相差 [deg]:共振点で0°、低周波で-90°, 高周波で+90°の典型形状
phase_deg = np.degrees(np.arctan((freq_kHz / resonance_freq - resonance_freq / freq_kHz) * 5))

# ================================================
# 線形スケールでのプロット / Linear Scale Plot
# ================================================
fig1, ax1 = plt.subplots(figsize=(10, 5))

# 左軸:電流 / Left Y-axis: Current
ax1.set_xlabel("Frequency f [kHz]")
ax1.set_ylabel("Current [mA]", color='tab:blue')
ax1.plot(freq_kHz, current_mA, 'o-', color='tab:blue', label="Current")
ax1.tick_params(axis='y', labelcolor='tab:blue')

# 右軸:位相差 / Right Y-axis: Phase
ax2 = ax1.twinx()
ax2.set_ylabel("Phase Difference [deg]", color='tab:orange')
ax2.plot(freq_kHz, phase_deg, 'o-', color='tab:orange', label="Phase")
ax2.tick_params(axis='y', labelcolor='tab:orange')

# グラフの装飾 / Decoration
plt.title("RLC Series Circuit Frequency Characteristics (Linear Scale)")
fig1.tight_layout()
plt.grid(True, linestyle="--", linewidth=0.5)
plt.show()

# ================================================
# 片対数スケールでのプロット / Semilog X Plot
# ================================================
fig2, ax3 = plt.subplots(figsize=(10, 5))

# 左軸:電流 / Left Y-axis: Current
ax3.set_xlabel("Frequency f [kHz]")
ax3.set_ylabel("Current [mA]", color='tab:blue')
ax3.semilogx(freq_kHz, current_mA, 'o-', color='tab:blue', label="Current")
ax3.tick_params(axis='y', labelcolor='tab:blue')

# 右軸:位相差 / Right Y-axis: Phase
ax4 = ax3.twinx()
ax4.set_ylabel("Phase Difference [deg]", color='tab:orange')
ax4.semilogx(freq_kHz, phase_deg, 'o-', color='tab:orange', label="Phase")
ax4.tick_params(axis='y', labelcolor='tab:orange')

# グラフの装飾 / Decoration
plt.title("RLC Series Circuit Frequency Characteristics (Semilog X Scale)")
fig2.tight_layout()
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()

結果

image.png

image.png

CSVファイルを作るPythonコード

import pandas as pd

# データの辞書 / Define data as dictionary
data = {
    "Frequency_kHz": [
        0.1, 0.2, 0.5, 1.0, 1.5, 2.0, 3.0, 5.0, 6.0, 7.0, 7.5, 8.0, 8.5, 9.0,
        9.188, 9.5, 10.0, 12.0, 15.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
        80.0, 85.0, 90.0, 95.0, 100.0
    ],
    "Current_mA": [
        0.724, 0.645, 0.453, 0.459, 0.841, 1.221, 2.44, 4.51, 6.22, 7.59, 5.02,
        4.52, 3.18, 3.05, 3.02, 3.39, 5.02, 0.581, 0.922, 1.41, 2.44, 3.42,
        4.41, 5.42, 6.44, 7.58, 7.59, 8.05, 8.05, 9.0
    ],
    "Phase_deg": [
        -5.54, -25.35, -45.0, -61.35, -54.07, -44.52, -24.78, -10.55, -4.29,
        -1.86, -3.02, -4.74, -6.86, -7.59, -3.82, -3.52, 4.58, 41.71, 64.29,
        74.75, 78.05, 78.05, 78.54, 78.54, 78.54, 78.54, 76.74, 76.85, 76.85,
        77.89
    ]
}

# DataFrameに変換 / Create DataFrame
df = pd.DataFrame(data)

# CSVファイルとして保存 / Save as CSV
df.to_csv("rlc_data.csv", index=False)

print(" rlc_data.csv has been created.")
from google.colab import files
files.download("rlc_data.csv")

CSVファイルを入れたらプロットしてくれるPythonコード

# =========================================
# Google Colab用:CSVアップロード + プロット一括コード
# =========================================
import pandas as pd
import matplotlib.pyplot as plt
from google.colab import files

# ===  ① CSVファイルをアップロード / Upload CSV file ===
uploaded = files.upload()
filename = next(iter(uploaded))  # 最初にアップロードされたファイル名を取得

# ===  ② CSVファイルを読み込む / Read CSV data ===
# カラム名は「Frequency_kHz」「Current_mA」「Phase_deg」を想定
df = pd.read_csv(filename)

# データの抽出 / Extract columns
freq_kHz = df["Frequency_kHz"]
current_mA = df["Current_mA"]
phase_deg = df["Phase_deg"]

# ===  ③ 線形スケールでのプロット / Linear scale plot ===
fig1, ax1 = plt.subplots(figsize=(10, 5))

ax1.set_xlabel("Frequency f [kHz]")
ax1.set_ylabel("Current [mA]", color='tab:blue')
ax1.plot(freq_kHz, current_mA, 'o-', color='tab:blue', label="Current")
ax1.tick_params(axis='y', labelcolor='tab:blue')

ax2 = ax1.twinx()
ax2.set_ylabel("Phase Difference [deg]", color='tab:orange')
ax2.plot(freq_kHz, phase_deg, 'o-', color='tab:orange', label="Phase")
ax2.tick_params(axis='y', labelcolor='tab:orange')

plt.title("RLC Series Circuit Frequency Characteristics (Linear Scale)")
fig1.tight_layout()
plt.grid(True, linestyle="--", linewidth=0.5)
plt.show()

# ===  ④ 片対数スケール(X軸のみ)/ Semilog X scale plot ===
fig2, ax3 = plt.subplots(figsize=(10, 5))

ax3.set_xlabel("Frequency f [kHz]")
ax3.set_ylabel("Current [mA]", color='tab:blue')
ax3.semilogx(freq_kHz, current_mA, 'o-', color='tab:blue', label="Current")
ax3.tick_params(axis='y', labelcolor='tab:blue')

ax4 = ax3.twinx()
ax4.set_ylabel("Phase Difference [deg]", color='tab:orange')
ax4.semilogx(freq_kHz, phase_deg, 'o-', color='tab:orange', label="Phase")
ax4.tick_params(axis='y', labelcolor='tab:orange')

plt.title("RLC Series Circuit Frequency Characteristics (Semilog X Scale)")
fig2.tight_layout()
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()

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?