如何使用 Matplotalib 以對數刻度視覺化值?
要使用 Matplotlib 以對數刻度視覺化值,我們可以使用 yscale('log')。
步驟
匯入 Matplotlib 和 Numpy。
設定圖形大小並調整子圖間和周圍的填充。
使用 Numpy 建立 x 和 y 資料點。
使用 yscale('log') 以對數刻度視覺化值。
使用 plot 方法繪製 x 和 y 資料點。
在圖形上放置圖例。
要顯示圖形,請使用 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()輸出
它將產生以下輸出 −

廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP