matplotlib基础用法

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基础用法

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基础用法
matplotlib基础用法

matplotlib把两个曲线画在同一个图里

plt.plot(x, y1, color="red", linewidth=1.0, linestyle="--")
plt.plot(x, y2, color="blue", linewidth=2.0, linestyle="-")
plt.show()

matplotlib基础用法

设置坐标值范围和描述

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()

matplotlib基础用法

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")
# 把x轴的刻度设置为bottom
# 把y轴的刻度设置为left
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
# 设置bootom对应到0点
# 设置left对应到0点
ax.spines["bottom"].set_position(('data', 0))
ax.spines["left"].set_position(('data', 0))

plt.show()

matplotlib基础用法

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")
# 把x轴的刻度设置为bottom
# 把y轴的刻度设置为left
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
# 设置bootom对应到0点
# 设置left对应到0点
ax.spines["bottom"].set_position(('data', 0))
ax.spines["left"].set_position(('data', 0))
plt.show()

matplotlib基础用法

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")
# 把x轴的刻度设置为bottom
# 把y轴的刻度设置为left
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
# 设置bootom对应到0点
# 设置left对应到0点
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()

matplotlib基础用法

绘制散点图

plt.scatter(np.arange(5),np.arange(5))
plt.show()

matplotlib基础用法

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()

matplotlib基础用法

绘制直方图

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()

matplotlib基础用法

绘制子图像

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()

matplotlib基础用法

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()

matplotlib基础用法