首页 > 解决方案 > 当 x 在 python 中介于 1 和 100 之间时,绘制 y=logx

问题描述

有人可以帮助如何绘制 y = log(x) 的图,x 在 Python 中介于 1 和 100 之间。我已经尝试了很多,但无法弄清楚如何做到这一点。

标签: pythonjupyter-notebook

解决方案


您可以尝试使用 matplotlib 库。一个简单的散点图将来自以下代码:

import matplotlib.pyplot as plt
from math import log

x = range(1, 100)
y = [log(xi) for xi in x]

plt.scatter(x, y)
plt.show()

我还使用了数学库中的 log() 函数。如果您在 jupyter notebook 中编码,则不需要最后一行代码

plt.show()

您可以用“plt”结束单元格

import matplotlib.pyplot as plt
from math import log

x = range(1, 100)
y = [log(xi) for xi in x]

plt.scatter(x, y)
# some other operations in your code
# and you end with plt
plt

推荐阅读