Coverage for melissa/utility/plots.py: 0%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-06-13 15:44 +0200

1"""This module contains classes for creating plots.""" 

2 

3import matplotlib as mpl 

4import numpy as np 

5 

6import matplotlib.pyplot as plt 

7 

8 

9class DynamicHistogram: 

10 """ 

11 A class to create and update a dynamic histogram plot. 

12 Attributes: 

13 ----------- 

14 cmap : matplotlib.colors.Colormap 

15 The colormap used for the histogram. 

16 bins : int 

17 The number of bins for the histogram. 

18 fig : matplotlib.figure.Figure 

19 The figure object for the plot. 

20 axes : matplotlib.axes._subplots.AxesSubplot 

21 The axes object for the plot. 

22 first : int 

23 The index of the first histogram step shown. 

24 current : int 

25 The current step index. 

26 show_last : int 

27 The number of last histogram steps to show. If 0, show all steps. 

28 cbar : matplotlib.colorbar.Colorbar 

29 The colorbar for the plot. 

30 Methods: 

31 -------- 

32 __init__(self, title='Histogram', cmap='magma', bins=50, size=(8,5), show_last=0): 

33 Initializes the DynamicHistogram with the given parameters. 

34 add_histogram_step(self, data): 

35 Adds a new step to the histogram with the given data. 

36 """ 

37 

38 def __init__(self, title='Histogram', cmap='magma', bins=50, size=(8, 5), show_last=0): 

39 """ 

40 Initializes the plot with the given parameters. 

41 Parameters: 

42 title (str): The title of the plot. Default is 'Histogram'. 

43 cmap (str): The colormap to be used for the plot. Default is 'magma'. 

44 bins (int): The number of bins for the histogram. Default is 50. 

45 size (tuple): The size of the figure in inches. Default is (8, 5). 

46 show_last (int): The number of last iterations to show. Default is 0. 

47 

48 Attributes: 

49 cmap (Colormap): The colormap instance. 

50 bins (int): The number of bins for the histogram. 

51 fig (Figure): The figure object. 

52 axes (Axes): The axes object. 

53 first (int): The first iteration index. 

54 current (int): The current iteration index. 

55 show_last (int): The number of last iterations to show. 

56 cbar (Colorbar): The colorbar object. 

57 """ 

58 

59 self.cmap = plt.get_cmap(cmap) 

60 self.bins = bins 

61 self.fig, self.axes = plt.subplots(1, 1, figsize=size) 

62 self.axes.set_title(title) 

63 self.axes.set_xlabel('Value') 

64 self.axes.set_ylabel('Log-Density') 

65 self.first = 0 

66 self.current = 0 

67 self.show_last = show_last 

68 self.cbar = self.fig.colorbar( 

69 mpl.cm.ScalarMappable(cmap=self.cmap), 

70 ax=self.axes, orientation='vertical', label='Iteration') 

71 

72 def add_histogram_step(self, data): 

73 # 1. update counter 

74 self.current += 1 

75 

76 # 2. add histogram 

77 self.axes.hist(data, bins=self.bins, histtype='stepfilled', log=True) 

78 

79 # 3. update colors 

80 for i, color in enumerate(map(self.cmap, np.linspace(0, 1, self.current - self.first))): 

81 self.axes.patches[i].set_facecolor(color) 

82 

83 # 4. update xlim 

84 xmin, xmax = min(data), max(data) 

85 if (self.axes.get_xlim()[0] / xmin) <= 0.5 or xmin < self.axes.get_xlim()[0]: 

86 self.axes.set_xlim(xmin, None) 

87 if (xmax / self.axes.get_xlim()[1]) <= 0.5 or xmax > self.axes.get_xlim()[1]: 

88 self.axes.set_xlim(None, xmax) 

89 

90 # 5. when theres enough artists - remove first 

91 if self.show_last != 0 and self.current > self.show_last: 

92 self.axes.patches[0].remove() 

93 self.first = self.current - self.show_last 

94 

95 # 6. update colorbar limit 

96 self.cbar.mappable.set_norm(mpl.colors.Normalize(self.first, self.current))