@@ -45,9 +45,12 @@ module pyplot_module
45
45
46
46
! public methods
47
47
procedure , public :: initialize ! ! initialize pyplot instance
48
+
48
49
procedure , public :: add_plot ! ! add a 2d plot to pyplot instance
49
50
procedure , public :: add_3d_plot ! ! add a 3d plot to pyplot instance
51
+ procedure , public :: add_contour ! ! add a contour plot to pyplot instance
50
52
procedure , public :: add_bar ! ! add a barplot to pyplot instance
53
+
51
54
procedure , public :: savefig ! ! save plots of pyplot instance
52
55
procedure , public :: destroy ! ! destroy pyplot instance
53
56
@@ -260,6 +263,81 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
260
263
end subroutine add_plot
261
264
! *****************************************************************************************
262
265
266
+ ! *****************************************************************************************
267
+ ! > author: Jacob Williams
268
+ !
269
+ ! Add a contour plot.
270
+ !
271
+ ! @note This requires `use_numpy` to be True.
272
+
273
+ subroutine add_contour (me , x , y , z , label , linestyle , linewidth , levels , color )
274
+
275
+ class(pyplot), intent (inout ) :: me ! ! pyplot handler
276
+ real (wp),dimension (:), intent (in ) :: x ! ! x values
277
+ real (wp),dimension (:), intent (in ) :: y ! ! y values
278
+ real (wp),dimension (:,:), intent (in ) :: z ! ! z values (a matrix)
279
+ character (len=* ), intent (in ) :: label ! ! plot label
280
+ character (len=* ), intent (in ) :: linestyle ! ! style of the plot line
281
+ integer , intent (in ), optional :: linewidth ! ! width of the plot line
282
+ real (wp),dimension (:), intent (in ), optional :: levels ! ! contour levels to plot
283
+ character (len=* ), intent (in ), optional :: color ! ! color of the contour line
284
+
285
+ character (len= :), allocatable :: xstr ! ! x values strinfied
286
+ character (len= :), allocatable :: ystr ! ! y values strinfied
287
+ character (len= :), allocatable :: zstr ! ! z values strinfied
288
+ character (len= :), allocatable :: levelstr ! ! levels vector strinfied
289
+ character (len= max_int_len) :: iline ! ! actual line width
290
+ character (len=* ), parameter :: xname = ' x' ! ! x variable name for script
291
+ character (len=* ), parameter :: yname = ' y' ! ! y variable name for script
292
+ character (len=* ), parameter :: zname = ' z' ! ! z variable name for script
293
+ character (len=* ), parameter :: xname_ = ' X' ! ! X variable name for contour
294
+ character (len=* ), parameter :: yname_ = ' Y' ! ! Y variable name for contour
295
+ character (len=* ), parameter :: zname_ = ' Z' ! ! Z variable name for contour
296
+ character (len= :), allocatable :: extras ! ! optional stuff
297
+
298
+ if (allocated (me% str)) then
299
+
300
+ ! convert the arrays to strings:
301
+ call vec_to_string(x, xstr, me% use_numpy)
302
+ call vec_to_string(y, ystr, me% use_numpy)
303
+ call matrix_to_string(z, zstr, me% use_numpy)
304
+ if (present (levels)) call vec_to_string(levels, levelstr, me% use_numpy)
305
+
306
+ ! get optional inputs (if not present, set default value):
307
+ call optional_int_to_string(linewidth, iline, ' 3' )
308
+
309
+ ! write the arrays:
310
+ call me% add_str(trim (xname)// ' = ' // xstr)
311
+ call me% add_str(trim (yname)// ' = ' // ystr)
312
+ call me% add_str(trim (zname)// ' = ' // zstr)
313
+ call me% add_str(' ' )
314
+
315
+ ! convert inputs for contour plotting:
316
+ call me% add_str(yname_// ' , ' // xname_// ' = np.meshgrid(' // trim (xname)// ' , ' // trim (yname)// ' )' )
317
+ call me% add_str(zname_// ' = ' // zname)
318
+
319
+ ! optional arguments:
320
+ extras = ' '
321
+ if (present (levels)) extras = extras// ' ,' // ' levels=' // levelstr
322
+ if (present (color)) extras = extras// ' ,' // ' colors="' // color// ' "'
323
+ if (present (linewidth)) extras = extras// ' ,' // ' linewidths=' // trim (adjustl (iline))
324
+
325
+ ! write the plot statement:
326
+ call me% add_str(' CS = ax.contour(' // xname_// ' ,' // yname_// ' ,' // zname_// ' ,' // &
327
+ ' label="' // trim (label)// ' ",' // &
328
+ ' linestyles="' // trim (adjustl (linestyle))// ' "' // &
329
+ extras// ' )' )
330
+
331
+ call me% add_str(' ax.clabel(CS, fontsize=9, inline=1)' )
332
+ call me% add_str(' ' )
333
+
334
+ else
335
+ error stop ' Error in add_plot: pyplot class not properly initialized.'
336
+ end if
337
+
338
+ end subroutine add_contour
339
+ ! *****************************************************************************************
340
+
263
341
! *****************************************************************************************
264
342
! > author: Jacob Williams
265
343
!
@@ -454,6 +532,35 @@ subroutine vec_to_string(v, str, use_numpy)
454
532
end subroutine vec_to_string
455
533
! *****************************************************************************************
456
534
535
+ ! *****************************************************************************************
536
+ ! > author: Jacob Williams
537
+ !
538
+ ! Real matrix (rank 2) to string.
539
+
540
+ subroutine matrix_to_string (v , str , use_numpy )
541
+
542
+ real (wp), dimension (:,:), intent (in ) :: v ! ! real values
543
+ character (len= :), allocatable , intent (out ) :: str ! ! real values stringified
544
+ logical , intent (in ) :: use_numpy ! ! activate numpy python module usage
545
+
546
+ integer :: i ! ! counter
547
+ integer :: j ! ! counter
548
+ character (len= :),allocatable :: tmp ! ! dummy string
549
+
550
+ str = ' ['
551
+ do i= 1 , size (v,1 ) ! rows
552
+ call vec_to_string(v(i,:), tmp, use_numpy) ! one row at a time
553
+ str = str// trim (adjustl (tmp))
554
+ if (i< size (v)) str = str // ' ,'
555
+ end do
556
+ str = str // ' ]'
557
+
558
+ ! convert to numpy array if necessary:
559
+ if (use_numpy) str = ' np.array(' // str// ' )'
560
+
561
+ end subroutine matrix_to_string
562
+ ! *****************************************************************************************
563
+
457
564
! *****************************************************************************************
458
565
! > author: Jacob Williams
459
566
! date: 8/16/2015
0 commit comments