サブプロットもしたい

サブプロットを含めた修正されたコードは以下のようになります。

python
import matplotlib.pyplot as plt # スタイルを設定 plt.style.use('ggplot') # データを定義 x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # サブプロットを作成 fig, ax = plt.subplots() # 折れ線グラフをプロット ax.plot(x, y, marker='o', linestyle='-', color='b', label='Data') # サブプロットにタイトルと軸ラベルを追加 ax.set_title('Sample Line Plot') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') # 凡例を表示 ax.legend() # グリッドを表示 ax.grid(True) # グラフを表示 plt.show()

修正されたコードでは、plt.subplots()を使用してサブプロットを作成し、そこにデータをプロットしています。また、サブプロットのタイトルと軸ラベルを設定するために、ax.set_title()ax.set_xlabel()ax.set_ylabel()を使用しています。

未分類

Posted by ぼっち