matplotlib plot函數使用詳解

以上圖片中的內容來自於plot函數的幫助文檔。

代碼示例:

# 繪製圖表,並設置圖表的軸刻度,刻度範圍,軸標籤
# 創建2 x 2的4個子圖
fig, axes = plt.subplots(2, 2, figsize=(8, 10), dpi=100)

#數據點
x = np.arange(10)
y = x + np.random.randn(10)

# 第一張子圖繪製虛線(紅色,數據點使用圓形標記),線寬爲2
axes[0, 0].plot(x, y, color="r", linestyle="--", marker="o", linewidth=2)

# 第二張圖繪製虛線(藍綠色,數據點使用+好標記)
axes[0, 1].plot(x, y.cumsum(), "g-.+")

# 第三張繪製散點圖(藍色,數據點使用上三角形標記)
axes[1, 0].scatter(x, y, color="b", marker="^")

# 第四張圖繪製散點圖(黑色,數據點使用下三角形標記),alpha設置圖像的透明度
axes[1, 1].scatter(x, y.cumsum(), color="k", marker="v", alpha=0.3)

# 設置座標軸的刻度,座標範圍,軸標籤
# 默認這些標記在當前的(也就是最後一個)subplot上
plt.xlim([-10, 10])
plt.ylim([-50, 50])
plt.xlabel("axis-x")
plt.ylabel("axis-y")
plt.xticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])
plt.yticks([-10, 0.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0])

# 顯式指定每個子圖的刻度,座標範圍,軸標籤
# 設置第一張子圖axes[0, 0]
axes[0, 0].set_xlabel("subplot(0, 0)-x")
axes[0, 0].set_ylabel("subplot(0, 0)-y")
axes[0, 0].set_xlim([-10, 10])
axes[0, 0].set_ylim([-5, 15])
axes[0, 0].set_xticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])
axes[0, 0].set_yticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章