【Python】matplotlib.pyplot 常用技巧

导入matplotlib.pyplot包

import matplotlib.pyplot as plt

使用ax进行绘图

我们推荐使用ax进行绘图;
这样一个画布 figure (代码中的 fig )可以有多个子图像;

# 初始化整个画布,一个画布可以有若干子图
fig = plt.figure()

# 添加子图
ax1 = fig.add_subplot(1,1,1)	# 1*1 的图像域,这个 ax1 是第 1 个子图像
#等价于:ax1 = fig.add_subplot(111)
fig, ax = plt.subplots()		# 如果只有 1 个子图,也可以这么写

# 在子图上绘制曲线
ax1.plot(x, y, label="value of y", ls=':')		# x,y 为自变量和因变量的列表
# ls: 线型,'-'实线; ':'虚线;'-.'点划线;
ax1.set(
	xlim=[0.5, 4.5], 		# 设定 x 的定义域
	ylim=[-2, 8], 			# 设定 y 的定义域
	title='An Example Axes', 	# 设定子图的名称
	xlabel='X-Axis',
	ylabel='Y-Axis', 
	fontsize=40,		# 字体大小
)
# 也可以通过以下命令实现对 x,y 轴名称的标注,子图赋名
ax1.set_xlim(-5,5)
ax1.set_ylim([-5,5])
ax1.set_xlabel('X-Axis')
ax1.set_ylabel('Y-Axis')
ax1.legend(title="figure of x & y", loc=1)		# loc=1 代表图例在右上方
ax1.set_title('figure', fontsize=30)

# 显示画布与保存图像
# 子图显示网格
ax1.grid(True, color='green', axis='x',alpha=0.5)
# True 表示显示网格,green 表示网格颜色,axis 表示网格方向,alpha 表示明暗程度
plt.savefig("./figure.png")		# 保存图像
plt.show()		# 是否显示图像

此时在macOS和Linux系统下可保存为相对路径;
windows的vscode编辑器下保存为绝对路径;

可复用代码如下;

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
fig.set_size_inches(20.0, 10.0)
ax1.plot(x, y, label="value of y", color="g", marker='^', linewidth = '2')
ax1.set_xlim(-10,10)
ax1.set_ylim(-10,10)
ax1.set_xlabel('X', fontsize=30)
ax1.set_ylabel('Y', fontsize=30)
plt.yticks(fontproperties='Times New Roman', size=30)
plt.xticks(fontproperties='Times New Roman', size=30)
ax1.legend(title="figure of x & y", loc=1, fontsize=30)
ax1.grid(True,alpha=0.5)
plt.savefig("./figure.png",dpi=100)
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是常用Python 画图代码示例: 1. 散点图 ```python import matplotlib.pyplot as plt import numpy as np x = np.random.rand(50) y = np.random.rand(50) plt.scatter(x,y) plt.show() ``` 2. 折线图 ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) y = np.sin(x) plt.plot(x,y) plt.show() ``` 3. 条形图 ```python import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y = np.random.rand(5) plt.bar(x,y) plt.show() ``` 4. 直方图 ```python import matplotlib.pyplot as plt import numpy as np x = np.random.randn(1000) plt.hist(x,bins=30) plt.show() ``` 5. 饼图 ```python import matplotlib.pyplot as plt import numpy as np sizes = [15, 30, 45, 10] labels = ['A', 'B', 'C', 'D'] explode = (0, 0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) plt.axis('equal') plt.show() ``` 6. 热力图 ```python import matplotlib.pyplot as plt import numpy as np x = np.random.randn(100,100) plt.imshow(x, cmap='hot', interpolation='nearest') plt.show() ``` 7. 3D图 ```python import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.gca(projection='3d') x = np.linspace(-10,10,100) y = np.linspace(-10,10,100) X,Y = np.meshgrid(x,y) Z = np.sin(np.sqrt(X**2+Y**2)) surf = ax.plot_surface(X,Y,Z,cmap='coolwarm') plt.show() ``` 这些是常用Python 画图代码示例,你可以根据需要进行修改和优化。同时,也可以通过查阅 Matplotlib 官方文档来学习更多的画图技巧

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值