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