1. 程式人生 > >Python:matplotlib畫圖模組

Python:matplotlib畫圖模組

ipython notebookJupyter 下使用matplotlib畫圖:

%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

#畫普通影象
x = np.linspace(0.0, 1.0, 100)
y = 1-x
z = -(x*np.log2(x) + y*np.log2(y))

plt.title(u"兩面硬幣", fontproperties=font)
plt.xlabel("正面朝上概率p"
, fontproperties=font) plt.ylabel(u"熵值", fontproperties=font) plt.plot(x, z) plt.show() #畫3維圖 fig = plt.figure() ax = Axes3D(fig) x = np.linspace(0, 1, 100) y = np.linspace(0, 1, 100) x, y = np.meshgrid(x, y) from matplotlib.font_manager import FontProperties #為了正確顯示漢字 font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc"
, size=14) ax.set_title(u"三面硬幣", fontproperties=font) ax.set_xlabel("面A朝上概率", fontproperties=font) ax.set_ylabel("面B朝上概率", fontproperties=font) ax.set_zlabel(u"熵值", fontproperties=font) z = -(x*np.log2(x) + y*np.log2(y) + (1-x-y)*np.log2(1-x-y)) #ax.scatter(x,y,z,c='r') #繪點 ax.plot_surface(x, y,
z, rstride=1, cstride=1, cmap='rainbow') plt.show()