说明
1、Matplotlib函数可以绘制图形,使用plot函数绘制曲线。
2、需要将200个点的x坐标和Y坐标分别以序列的形式输入plot函数,然后调用show函数来显示图形。
实例
import matplotlib.pyplot as plt #200个点的x坐标 x=range(-100,100) #生成y点的坐标 y=[i**2 for i in x ] #绘制一元二次曲线 plt.plot(x,y) #调用savefig将一元二次曲线保存为result.jpg plt.savefig('result.jpg') #如果直接写成 plt.savefig('cos') 会生成cos.png plt.show()
实例扩展:
import matplotlib.pyplot as plt # 定义定义域[-10, 11] x_val = [i for i in range(-10,11)] # 求解应变量 y_val = [5*x**2 for x in x_val] print(x_val) print(y_val) # 设置matplotlib作图工具 fig, ax = plt.subplots() ax.plot(x_val, y_val) ax.set(xlabel='x(independentVariable)', ylabel='y(strain)', title='On the equation of a quadratic function') ax.grid() # 保存图片 fig.savefig("test.png") # 展示图片 plt.show()
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)