日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

簡述

這個操作很常用。用于做對比區(qū)分

代碼范式:

ax.set_title(str)

但是結(jié)合相對應(yīng)的子圖的設(shè)計卻有多種的操作方式。比如 實例2,實例3

其中實例2的例子當(dāng)中,不能將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為例:

導(dǎo)入包

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

下載并加載數(shù)據(jù)

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)

調(diào)用數(shù)據(jù)

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一模一樣,就不重復(fù)了。

調(diào)用數(shù)據(jù)

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

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

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。