Matplotlib标记子子图

时间:2018-08-01 07:41:05

标签: python matplotlib

我想将标签添加到包含子子图的绘图中。这就是我想要的(我用GIMP添加了外部标签)

enter image description here

这是我真正得到的:

enter image description here

这是产生最后一个情节的代码:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

plots = 16
subplots = 9

fig = plt.figure(figsize=(8, 8))

wh_plots = int(np.sqrt(plots))
wh_subplots = int(np.sqrt(subplots))

outer_grid = gridspec.GridSpec(wh_plots, wh_plots, wspace=0.1, hspace=0.1)
for p in range(plots):
    inner_grid = gridspec.GridSpecFromSubplotSpec(wh_subplots, wh_subplots, subplot_spec=outer_grid[p], wspace=0.05, hspace=0.05)
    for s in range(subplots):
        ax = plt.Subplot(fig, inner_grid[s])
        ax.imshow(np.random.rand(10,10), cmap="magma", interpolation="none")
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

        if (p+1) > 12 and s == 7:
            ax.set_xlabel("sub_xlabel")
        if (p) % 4 == 0 and s == 3:
            ax.set_ylabel("sub_ylabel")

all_axes = fig.get_axes()
plt.show()

我的问题:

  • 如何获得第一张图中的“ xlabel”和“ ylabel”?
  • 是否有更好的方法来标记子图(sub_xlabel / sub_ylabel) 相比我做了什么?

    if (p+1) > 12 and s == 7:
        ax.set_xlabel("sub_xlabel")
    if (p) % 4 == 0 and s == 3:
        ax.set_ylabel("sub_ylabel")
    

它可以工作,但是看起来不正确。

1 个答案:

答案 0 :(得分:1)

您可以在 NSURL *tempDir = [NSURL fileURLWithPath:destinationPath]; NSError *error; NSURL *tmpDirectory = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:tempDir create:YES error:&error]; if (error) { return ; } tmpDirectory = [tmpDirectory URLByAppendingPathComponent:@"extracts"]; NSLog(@"temp dir %@",tmpDirectory); NSLog(@"temp path %@",tmpDirectory.path); [SSZipArchive unzipFileAtPath:zipFilePath toDestination:tmpDirectory.path]; NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory.path error:nil]; NSLog(@"dir file %@",dirFiles); for (NSString *string in dirFiles) { NSArray *dirDestinationFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:destinationPath error:nil]; NSLog(@"dir destination file %@",dirDestinationFiles); NSMutableArray *folderCount = [[NSMutableArray alloc] init]; NSMutableArray *folderNumCount = [[NSMutableArray alloc] init]; [dirDestinationFiles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj containsString:string]){ [folderNumCount addObject:obj]; } if ([string isEqualToString:obj]) { [folderCount addObject:string]; } }]; NSFileManager *fm = [NSFileManager defaultManager]; NSError *error; if (folderCount.count == 0) { NSLog(@"First time extract"); BOOL isMoved = [fm moveItemAtPath:tmpDirectory.path toPath:[destinationPath stringByAppendingString:[NSString stringWithFormat:@"/%@",string]] error:&error]; if (isMoved) { NSLog(@"Moved"); }else{ NSLog(@"errorL %@", error); NSLog(@"Not moved"); } [fm removeItemAtPath:tmpDirectory.path error:&error]; // [self moveFileToTrash:zipFilePath]; // [self openExtractedFolderWithZipPath:zipFilePath toDestinationPath:destinationPath]; }else if (folderCount.count > 0){ NSLog(@"Already present"); BOOL isMoved = [fm moveItemAtPath:tmpDirectory.path toPath:[destinationPath stringByAppendingString:[NSString stringWithFormat:@"/%@-%lu",string,folderNumCount.count-1]] error:&error]; if (isMoved) { NSLog(@"Moved"); }else{ NSLog(@"errorL %@", error); NSLog(@"Not moved"); } [fm removeItemAtPath:tmpDirectory.path error:&error]; // [self moveFileToTrash:zipFilePath]; // [self openExtractedFolderWithZipPath:zipFilePath toDestinationPath:destinationPath]; } } 之前添加以下行:

plt.show()