1. 使用tkinter库
import tkinter库时提示:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
提示很明显了,我当前macos big sur系统未配置tk,此时仅需安装与python版本匹配的tk即可。
brew install python-tk@3.10
2. 绘制函数图像
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('TkAgg')
x = np.arange(-10, 10, 0.1)
y = []
for t in x:
y_1 = 1 / (1 + math.exp(-t))
y.append(y_1)
plt.plot(x, y, label="sigmoid")
plt.xlabel("x")
plt.ylabel("y")
plt.ylim(0, 1)
plt.legend()
plt.show()