Pythonのmatplotlib
ライブラリを使用して、折れ線グラフの線の種類や太さをカスタマイズする方法を解説します。
線の種類のカスタマイズ
matplotlib
のAxes.plot()
メソッドで描く線の種類はlinestyle
オプションで設定できます。以下に具体的なコードを示します。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(-4, 4, 0.1)
y = x
ax.set_xlabel("x", size = 14)
ax.set_ylabel("y", size = 14)
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()
線の太さのカスタマイズ
線の太さはlinewidth
オプションで指定します。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (6.8, 5))
ax = fig.add_subplot(111)
x = np.arange(-4, 4, 0.1)
y = np.exp(-x**2)
ax.set_xlabel("x", size = 14)
ax.set_ylabel("y", size = 14)
ax.plot(x - 2, y, linewidth = 1, label = "linewidth = 1")
ax.plot(x, y, linewidth = 2, label = "linewidth = 2")
ax.plot(x + 2, y, linewidth = 4, label = "linewidth = 4")
ax.legend()
以上のように、Pythonのmatplotlib
ライブラリを使用することで、折れ線グラフの線の種類や太さを簡単にカスタマイズすることができます。これらのテクニックを活用して、データの視覚化をより効果的に行いましょう。.