import sys reload(sys) sys.setdefaultencoding('utf-8') import matplotlib.pyplot as plt import numpy as np plt.figure(1)#實例化作圖變量 plt.title("single variable")#圖像標題 plt.xlabel('x') plt.ylabel('y') plt.axis([0, 5,0, 10])#x軸范圍是0-5,y軸范圍是0-10 plt.grid(True)# 是否網格化 xx = np.linspace(0, 5, 10)#在0-5之間產生10個數據 plt.plot(xx, 2 * xx, 'g-') plt.show() plt.figure(2)#實例化作圖變量 plt.title("single variable")#圖像標題 plt.xlabel('x') plt.ylabel('y') plt.axis([-12, 12,-1, 1])#x軸范圍是-12-12,y軸范圍是-1-1 plt.grid(True)# 是否網格化 xx = np.linspace(-12, 12, 100)#在-12-12之間產生100個數據 plt.plot(xx, np.sin(xx), 'g-', label="$sin(x)$") plt.plot(xx, np.cos(xx), 'r--', label="$cos(x)$") plt.legend() plt.show()
#繪制多軸圖 def draw(plt): plt.axis([-12, 12,-1, 1])#x軸范圍是-12-12,y軸范圍是-1-1 plt.grid(True)# 是否網格化 xx = np.linspace(-12, 12, 100)#在-12-12之間產生100個數據 plt.plot(xx, np.sin(xx), 'g-', label="$sin(x)$") plt.plot(xx, np.cos(xx), 'r--', label="$cos(x)$") plt.legend() plt.figure(3) plt1 = plt.subplot(2,2,1)#兩行兩列中第一個 draw(plt1) plt2 = plt.subplot(2,2,2)#兩行兩列中第2個 draw(plt2) plt3 = plt.subplot(2,2,3)#兩行兩列中第3個 draw(plt3) plt4 = plt.subplot(2,2,4)#兩行兩列中第4個 draw(plt4)
from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(1,1,1, projection='3d') theta = np.linspace(-4 * np.pi, 4 * np.pi, 500) z = np.linspace(0,2,500) r =z x = r * np.sin(theta) y = r * np.cos(theta) ax.plot(x, y, z, label='curve') ax.legend() plt.show() fig = plt.figure() ax = fig.add_subplot(1,1,1, projection='3d') xx = np.linspace(0, 5, 10) yy = np.linspace(0, 5, 10) zz1 = xx zz2 = 2 * xx; zz3 = 3 * xx ax.scatter(xx, yy, zz1, c = 'red', marker='o') ax.scatter(xx, yy, zz2, c = 'green', marker='^') ax.scatter(xx, yy, zz3, c = 'black', marker='*') ax.legend() plt.show() from matplotlib import cm fig = plt.figure() ax = fig.gca( projection='3d') X = np.arange(-5,5,0.25) Y = np.arange(-5,5,0.25) X, Y = np.meshgrid(X, Y) Z = X ** 2 + Y ** 2 ax.plot_surface(X, Y, Z, rstride=1,cstride =1, cmap =cm.coolwarm,\ linewidth = 0,antialiased = False) plt.show()