import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,100)
y = 2*x + 1
plt.plot(x,y)
plt.show()
matplotlib figure的用法
x = np.linspace(-1,1,100)
y1 = 2*x + 1
y2 = x**2
plt.figure()
plt.plot(x,y1)
plt.figure(figsize=(6,4))
plt.plot(x,y2)
plt.show()
matplotlib把两个曲线画在同一个图里
plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
plt.plot(x, y2, color="blue", linewidth=2.0, linestyle="-")
plt.show()
设置坐标值范围和描述
x = np.linspace(-3,3,100)
y1 = 2*x + 1
y2 = x**2
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("i am x")
plt.ylabel("i am y")
plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
plt.plot(x, y2, color="blue", linewidth=2.0, linestyle="-")
plt.show()
x = np.linspace(-3,3,100)
y1 = 2*x + 1
y2 = x**2
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("i am x")
plt.ylabel("i am y")
plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
plt.plot(x, y2, color="blue", linewidth=2.0, linestyle="-")
new_ticks = np.linspace(-2,2,11)
plt.xticks(new_ticks)
plt.yticks([-1,0,1,2,3],
["level1","level2","level3","level4","level5"])
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(('data', 0))
ax.spines["left"].set_position(('data', 0))
plt.show()
legend图例
x = np.linspace(-3,3,100)
y1 = 2*x + 1
y2 = x**2
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("i am x")
plt.ylabel("i am y")
l1, = plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
l2, = plt.plot(x, y2, color="blue", linewidth=2.0, linestyle="-")
plt.legend(handles=[l1,l2], labels=['test1','test2'], loc='best')
new_ticks = np.linspace(-2,2,11)
plt.xticks(new_ticks)
plt.yticks([-1,0,1,2,3],
["level1","level2","level3","level4","level5"])
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(('data', 0))
ax.spines["left"].set_position(('data', 0))
plt.show()
x = np.linspace(-3,3,100)
y1 = 2*x + 1
l1, = plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
plt.legend(handles=[l1], labels=['test1','test2'], loc='best')
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(('data', 0))
ax.spines["left"].set_position(('data', 0))
x0 = 0.5
y0 = 2*x0+1
plt.scatter(x0,y0,s=50,color='blue')
plt.plot([x0,x0],[y0,0],"k--",lw=2)
plt.annotate(r'$2x+1=%s$'% y0,xy=(x0,y0),xytext=(+30,-30),textcoords="offset points",fontsize=16,
arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))
plt.text(-4,2,r'$this\ is\ the\ text$',fontdict={"size":16,"color":'r'})
plt.show()
绘制散点图
plt.scatter(np.arange(5),np.arange(5))
plt.show()
x = np.random.normal(0,1,500)
y = np.random.normal(0,1,500)
plt.scatter(x,y,s=50,c='blue',alpha=0.5)
plt.xlim((-2,2))
plt.ylim((-2,2))
plt.xticks(())
plt.yticks(())
plt.show()
绘制直方图
x = np.arange(10)
y = 2**x + 10
plt.bar(x,y,facecolor="#9999ff",edgecolor="white")
for x,y in zip(x,y):
plt.text(x,y,'%.2f'%y, ha="center", va="bottom")
plt.show()
绘制子图像
plt.figure()
plt.subplot(2,2,1)
plt.plot([0,1],[0,1])
plt.subplot(2,2,2)
plt.plot([-1,1],[-1,1])
plt.subplot(2,2,3)
plt.plot([-1,1],[-1,1])
plt.subplot(2,2,4)
plt.plot([-1,1],[-1,1])
plt.show()
plt.figure()
plt.subplot(2,1,1)
plt.plot([0,1],[0,1])
plt.subplot(2,3,4)
plt.plot([-1,1],[-1,1])
plt.subplot(2,3,5)
plt.plot([-1,1],[-1,1])
plt.subplot(2,3,6)
plt.plot([-1,1],[-1,1])
plt.show()