语言, Python专题

Python中Matplotlib库的常用语句

这是之前的一篇:使用Matplotlib画图。这里列出Matplotlib常用语句。语句的具体用法推荐直接在官网上搜索:https://matplotlib.org/stable/contents.html。关于颜色,可参考这篇:论文中图片的配色(以Matplotlib为例)

引入包

import matplotlib.pyplot as plt

定义图

fig = plt.figure()  
fig = plt.figure(figsize=(16,9))  # 定义画布大小 

调整位置

plt.subplots_adjust(left=0, right=1, bottom=0, top=1)   # 贴边显示 
plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)   # 贴边显示,子图之间紧贴

显示图例

plt.legend(['a', 'b'])

定义子图

ax = fig.add_subplot(111)  
ax = fig.add_subplot(111, projection='3d')  
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

画图

ax.plot(x_array, y_array)
ax.scatter(x_array, y_array)
contour = ax.contourf(x_array,y_array,matrix,cmap='jet') 
surf = ax.plot_surface(x_array, y_array, matrix, cmap=cm.coolwarm, linewidth=0, antialiased=False) 

标题

ax.set_title(titlename_string, fontsize=20, fontfamily='Times New Roman')

坐标的范围

ax.set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)

坐标名称和字体

ax.set_xlabel('x', fontsize=30, fontfamily='Times New Roman') 
ax.set_ylabel('y', fontsize=30, fontfamily='Times New Roman')
ax.set_zlabel('z', fontsize=30, fontfamily='Times New Roman')

坐标刻度的字体

labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() 
[label.set_fontname('Times New Roman') for label in labels] 

坐标刻度的字体大小

ax.tick_params(labelsize=20)

坐标刻度的显示格式

from matplotlib.ticker import LinearLocator
from matplotlib.ticker import MultipleLocator

ax.xaxis.set_major_locator(LinearLocator(6)) # 设置x轴主刻度的个数(和间隔是覆盖的关系)
ax.xaxis.set_major_locator(MultipleLocator(0.5)) # 设置x轴主刻度间隔ax.xaxis.set_minor_locator(MultipleLocator(0.1)) # 设置x轴次刻度间隔
ax.xaxis.set_major_formatter('{x:.2f}') # 设置x轴主刻度的格式

ax.yaxis.set_major_locator(LinearLocator(6)) # 设置y轴主刻度的个数(和间隔是覆盖的关系)
ax.yaxis.set_major_locator(MultipleLocator(5)) # 设置y轴主刻度间隔ax.yaxis.set_minor_locator(MultipleLocator(1)) # 设置y轴次刻度间隔
ax.yaxis.set_major_formatter('{x:.2f}') # 设置y轴主刻度的格式

ax.tick_params(which='major', length=12) # 定义主刻度的长度
ax.tick_params(which='minor', length=6) # 定义次刻度的长度

显示网格

ax.grid()

固定轴的纵横比

ax.set_aspect('equal')  # 设置相等的纵横比
ax.set_aspect(2) # 表示 y 轴的单位长度是 x 轴的两倍。

关闭坐标轴

ax.axis('off')

三维图调整视图

ax.view_init(elev=0, azim=90)

设置color bar

contour = ax.contourf(x_array,y_array,matrix,cmap='jet')
cax = plt.axes([0.75, 0.15, 0.08, 0.73]) # 位置 [左,下,宽度, 高度]
cbar = fig.colorbar(contour, cax=cax) # 定义contour的color bar
[l.set_family('Times New Roman') for l in cbar.ax.yaxis.get_ticklabels()] # 设置color bar刻度的字体
cbar.ax.tick_params(labelsize=15) # 设置color bar刻度的字体大小 

保存图片

plt.savefig('a.jpg') 
plt.savefig('a.jpg', dpi=300) 
plt.savefig('a.eps') 

显示图片

plt.show()

关闭所有

plt.close('all')

说明:该命令在循环画图时推荐使用。

读取和显示图片

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
a = mpimg.imread('a.jpg')
plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0) 
plt.imshow(a)
plt.axis('off')
plt.show()

拼接两个图片

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
a1 = mpimg.imread('a1.jpg')
a2 = mpimg.imread('a2.jpg')
fig = plt.figure(figsize=(16,9))
plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0) 
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.imshow(a1)
ax2.imshow(a1)
ax1.axis('off')
ax2.axis('off')
plt.show()
# plt.savefig('a1_a2.jpg')

1,336 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code