matplotlib中的pyplot实用详解

matplotlib中的pyplot实用详解

示例1:使用axes确定子图的位置

matplotlib中的pyplot实用详解
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)

plt.subplot(211)
plt.imshow(np.random.random((10, 10)))

plt.subplot(212)
plt.imshow(np.random.random((10, 10)))

plt.subplots_adjust(left=0, bottom=0, right=1, top=1)

cax = plt.axes([0.8, 0, 0.06, 1])
plt.colorbar(cax=cax)

plt.show()

相关函数详解

1、plt.subplot()

官网参考链接:matplotlib-pyplot-subplot
输入的参数有以下4种情况:

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)

三个整数(nrows、ncols、index)。子图将占据nrows行和ncols列网格上的索引位置。索引从左上角的1开始,向右增加。index也可以是指定子时隙的(第一个,最后一个)索引(以1为基础,包括最后一个索引)的两元组,例如,fig.add_subplot(3,1,(1,2))构成了一个跨越图上部2/3的子时隙。

matplotlib中的pyplot实用详解
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as mtransforms

np.random.seed(0)

fig = plt.figure(figsize=(10,20))

ax1 = plt.subplot(4, 2, 1, title="first subplot (4,2,1)")
plt.imshow(np.random.random((10, 10)))

ax2 = plt.subplot(422, frame_on=False, title="no frame subplot")
plt.imshow(np.random.random((10, 10)))

ax3 = plt.subplot(423, projection='polar', title="polar subplot")
xs = np.arange(7)
ys = xs**2
trans_offset = mtransforms.offset_copy(ax3.transData, fig=fig, y=5, units='dots')
'''
x, y : float, default: 0.0 -> x,y方向上偏移的距离.

'''
for x, y in zip(xs, ys):
    plt.polar(x, y, 'ro')
    plt.text(x, y, '%d, %d' % (int(x), int(y)),
            transform=trans_offset,
            horizontalalignment='center',
            verticalalignment='bottom')

plt.subplot(424, sharex=ax2, facecolor='red', title="red facecolor subplot")
plt.imshow(np.random.random((10, 10)))

plt.subplot(4,2,(5, 6), sharex=ax2, title="corss (5,6) subplot")
plt.imshow(np.random.random((10, 28)))

plt.subplot(4,2,7, title="about ticks change subplot 1")
plt.minorticks_on()
plt.tick_params(which='major',width=4)

plt.subplot(4,2,8, title="about ticks change subplot 2")
plt.minorticks_off()
plt.tick_params(top=True,bottom=False,left=False,right=False)
plt.tick_params(labeltop=True,labelleft=False,labelright=False,labelbottom=False)

plt.show()

2、plt.subplots_adjust() 与 plt.axes()来控制子图的位置

plt.subplots()能够自动确定子图的位置,但是不够灵活,而通过plt.subplots_adjust() 与 plt.axes()就可以实现子图任意位置的摆放。
假设我现在有如下长条状的图片 imgs:

matplotlib中的pyplot实用详解
然后通过plt.subplots_adjust() 与 plt.axes()我可以让多张这样的图片根据我喜欢的位置进行排列。其用法如下:
plt.subplots_adjust():
Parameters:
left: float, optional
子图左边缘的位置,作为图形宽度的一部分。
right:float, optional
子图右边缘的位置,作为图形宽度的一部分。
bottom:float, optional
子图底边缘的位置,作为图形宽度的一部分。
top:float, optional
子图顶边缘的位置,作为图形宽度的一部分。
wspace:float, optional
子图之间的填充宽度,作为平均轴宽度的一部分。
hspace:float, optional
子图之间的填充高度,作为平均轴宽度的一部分。

plt.axes()
最主要的Parameters:
rect: [left, bottom, width, height] ->分别代表子图的左下点位置,以及子图的宽、高。

matplotlib中的pyplot实用详解

def imshow(imgs):
    imgs = imgs / 2.0 + 0.5
    imgs = imgs.cpu().numpy().transpose((1, 2, 0))
    plt.axis("off")
    plt.imshow(imgs)

plt.axes(facecolor='grey')

plt.tick_params(top=False,bottom=False,left=False,right=False)
plt.tick_params(labeltop=False,labelleft=False,labelright=False,labelbottom=False)

plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
plt.axes([0, 0.9, 1, 0.15])
imshow(imgs)

plt.axes([0, 0.75, 1, 0.15])
imshow(imgs)

plt.axes([0, 0.6, 1, 0.15])
imshow(imgs)

plt.axes([0, 0.45, 1, 0.15])
imshow(imgs)

plt.show()

Original: https://blog.csdn.net/shizuguilai/article/details/127682561
Author: 氏族归来
Title: matplotlib中的pyplot实用详解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/767849/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球