Python matplotlib画曲线例题解析
作者:行易软件技术团队 时间:2022-10-18 14:17:30
这篇文章主要介绍了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(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()
来源:https://www.cnblogs.com/exesoft/p/12266176.html
标签:Python,matplotlib
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python通过4种方式实现进程数据通信
2023-11-04 15:13:48
在 Python 中如何将天数添加到日期
2023-02-09 03:34:30
DOM 精简知识教程
2013-07-18 07:28:33
![](https://img.aspxhome.com/file/UploadPic/20071/200713110268848s.gif)
Oracle 实现类似SQL Server中自增字段的一个办法
2009-08-02 07:51:00
Python设计模式之简单工厂模式实例详解
2022-11-03 04:43:08
![](https://img.aspxhome.com/file/2023/8/76128_0s.png)
javascript使用location.search的示例
2023-07-16 09:14:40
python django 原生sql 获取数据的例子
2023-02-16 18:35:02
Go语言字符串基础示例详解
2023-07-17 03:14:56
![](https://img.aspxhome.com/file/2023/9/90489_0s.png)
text-overflow实现标题省略号功能
2009-05-19 12:13:00
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2023-11-18 01:11:16
网页栅格系统研究:蛋糕的切法
2008-10-24 17:07:00
![](https://img.aspxhome.com/file/UploadPic/200810/24/20081024171957433s.jpg)
利用不同样式改变相同xhtml结构的布局
2008-08-20 18:17:00
Python Flask请求扩展与中间件相关知识总结
2022-03-10 17:03:06
MYSQL教程:表达式操作符和数据类型转换
2009-02-27 15:51:00
对Python中 \\r, \\n, \\r\\n的彻底理解
2022-09-14 07:00:15
![](https://img.aspxhome.com/file/2023/7/72117_0s.jpg)
详解利用Pytorch实现ResNet网络之评估训练模型
2023-06-13 16:23:24
Firebug 必须掌握的技巧
2009-12-21 20:04:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/01-66s.jpg)
INPUT的对齐问题
2008-05-24 08:32:00
数据安全之MySQL安全的二十三条军规
2008-12-24 16:26:00
python如何编写win程序
2022-12-09 11:48:38
![](https://img.aspxhome.com/file/2023/6/76856_0s.png)