fplot 1.7.1
A Fortran library providing a convenient interface for plotting with Gnuplot.
Loading...
Searching...
No Matches
fplot_plot_data_histogram.f90
1! fplot_plot_data_histogram.f90
2
3submodule(fplot_core) fplot_plot_data_histogram
4contains
5! ------------------------------------------------------------------------------
6pure module function pdh_get_bin_count(this) result(x)
7 class(plot_data_histogram), intent(in) :: this
8 integer(int32) :: x
9 x = this%m_binCount
10end function
11
12! ------------------------------------------------------------------------------
13module subroutine pdh_set_bin_count(this, x)
14 class(plot_data_histogram), intent(inout) :: this
15 integer(int32), intent(in) :: x
16 this%m_binCount = x
17end subroutine
18
19! ------------------------------------------------------------------------------
20module function pdh_bin_data(this, x, err) result(bx)
21 ! Arguments
22 class(plot_data_histogram), intent(in) :: this
23 real(real64), intent(in), dimension(:) :: x
24 class(errors), intent(inout), optional, target :: err
25 real(real64), allocatable, dimension(:,:) :: bx
26
27 ! Local Variables
28 real(real64) :: maxX, minX, width, val
29 integer(int32) :: i, j, flag, n, nbins
30 real(real64), allocatable, dimension(:,:) :: ranges
31 class(errors), pointer :: errmgr
32 type(errors), target :: deferr
33
34 ! Initialization
35 if (present(err)) then
36 errmgr => err
37 else
38 errmgr => deferr
39 end if
40 n = size(x)
41 nbins = this%get_bin_count()
42
43 ! Get the max and min of the entire data set
44 maxx = maxval(x)
45 minx = minval(x)
46 width = (maxx - minx) / (nbins - 1.0)
47
48 ! Allocate space for the output
49 allocate(bx(nbins, 2), stat = flag)
50 if (flag == 0) allocate(ranges(nbins, 2), stat = flag)
51 if (flag /= 0) then
52 call errmgr%report_error("pdh_bin_data", &
53 "Insufficient memory available.", plot_out_of_memory_error)
54 return
55 end if
56 bx = 0.0d0
57
58 ! Define each range
59 ranges(1,:) = [minx, minx + width]
60 do i = 2, nbins
61 ranges(i,1) = ranges(i-1,2)
62 ranges(i,2) = ranges(i,1) + width
63 end do
64
65 ! Construct the bins
66 do i = 1, n
67 val = x(i)
68 do j = 1, nbins
69 if ((val >= ranges(j,1)) .and. (val <= ranges(j,2))) then
70 bx(j,1) = bx(j,1) + 1.0d0 ! Counter
71 exit ! Exit the inner do loop
72 end if
73 end do
74 end do
75
76 ! Now compute the center of each bin - store in column 2 of bx
77 bx(:,2) = 0.5d0 * (ranges(:,1) + ranges(:,2))
78end function
79
80! ------------------------------------------------------------------------------
81pure module function pdh_get_extremes(this) result(x)
82 ! Arguments
83 class(plot_data_histogram), intent(in) :: this
84 real(real64), dimension(2) :: x
85
86 ! Local Variables
87 integer(int32) :: i, j, nrows, ncols
88 real(real64) :: maxX, minX, val
89 logical :: check
90
91 ! Initialization
92 nrows = this%get_count()
93 ncols = this%get_bar_per_label_count()
94 check = .true.
95
96 ! Process
97 do j = 1, ncols
98 do i = 1, nrows
99 val = this%get(i, j)
100 if (check) then
101 maxx = val
102 minx = val
103 check = .false.
104 else
105 if (val > maxx) maxx = val
106 if (val < minx) minx = val
107 end if
108 end do
109 end do
110
111 ! End
112 x = [minx, maxx]
113end function
114
115! ------------------------------------------------------------------------------
116module subroutine pdh_set_data_1(this, x, err)
117 ! Arguments
118 class(plot_data_histogram), intent(inout) :: this
119 real(real64), intent(in), dimension(:) :: x
120 class(errors), intent(inout), optional, target :: err
121
122 ! Local Variables
123 real(real64), allocatable, dimension(:,:) :: bx
124 class(errors), pointer :: errmgr
125 type(errors), target :: deferr
126
127 ! Initialization
128 if (present(err)) then
129 errmgr => err
130 else
131 errmgr => deferr
132 end if
133
134 ! Bin the data
135 bx = this%bin_data(x, errmgr)
136 if (errmgr%has_error_occurred()) return
137
138 ! Call the base routine to store the data - use the average values to
139 ! establish labels for the x-axis
140 call this%plot_data_bar%set_data_3(bx(:,2), bx(:,1), &
141 this%get_number_format(), errmgr)
142end subroutine
143
144! ------------------------------------------------------------------------------
145module subroutine pdh_set_data_2(this, labels, x, err)
146 ! Arguments
147 class(plot_data_histogram), intent(inout) :: this
148 class(string), intent(in), dimension(:) :: labels
149 real(real64), intent(in), dimension(:) :: x
150 class(errors), intent(inout), optional, target :: err
151
152 ! Local Variables
153 real(real64), allocatable, dimension(:,:) :: bx
154 class(errors), pointer :: errmgr
155 type(errors), target :: deferr
156
157 ! Initialization
158 if (present(err)) then
159 errmgr => err
160 else
161 errmgr => deferr
162 end if
163
164 ! Ensure the labels array is the same size as the number of bins
165 if (size(labels) /= this%get_bin_count()) then
166 call errmgr%report_error("pdh_set_data_2", &
167 "The labels array must be the same size as the number of bins.", &
168 plot_array_size_mismatch_error)
169 return
170 end if
171
172 ! Call the base routine to store the data
173 call this%plot_data_bar%set_data_2(labels, bx(:,1), errmgr)
174end subroutine
175
176! ------------------------------------------------------------------------------
177module subroutine pdh_set_data_3(this, labels, x, fmt, err)
178 ! Arguments
179 class(plot_data_histogram), intent(inout) :: this
180 real(real64), intent(in), dimension(:) :: labels
181 real(real64), intent(in), dimension(:) :: x
182 character(len = *), intent(in), optional :: fmt
183 class(errors), intent(inout), optional, target :: err
184
185 ! Local Variables
186 real(real64), allocatable, dimension(:,:) :: bx
187 class(errors), pointer :: errmgr
188 type(errors), target :: deferr
189
190 ! Initialization
191 if (present(err)) then
192 errmgr => err
193 else
194 errmgr => deferr
195 end if
196
197 ! Ensure the labels array is the same size as the number of bins
198 if (size(labels) /= this%get_bin_count()) then
199 call errmgr%report_error("pdh_set_data_3", &
200 "The labels array must be the same size as the number of bins.", &
201 plot_array_size_mismatch_error)
202 return
203 end if
204
205 ! Call the base routine to store the data
206 call this%plot_data_bar%set_data_3(labels, bx(:,1), fmt, errmgr)
207end subroutine
208
209! ------------------------------------------------------------------------------
210pure module function pdh_get_num_fmt(this) result(x)
211 class(plot_data_histogram), intent(in) :: this
212 character(len = :), allocatable :: x
213 if (allocated(this%m_numberFmt)) then
214 x = this%m_numberFmt
215 else
216 x = "(F6.2)"
217 end if
218end function
219
220! ------------------------------------------------------------------------------
221module subroutine pdh_set_num_fmt(this, x)
222 class(plot_data_histogram), intent(inout) :: this
223 character(len = *), intent(in) :: x
224 this%m_numberFmt = x
225end subroutine
226
227! ------------------------------------------------------------------------------
228end submodule
fplot_core