日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【数据展示】matplotlib子图设置子标题(subtitle for subplot)

發布時間:2025/4/16 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【数据展示】matplotlib子图设置子标题(subtitle for subplot) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡述

這個操作很常用。用于做對比區分

代碼范式:

ax.set_title(str)

但是結合相對應的子圖的設計卻有多種的操作方式。比如 實例2,實例3

其中實例2的例子當中,不能將nrows改成10(雖然不知道為什么)。

  • 實例1和實例2比較適用于特殊的情況(如果行或者列超過10第一方法就不太好了,但是第二種方法就可行)
  • 實例3會更加靈活。(推薦使用

實例1

import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=(6, 6.5)) for i in range(4):ax = plt.subplot(221+i)alpha = 0.98 / 4 * i + 0.01ax.set_title('%.3f' % alpha)t1 = np.arange(0.0, 1.0, 0.01)for n in [1, 2, 3, 4]:plt.plot(t1, t1 ** n, label="n=%d" % n)leg = plt.legend(loc='best', ncol=4, mode="expand", shadow=True)leg.get_frame().set_alpha(alpha)plt.savefig('1.png') plt.show()

實例2

CIFAR10為例:

導入包

import torch import torchvision import os from torch.utils.data import Dataset, DataLoader import torchvision.utils as vutils import numpy as np import matplotlib.pyplot as plt import pickle

下載并加載數據

DOWNLOAD_CIFAR10 = False cifar10_root = './cifar10/' if not (os.path.exists(cifar10_root)) or not os.listdir(cifar10_root):# not mnist dir or mnist is empyt dirDOWNLOAD_CIFAR10 = True train_data = torchvision.datasets.CIFAR10(root=cifar10_root,train=True, # this is training datatransform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]download=DOWNLOAD_CIFAR10, ) train_loader = DataLoader(dataset=train_data, batch_size=100, shuffle=True) with open('./cifar10/cifar-10-batches-py/batches.meta', 'rb') as f:data = pickle.load(f)

調用數據

for step, (x, y) in enumerate(train_loader):print(x.shape, y.shape)print(y)fig = plt.figure(figsize=(10, 10))fig, axs = plt.subplots(nrows=1, ncols=10, figsize=(10, 1.5))for i in range(10):ax = axs[i]ax.axis("off")ax.set_title(data['label_names'][y[i]])ax.imshow(np.transpose(x[i].numpy(), (1, 2, 0)))plt.savefig('cifar-10.png')plt.show()break

實例3

前面的步驟和實例2一模一樣,就不重復了。

調用數據

注意plt.axis()不可以拿出到循環外面來。

for step, (x, y) in enumerate(train_loader):print(x.shape, y.shape)print(y)fig = plt.figure(figsize=(20, 20))for i in range(100):ax = plt.subplot(10, 10, i+1)plt.axis("off")ax.set_title(data['label_names'][y[i]])plt.imshow(np.transpose(x[i].numpy(), (1, 2, 0)))plt.savefig('cifar-100.png')plt.show()break

效果類似于:

for step, (x, y) in enumerate(train_loader):print(x.shape, y.shape)print(y)fig = plt.figure(figsize=(20, 20))for i in range(100):ax = plt.subplot(10, 10, i+1)ax.axis("off")ax.set_title(data['label_names'][y[i]])plt.imshow(np.transpose(x[i].numpy(), (1, 2, 0)))plt.savefig('cifar-100.png')plt.show()break

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【数据展示】matplotlib子图设置子标题(subtitle for subplot)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。