• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python matplotlib画曲线例题解析

python 搞代码 4年前 (2022-01-08) 41次浏览 已收录 0个评论

这篇文章主要介绍了Python matplotlib画曲线例题解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了Python matplotlib画曲线例题解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

初学者,练习以下片段:

代码1:用 一元一次函数 画直线

 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-2, 2, 50) print(x) y = 2*x + 1 plt.plot(x, y) ax = plt.gca() ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0)) plt.show()

代码2:用 一元二次函数 画抛物线

 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-4, 6, 50) print(type(x)) print(x) y = x**2 - x*2+1 plt.plot(x, y) plt.show()

代码3:用反比例函数 画曲线

 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-11, 11, 110) print(x) y =10/x plt.plot(x, y) ax = plt.gca() ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0)) plt.show()

代码4:子坐标系的应用

 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(224) ax1.set(<em style="color:transparent">来源[email protected]搞@^&代*@码网</em>xlim=[0.5, 4.5], ylim=[-2, 8], title='Axes Show', ylabel='Y', xlabel='X') plt.show()

代码5:子坐标系的应用

 import matplotlib.pyplot as plt fig = plt.figure() fig,axes=plt.subplots(nrows=2, ncols=2) axes[0,0].set(xlim=[0.5, 4.5], ylim=[-2, 8], title='Axes Show', ylabel='Y', xlabel='X') axes[0,1].set(title='Upper Right') axes[1,0].set(title='Lower Left') axes[1,1].set(title='Lower Right')

代码6:用已知少量坐标值画曲线

 import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3) plt.xlim(0.5, 4.5) plt.show()

代码7:画离散点

 mport matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = np.random.randn(10) print(y) plt.scatter(x, y, color='red', marker='+') plt.show()

代码8:画出正弦曲线

 import numpy as np import matplotlib.pyplot as plt x=np.arange(-2*np.pi,2*np.pi,0.01) y=np.sin(x) plt.plot(x,y) ax = plt.gca() ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0)) plt.xticks([-np.pi*2,-np.pi*3/2,-np.pi, -np.pi/2, 0, np.pi/2, np.pi,np.pi*3/2,np.pi*2],[r'$-2\pi$',r'$-\frac{3}{2} \pi$',r'$-\pi$',r'$-\frac{1}{2}\pi$', r'0', r'$\frac{1}{2}\pi$','$\pi$',r'$\frac{3}{2}\pi$',r'$2 \pi$']) plt.show()

以上就是Python matplotlib画曲线例题解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python matplotlib画曲线例题解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址