使用matplotlib实现画图

来源:互联网 发布:退伍军人召回通知软件 编辑:程序博客网 时间:2024/05/01 12:26
画图主要分为两部分:函数和图像
使用matplotlib画图,先把函数表达出来(通常使用numpy第三方库),然后pyplot模块plot就可以了。

1、函数部分:
y=f(x),将x 和 y 都表达出来:
eg:
from matplotlib import  pyplot as plt
import numpy as np
x =np.arange(-7,10,0.5)                     #的取值范围
out = np.zeros(x.shape[0])                  #初始化J()

for in range(x.shape[0]):
    h = 0
    for in np.arange(1,4,1):
        y = ((x[j]*i-i)**2)
        h = h + y
    out[j] = (1/(2*np.shape(range(3))[0]))*h

上面的代码其实是实现函数:


的部分值

2、画图部分:直接使用pyplot模块即可,如下:
plt.plot(x,out,"r")
plt.show()

结果如下:

0 0
原创粉丝点击