聲明:內容全部來自《Python科學計算》(張若愚),僅供學習記錄使用。如有興趣,請尊重知識,購買正版。
使用pyplot模塊繪圖
# -*- coding: utf-8 -*-
"""
@author: xusworld
@file: FirstExample.py
@time: 2017-05-06 09:37
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10,1000)
y = np.sin(x)
z = np.cos(x**2)
plt.figure(figsize=(8,4))
plt.plot(x,y, label="$sin(x)$", color="red", linewidth=2)
plt.plot(x,z,"b--", label="$cos(x^2)$")
plt.xlabel("Times(s)")
plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2, 1.2)
plt.legend()
plt.show()
plt.figure(figsize=(8,4))
plt.plot(x,y, label="$sin(x)$", color="red", linewidth=2)
plot傳送門創建Figure對象之後,接下來調用plot()在當前的Figure對象中繪圖。實際上plot()實在Axes(子圖)對象上繪圖,如果當前的Figure對象中沒有Axes對象,將會位置
- label: 給曲線指定一個標籤,此標籤將在圖示中顯示。如果標籤字符串的前後有字符‘$’,matplotlib會使用內嵌的LaTeX引擎將其顯示爲數學公式。
- color: 指定曲線的顏色。(上面的傳送門中有相關的解釋)
- linewidth: 指定曲線的寬度,可以不是整數,也可以使用縮寫形式的參數名lw。
plt.plot(x,z,"b--", label="$cos(x^2)$")
plt.xlabel("Times(s)") plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2, 1.2)
plt.legend()
plt.show()
面向對象方式繪圖
figure = plt.gcf()
axes = plt.gca()
print(figure)
print(axes)
Output
Figure(640x480)
Axes(0.125,0.11;0.775x0.77)