对比 sin(x^2) 与 sin(x)^2 ,请先看:python:绘制 sin(x^2) 函数的曲线
编写 test_sinx_2.py 如下
# -*- coding: utf-8 -*-
""" 绘制函数 y = sin(x)^2 在 -6<=x<=6 的曲线 """
import numpy as np
from matplotlib import pyplot as plt
# 用于正常显示中文标题,负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(-6., 6., 0.01)
y = np.sin(x)**2
print(len(x))
# 可视化
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y) # 画曲线
#axes.axis('scaled') # 用缩尺制图
axes.axis('equal')
plt.title('函数 y = sin(x)^2 的曲线')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
运行 python test_sinx_2.py