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()
posted @ 2018-12-26 09:27  xuejianbest  阅读(147)  评论(0编辑  收藏  举报