如何使用 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