pycharm中實現簡單的sin和cos函式曲線圖
阿新 • • 發佈:2018-12-10
1.需要下載相應的package,主要就是numpy和matplotlib;具體程式碼如下:
#!/usr/bin/env python # encoding: utf-8 ''' @author: yangxin @contact: [email protected] @software: garner @file: number1.py @time: 2018/9/16 11:22 @desc: ''' import numpy as np import matplotlib.pyplot as plt # 設定x,y軸的數值 x1 = np.linspace(0, 15, 100) y1 = np.sin(x1) y2 = np.cos(x1) # 在當前繪圖物件中畫圖(x軸,y軸,給所繪製的曲線的名字,畫線顏色,畫線寬度) plt.plot(x1, y1, label="$sin(x)$", color="blue", linewidth=2) plt.plot(x1, y2, label="$cos(x)$", color="red", linewidth=2) # X和Y座標軸的表示 plt.xlabel("Domain") plt.ylabel("Range") # 圖表的標題 plt.title("sin and cos") # Y軸的範圍 plt.ylim(-1.5, 1.5) # 顯示圖示 plt.legend() # 顯示圖 plt.show() # 儲存圖 plt.savefig("sin&cos.png")