python 100天 97 画一个)的sin cos函数
画正弦曲线
先定义X,Y坐标关系
import numpy as npimport matplotlib.pyplot as pltplt.figure(figsize=(8,5), dpi=80)plt.subplot(111)X = np.linspace(-np.pi, np.pi, 256,endpoint=True)C = np.cos(X)S = np.sin(X)
先重新定义坐标系
ax = plt.gca()ax.spines[&39;].set_color(&39;)ax.spines[&39;].set_color(&39;)ax.xaxis.set_ticks_position(&39;)ax.spines[&39;].set_position((&39;,0))ax.yaxis.set_ticks_position(&39;)ax.spines[&39;].set_position((&39;,0))plt.xlim(X.min() * 1.1, X.max() * 1.1)plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r&39;, r&39;, r&39;, r&39;, r&39;])plt.ylim(C.min() * 1.1, C.max() * 1.1)plt.yticks([-1, +1],[r&39;, r&39;])plt.legend(loc=&39;)
默认情况 下的是这样的
接着画线
plt.plot(X, C, color=&34;, linewidth=2.5, linestyle=&34;, label=&34;)plt.plot(X, S, color=&34;, linewidth=2.5, linestyle=&34;, label=&34;)
最后这样效果如下
&39;&39;&39;import numpy as npimport matplotlib.pyplot as pltplt.figure(figsize=(8,5), dpi=80)plt.subplot(111)X = np.linspace(-np.pi, np.pi, 256,endpoint=True)C = np.cos(X)S = np.sin(X)plt.plot(X, C, color=&34;, linewidth=2.5, linestyle=&34;, label=&34;)plt.plot(X, S, color=&34;, linewidth=2.5, linestyle=&34;, label=&34;)ax = plt.gca()ax.spines[&39;].set_color(&39;)ax.spines[&39;].set_color(&39;)ax.xaxis.set_ticks_position(&39;)ax.spines[&39;].set_position((&39;,0))ax.yaxis.set_ticks_position(&39;)ax.spines[&39;].set_position((&39;,0))plt.xlim(X.min() * 1.1, X.max() * 1.1)plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r&39;, r&39;, r&39;, r&39;, r&39;])plt.ylim(C.min() * 1.1, C.max() * 1.1)plt.yticks([-1, +1],[r&39;, r&39;])plt.legend(loc=&39;)plt.show()