如何使用 Matplotlib 将 y 轴置于对数刻度?
默认情况下,使用 Matplotlib 的所有图中的轴都是线性的, matplotlib.pyplot库的yscale()方法可用于将 y 轴刻度更改为对数。
方法 yscale() 将单个值作为参数,它是比例转换的类型,要将 y 轴转换为对数比例,我们将“log”关键字或 matplotlib.scale.LogScale 类传递给 yscale 方法。
Syntax : matplotlib.pyplot.yscale(value, **kwargs)
Parameters:
- Value = { “linear”, “log”, “symlog”, “logit”, … }
- **kwargs = Different keyword arguments are accepted, depending on the scale (matplotlib.scale.LinearScale, LogScale, SymmetricalLogScale, LogitScale)
Returns : Converts the y-axes to the given scale type. (Here we use the “log” scale type)
线性比例示例:
Python3
import matplotlib.pyplot as plt
data = [10**i for i in range(4)]
plt.plot(data)
Python3
import matplotlib.pyplot as plt
data = [10**i for i in range(4)]
# convert y-axis to Logarithmic scale
plt.yscale("log")
plt.plot(data)
输出:
对数刻度示例:
蟒蛇3
import matplotlib.pyplot as plt
data = [10**i for i in range(4)]
# convert y-axis to Logarithmic scale
plt.yscale("log")
plt.plot(data)
输出: