\

PythonのデータビジュアライゼーションライブラリであるMatplotlibを使用して、線グラフを描画する方法について説明します。

線グラフの描画

まずは、基本的な線グラフの描画方法から始めましょう。以下のコードは、xとyのデータをプロットする簡単な例です。

import matplotlib.pyplot as plt
import numpy as np

# データの作成
x = np.linspace(-6, 6, 1000)
y = np.exp(-x**2)

# 線グラフの描画
plt.plot(x, y)
plt.show()

線のスタイルの変更

次に、線のスタイルを変更する方法について見ていきましょう。linestyleパラメータを使用して、線の種類を指定できます。以下の例では、4種類の異なる線スタイルを描画しています。

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.arange(-4, 4, 0.1)
y = x

ax.plot(x, y + 2, linestyle = "solid", label = "solid")
ax.plot(x, y + 1, linestyle = "dashed", label = "dashed")
ax.plot(x, y, linestyle = "dashdot", label = "dashdot")
ax.plot(x, y - 1, linestyle = "dotted", label = "dotted")

ax.legend()
plt.show()

線の太さと色の変更

線の太さはlinewidthパラメータ、色はcolorパラメータで指定できます。

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.linspace(-6, 6, 1000)
y = np.exp(-x**2)

ax.plot(x, y, color='red', linestyle='solid', linewidth = 3.0)
ax.plot(x, y/2, color='green', linestyle='dashed', linewidth = 2.0)
ax.plot(x, y/3, color='blue', linestyle='dashdot', linewidth = 1.0)

plt.show()

以上がPythonのMatplotlibを使用して線グラフを描画する基本的な方法です。これらの基本をマスターすれば、さまざまなデータを視覚化することが可能になります。.

投稿者 admin

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です