Coverage for arosics/plotting.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-04-03 14:59 +0000

1# -*- coding: utf-8 -*- 

2 

3# AROSICS - Automated and Robust Open-Source Image Co-Registration Software 

4# 

5# Copyright (C) 2017-2024 

6# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de) 

7# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam, 

8# Germany (https://www.gfz-potsdam.de/) 

9# 

10# This software was developed within the context of the GeoMultiSens project funded 

11# by the German Federal Ministry of Education and Research 

12# (project grant code: 01 IS 14 010 A-C). 

13# 

14# Licensed under the Apache License, Version 2.0 (the "License"); 

15# you may not use this file except in compliance with the License. 

16# You may obtain a copy of the License at 

17# 

18# https://www.apache.org/licenses/LICENSE-2.0 

19# 

20# Unless required by applicable law or agreed to in writing, software 

21# distributed under the License is distributed on an "AS IS" BASIS, 

22# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

23# See the License for the specific language governing permissions and 

24# limitations under the License. 

25 

26import numpy as np 

27 

28 

29def _norm(array, normto): 

30 return [float(i) * (normto / max(array)) for i in array] 

31 

32 

33def subplot_2dline(XY_tuples, titles=None, shapetuple=None, grid=False): 

34 from matplotlib import pyplot as plt 

35 

36 shapetuple = (1, len(XY_tuples)) if shapetuple is None else shapetuple 

37 assert titles is None or len(titles) == len(XY_tuples), \ 

38 'List in titles keyword must have the same length as the passed XY_tuples.' 

39 fig = plt.figure(figsize=_norm(plt.figaspect(shapetuple[0] / shapetuple[1] * 1.), 10)) 

40 for i, XY in enumerate(XY_tuples): 

41 ax = fig.add_subplot(shapetuple[0], shapetuple[1], i + 1) 

42 X, Y = XY 

43 ax.plot(X, Y, linestyle='-') 

44 if titles is not None: 

45 ax.set_title(titles[i]) 

46 if grid: 

47 ax.grid(which='major', axis='both', linestyle='-') 

48 plt.tight_layout() 

49 plt.show(block=True) 

50 

51 return fig 

52 

53 

54def subplot_imshow(ims, titles=None, shapetuple=None, grid=False): 

55 from matplotlib import pyplot as plt 

56 

57 ims = [ims] if not isinstance(ims, list) else ims 

58 assert titles is None or len(titles) == len(ims), 'Error: Got more or less titles than images.' 

59 

60 shapetuple = (1, len(ims)) if shapetuple is None else shapetuple 

61 fig, axes = plt.subplots(shapetuple[0], shapetuple[1], 

62 figsize=_norm(plt.figaspect(shapetuple[0] / shapetuple[1] * 1.), 20)) 

63 [axes[i].imshow(im, cmap='gray', interpolation='none', vmin=np.percentile(im, 2), vmax=np.percentile(im, 98)) 

64 for i, im in enumerate(ims)] 

65 if titles is not None: 

66 [axes[i].set_title(titles[i]) for i in range(len(ims))] 

67 if grid: 

68 [axes[i].grid(which='major', axis='both', linestyle='-') for i in range(len(ims))] 

69 plt.tight_layout() 

70 plt.show(block=True) 

71 

72 return fig 

73 

74 

75def subplot_3dsurface(ims, shapetuple=None): 

76 from matplotlib import pyplot as plt 

77 from mpl_toolkits.mplot3d import Axes3D # noqa: F401 # this is needed for fig.add_subplot(..., projection='3d') 

78 

79 ims = [ims] if not isinstance(ims, list) else ims 

80 shapetuple = (1, len(ims)) if shapetuple is None else shapetuple 

81 fig = plt.figure(figsize=_norm(plt.figaspect((shapetuple[0] / 2.) / shapetuple[1] * 1.), 20)) 

82 for i, im in enumerate(ims): 

83 ax = fig.add_subplot(shapetuple[0], shapetuple[1], i + 1, projection='3d') 

84 x = np.arange(0, im.shape[0], 1) 

85 y = np.arange(0, im.shape[1], 1) 

86 X, Y = np.meshgrid(x, y) 

87 Z = im.reshape(X.shape) 

88 ax.plot_surface(X, Y, Z, cmap=plt.get_cmap('hot')) 

89 ax.contour(X, Y, Z, zdir='x', cmap=plt.get_cmap('coolwarm'), offset=0) 

90 ax.contour(X, Y, Z, zdir='y', cmap=plt.get_cmap('coolwarm'), offset=im.shape[1]) 

91 ax.set_xlabel('X') 

92 ax.set_ylabel('Y') 

93 ax.set_zlabel('Z') 

94 plt.tight_layout() 

95 plt.show(block=True) 

96 

97 return fig