如何使用 Matplotalib 以對數刻度視覺化值?


要使用 Matplotlib 以對數刻度視覺化值,我們可以使用 yscale('log')

步驟

  • 匯入 Matplotlib 和 Numpy。

  • 設定圖形大小並調整子圖間和周圍的填充。

  • 使用 Numpy 建立 xy 資料點。

  • 使用 yscale('log') 以對數刻度視覺化值。

  • 使用 plot 方法繪製 xy 資料點。

  • 在圖形上放置圖例。

  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# x and y data points
x = np.linspace(1, 100, 1000)
y = np.log(x)

# logarithmic scale
plt.yscale('log')

# Plot the x and y data points
plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)")

# Place the legend
plt.legend()

# Display the plot
plt.show()

輸出

它將產生以下輸出 −

更新於: 2022 年 2 月 1 日

5 千+ 瀏覽量

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.