-
Notifications
You must be signed in to change notification settings - Fork 52
/
pyplot_module.F90
1474 lines (1191 loc) · 65.8 KB
/
pyplot_module.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!*****************************************************************************************
!> author: Jacob Williams
! date: 6/16/2017
! license: BSD
!
! For making simple x-y plots from Fortran.
! It works by generating a Python script and executing it.
!
!# See also
! * Inspired by: [EasyPlot](https://pypi.python.org/pypi/EasyPlot)
!
!@note The default real kind (`wp`) can be
! changed using optional preprocessor flags.
! This library was built with real kind:
#ifdef REAL32
! `real(kind=real32)` [4 bytes]
#elif REAL64
! `real(kind=real64)` [8 bytes]
#elif REAL128
! `real(kind=real128)` [16 bytes]
#else
! `real(kind=real64)` [8 bytes]
#endif
module pyplot_module
use, intrinsic :: iso_fortran_env
implicit none
private
#ifdef REAL32
integer,parameter,public :: pyplot_wp = real32 !! real kind used by this module [4 bytes]
#elif REAL64
integer,parameter,public :: pyplot_wp = real64 !! real kind used by this module [8 bytes]
#elif REAL128
integer,parameter,public :: pyplot_wp = real128 !! real kind used by this module [16 bytes]
#else
integer,parameter,public :: pyplot_wp = real64 !! real kind used by this module [8 bytes]
#endif
integer,parameter :: wp = pyplot_wp !! local copy of `pyplot_wp` with a shorter name
character(len=*), parameter :: tmp_file = 'pyplot_module_temp_1234567890.py' !! Default name of the temporary file
!! (this can also be user-specified).
character(len=*), parameter :: python_exe ='python' !! The python executable name.
character(len=*), parameter :: int_fmt = '(I10)' !! integer format string
integer, parameter :: max_int_len = 10 !! max string length for integers
character(len=*), parameter :: real_fmt_default = '(E30.16)' !! default real number format string
integer, parameter :: max_real_len = 30 !! max string length for reals
type, public :: pyplot
!! The main pyplot class.
private
character(len=:), allocatable :: str !! string buffer
character(len=1) :: raw_str_token = ' ' !! will be 'r' if using raw strings
logical :: show_legend = .false. !! show legend into plot
logical :: use_numpy = .true. !! use numpy python module
logical :: use_oo_api = .false. !! use OO interface of matplotlib (incopatible with showfig subroutine)
logical :: mplot3d = .false. !! it is a 3d plot
logical :: polar = .false. !! it is a polar plot
logical :: axis_equal = .false. !! equal scale on each axis
logical :: axisbelow = .true. !! axis below other chart elements
logical :: tight_layout = .false. !! tight layout option
logical :: usetex = .false. !! enable LaTeX
character(len=:),allocatable :: real_fmt !! real number formatting
contains
! public methods
procedure, public :: initialize !! initialize pyplot instance
procedure, public :: add_plot !! add a 2d plot to pyplot instance
procedure, public :: add_errorbar !! add a 2d error bar plot to pyplot instance
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
procedure, public :: add_sphere !! add a 3d sphere to pyplot instance
procedure, public :: add_contour !! add a contour plot to pyplot instance
procedure, public :: plot_surface !! add a surface plot to pyplot instance
procedure, public :: add_bar !! add a barplot to pyplot instance
procedure, public :: add_imshow !! add an image plot (using `imshow`)
procedure, public :: add_hist !! add a histogram plot to pyplot instance
procedure, public :: savefig !! save plots of pyplot instance
procedure, public :: showfig !! show plots of pyplot instance
procedure, public :: destroy !! destroy pyplot instance
! private methods
procedure :: execute !! execute pyplot commands
procedure :: add_str !! add string to pytplot instance buffer
procedure :: finish_ops !! some final ops before saving
end type pyplot
contains
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Destructor.
subroutine destroy(me)
class(pyplot),intent(inout) :: me !! pyplot handler
if (allocated(me%str)) deallocate(me%str)
if (allocated(me%real_fmt)) deallocate(me%real_fmt)
me%raw_str_token = ' '
end subroutine destroy
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a string to the buffer.
subroutine add_str(me,str)
class(pyplot), intent(inout) :: me !! pyplot handler
character(len=*), intent(in) :: str !! str to be added to pyplot handler buffer
me%str = me%str//str//new_line(' ')
end subroutine add_str
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Initialize a plot
subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy, figsize, &
font_size, axes_labelsize, xtick_labelsize, ytick_labelsize, ztick_labelsize, &
legend_fontsize, mplot3d, axis_equal, polar, real_fmt, use_oo_api, axisbelow,&
tight_layout, raw_strings, usetex)
class(pyplot), intent(inout) :: me !! pyplot handler
logical, intent(in), optional :: grid !! activate grid drawing
character(len=*), intent(in), optional :: xlabel !! label of x axis
character(len=*), intent(in), optional :: ylabel !! label of y axis
character(len=*), intent(in), optional :: zlabel !! label of z axis
character(len=*), intent(in), optional :: title !! plot title
logical, intent(in), optional :: legend !! plot legend
logical, intent(in), optional :: use_numpy !! activate usage of numpy python module
integer, dimension(2), intent(in), optional :: figsize !! dimension of the figure
integer, intent(in), optional :: font_size !! font size
integer, intent(in), optional :: axes_labelsize !! size of axis labels
integer, intent(in), optional :: xtick_labelsize !! size of x axis tick lables
integer, intent(in), optional :: ytick_labelsize !! size of y axis tick lables
integer, intent(in), optional :: ztick_labelsize !! size of z axis tick lables
integer, intent(in), optional :: legend_fontsize !! size of legend font
logical, intent(in), optional :: mplot3d !! set true for 3d plots (cannot use with polar)
logical, intent(in), optional :: axis_equal !! set true for axis = 'equal'
logical, intent(in), optional :: polar !! set true for polar plots (cannot use with mplot3d)
character(len=*), intent(in), optional :: real_fmt !! format string for real numbers (examples: '(E30.16)' [default], '*')
logical, intent(in), optional :: use_oo_api !! avoid matplotlib's GUI by using the OO interface (cannot use with showfig)
logical, intent(in), optional :: axisbelow !! to put the grid lines below the other chart elements [default is true]
logical, intent(in), optional :: tight_layout !! enable tight layout [default is false]
logical, intent(in), optional :: raw_strings !! if True, all strings sent to Python are treated as
!! raw strings (e.g., r'str'). Default is False.
logical, intent(in), optional :: usetex !! if True, enable LaTeX. (default if false)
character(len=max_int_len) :: width_str !! figure width dummy string
character(len=max_int_len) :: height_str !! figure height dummy string
character(len=max_int_len) :: font_size_str !! font size dummy string
character(len=max_int_len) :: axes_labelsize_str !! size of axis labels dummy string
character(len=max_int_len) :: xtick_labelsize_str !! size of x axis tick labels dummy string
character(len=max_int_len) :: ytick_labelsize_str !! size of x axis tick labels dummy string
character(len=max_int_len) :: ztick_labelsize_str !! size of z axis tick labels dummy string
character(len=max_int_len) :: legend_fontsize_str !! size of legend font dummy string
character(len=:),allocatable :: python_fig_func !! Python's function for creating a new Figure instance
character(len=*), parameter :: default_font_size_str = '10' !! the default font size for plots
call me%destroy()
if (present(raw_strings)) then
if (raw_strings) me%raw_str_token = 'r'
end if
if (present(legend)) then
me%show_legend = legend
else
me%show_legend = .false.
end if
if (present(use_numpy)) then
me%use_numpy = use_numpy
else
me%use_numpy = .true.
end if
if (present(use_oo_api)) then
me%use_oo_api = use_oo_api
else
me%use_oo_api = .false.
end if
if (present(figsize)) then
call integer_to_string(figsize(1), width_str)
call integer_to_string(figsize(2), height_str)
end if
if (present(mplot3d)) then
me%mplot3d = mplot3d
else
me%mplot3d = .false.
end if
if (present(polar)) then
me%polar = polar
else
me%polar = .false.
end if
if (present(axis_equal)) then
me%axis_equal = axis_equal
else
me%axis_equal = .false.
end if
if (present(real_fmt)) then
me%real_fmt = trim(adjustl(real_fmt))
else
me%real_fmt = real_fmt_default
end if
if (present(tight_layout)) then
me%tight_layout = tight_layout
else
me%tight_layout = .false.
end if
if (present(usetex)) then
me%usetex = usetex
else
me%usetex = .false.
end if
call optional_int_to_string(font_size, font_size_str, default_font_size_str)
call optional_int_to_string(axes_labelsize, axes_labelsize_str, default_font_size_str)
call optional_int_to_string(xtick_labelsize, xtick_labelsize_str, default_font_size_str)
call optional_int_to_string(ytick_labelsize, ytick_labelsize_str, default_font_size_str)
call optional_int_to_string(ztick_labelsize, ztick_labelsize_str, default_font_size_str)
call optional_int_to_string(legend_fontsize, legend_fontsize_str, default_font_size_str)
me%str = ''
call me%add_str('#!/usr/bin/env python')
call me%add_str('')
call me%add_str('import matplotlib')
if (me%use_oo_api) then
call me%add_str('from matplotlib.figure import Figure')
call me%add_str('from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas')
else
call me%add_str('import matplotlib.pyplot as plt')
endif
if (me%mplot3d) call me%add_str('from mpl_toolkits.mplot3d import Axes3D')
if (me%use_numpy) call me%add_str('import numpy as np')
call me%add_str('')
call me%add_str('matplotlib.rcParams["font.family"] = "Serif"')
call me%add_str('matplotlib.rcParams["font.size"] = '//trim(font_size_str))
call me%add_str('matplotlib.rcParams["axes.labelsize"] = '//trim(axes_labelsize_str))
call me%add_str('matplotlib.rcParams["xtick.labelsize"] = '//trim(xtick_labelsize_str))
call me%add_str('matplotlib.rcParams["ytick.labelsize"] = '//trim(ytick_labelsize_str))
call me%add_str('matplotlib.rcParams["legend.fontsize"] = '//trim(legend_fontsize_str))
if (me%usetex) call me%add_str('matplotlib.rcParams["text.usetex"] = True')
call me%add_str('')
if (me%use_oo_api) then
python_fig_func = 'Figure'
else
python_fig_func = 'plt.figure'
endif
if (present(figsize)) then !if specifying the figure size
call me%add_str('fig = '//python_fig_func//'(figsize=('//trim(width_str)//','//trim(height_str)//'),facecolor="white")')
else
call me%add_str('fig = '//python_fig_func//'(facecolor="white")')
end if
if (me%mplot3d) then
call me%add_str('ax = fig.add_subplot(1, 1, 1, projection=''3d'')')
elseif (me%polar) then
call me%add_str('ax = fig.add_subplot(1, 1, 1, projection=''polar'')')
else
call me%add_str('ax = fig.add_subplot(1, 1, 1)')
end if
if (present(grid)) then
if (grid) call me%add_str('ax.grid()')
end if
if (present(axisbelow)) then
me%axisbelow = axisbelow
else
me%axisbelow = .true. ! default
end if
if (me%axisbelow) call me%add_str('ax.set_axisbelow(True)')
if (present(xlabel)) call me%add_str('ax.set_xlabel('//trim(me%raw_str_token)//'"'//trim(xlabel)//'")')
if (present(ylabel)) call me%add_str('ax.set_ylabel('//trim(me%raw_str_token)//'"'//trim(ylabel)//'")')
if (present(zlabel)) call me%add_str('ax.set_zlabel('//trim(me%raw_str_token)//'"'//trim(zlabel)//'")')
if (present(title)) call me%add_str('ax.set_title('//trim(me%raw_str_token)//'"' //trim(title) //'")')
call me%add_str('')
end subroutine initialize
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add an x,y plot.
subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, ylim, xscale, yscale, color, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp), dimension(:), intent (in) :: x !! x values
real(wp), dimension(:), intent (in) :: y !! y values
character(len=*), intent (in) :: label !! plot label
character(len=*), intent (in) :: linestyle !! style of the plot line
integer, intent (in), optional :: markersize !! size of the plot markers
integer, intent (in), optional :: linewidth !! width of the plot line
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
real(wp),dimension(:), intent (in), optional :: color !! RGB color tuple [0-1,0-1,0-1]
integer, intent (out), optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: arg_str !! the arguments to pass to `plot`
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: ystr !! y values stringified
character(len=:), allocatable :: xlimstr !! xlim values stringified
character(len=:), allocatable :: ylimstr !! ylim values stringified
character(len=:), allocatable :: color_str !! color values stringified
character(len=max_int_len) :: imark !! actual markers size
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
if (allocated(me%str)) then
if (present(istat)) istat = 0
!axis limits (optional):
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(markersize, imark, '3')
call optional_int_to_string(linewidth, iline, '3')
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
call me%add_str('')
!main arguments for plot:
arg_str = trim(xname)//','//&
trim(yname)//','//&
trim(me%raw_str_token)//'"'//trim(linestyle)//'",'//&
'linewidth='//trim(adjustl(iline))//','//&
'markersize='//trim(adjustl(imark))//','//&
'label='//trim(me%raw_str_token)//'"'//trim(label)//'"'
! optional arguments:
if (present(color)) then
if (size(color)<=3) then
call vec_to_string(color(1:3), '*', color_str, use_numpy=.false., is_tuple=.true.)
arg_str = arg_str//',color='//trim(color_str)
end if
end if
!write the plot statement:
call me%add_str('ax.plot('//arg_str//')')
!axis limits:
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
!axis scales:
if (present(xscale)) call me%add_str('ax.set_xscale('//trim(me%raw_str_token)//'"'//xscale//'")')
if (present(yscale)) call me%add_str('ax.set_yscale('//trim(me%raw_str_token)//'"'//yscale//'")')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
end if
end subroutine add_plot
!*****************************************************************************************
!*****************************************************************************************
!> author: Jimmy Leta
!
! Add a histogram plot.
subroutine add_hist(me, x, label, xlim, ylim, xscale, yscale, bins, normed, cumulative, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp), dimension(:), intent (in) :: x !! array of data
character(len=*), intent (in) :: label !! plot label
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
integer, intent (in), optional :: bins !! number of bins
logical, intent (in), optional :: normed !! boolean flag that determines whether bin counts are normalized [NO LONGER USED]
logical, intent (in), optional :: cumulative !! boolean flag that determines whether histogram represents the cumulative density of dataset
integer, intent (out),optional :: istat !! status output (0 means no problems)
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: xlimstr !! xlim values stringified
character(len=:), allocatable :: ylimstr !! ylim values stringified
character(len=:), allocatable :: cumulativestr !!
character(len=max_int_len) :: binsstr !!
if (allocated(me%str)) then
if (present(istat)) istat = 0
!axis limits (optional):
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str('')
!get optional inputs (if not present, set default value):
call optional_int_to_string(bins, binsstr, '10')
call optional_logical_to_string(cumulative, cumulativestr, 'False')
!write the plot statement:
call me%add_str('ax.hist('//&
trim(xname)//','//&
'label='//trim(me%raw_str_token)//'"'//trim(label)//'",'//&
'bins='//trim(binsstr)//','//&
'cumulative='//trim(cumulativestr)//')')
!axis limits:
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
!axis scales:
if (present(xscale)) call me%add_str('ax.set_xscale('//trim(me%raw_str_token)//'"'//xscale//'")')
if (present(yscale)) call me%add_str('ax.set_yscale('//trim(me%raw_str_token)//'"'//yscale//'")')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
end if
end subroutine add_hist
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a contour plot.
!
!@note This requires `use_numpy` to be True.
subroutine add_contour(me, x, y, z, linestyle, linewidth, levels, color, &
filled, cmap, colorbar, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp),dimension(:), intent (in) :: x !! x values
real(wp),dimension(:), intent (in) :: y !! y values
real(wp),dimension(:,:), intent (in) :: z !! z values (a matrix)
character(len=*), intent (in) :: linestyle !! style of the plot line
integer, intent (in), optional :: linewidth !! width of the plot line [only used when `filled=False`]
real(wp),dimension(:), intent (in), optional :: levels !! contour levels to plot
character(len=*), intent (in), optional :: color !! color of the contour line
logical, intent (in), optional :: filled !! use filled control (default=False)
character(len=*), intent (in), optional :: cmap !! colormap if filled=True (examples: 'jet', 'bone')
logical, intent (in), optional :: colorbar !! add a colorbar (default=False)
integer, intent (out),optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: ystr !! y values stringified
character(len=:), allocatable :: zstr !! z values stringified
character(len=:), allocatable :: levelstr !! levels vector stringified
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
character(len=*), parameter :: zname = 'z' !! z variable name for script
character(len=*), parameter :: xname_ = 'X' !! X variable name for contour
character(len=*), parameter :: yname_ = 'Y' !! Y variable name for contour
character(len=*), parameter :: zname_ = 'Z' !! Z variable name for contour
character(len=:), allocatable :: extras !! optional stuff
character(len=:), allocatable :: contourfunc !! 'contour' or 'contourf'
logical :: is_filled !! if it is a filled contour plot
if (allocated(me%str)) then
if (present(istat)) istat = 0
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
call matrix_to_string(z, me%real_fmt, zstr, me%use_numpy)
if (present(levels)) call vec_to_string(levels, me%real_fmt, levelstr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(linewidth, iline, '3')
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
call me%add_str(trim(zname)//' = '//zstr)
call me%add_str('')
!convert inputs for contour plotting:
call me%add_str(xname_//', '//yname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
call me%add_str(zname_//' = np.transpose('//zname//')')
!optional arguments:
extras = ''
if (present(levels)) extras = extras//','//'levels='//levelstr
if (present(color)) extras = extras//','//'colors='//trim(me%raw_str_token)//'"'//color//'"'
if (present(linewidth)) extras = extras//','//'linewidths='//trim(adjustl(iline))
if (present(cmap)) extras = extras//','//'cmap='//trim(me%raw_str_token)//'"'//cmap//'"'
!filled or regular:
contourfunc = 'contour' !default
is_filled = .false.
if (present(filled)) then
is_filled = filled
if (filled) contourfunc = 'contourf' !filled contour
end if
!write the plot statement:
call me%add_str('CS = ax.'//contourfunc//'('//xname_//','//yname_//','//zname_//','//&
'linestyles='//trim(me%raw_str_token)//'"'//trim(adjustl(linestyle))//'"'//&
extras//')')
if (present(colorbar)) then
if (colorbar) call me%add_str('fig.colorbar(CS)')
end if
if (.not. is_filled) call me%add_str('ax.clabel(CS, fontsize=9, inline=1)')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
end if
end subroutine add_contour
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a surface plot.
!
!@note This requires `use_numpy` to be True.
subroutine plot_surface(me, x, y, z, label, linestyle, linewidth, levels, color, &
cmap, colorbar, antialiased, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp),dimension(:), intent (in) :: x !! x values
real(wp),dimension(:), intent (in) :: y !! y values
real(wp),dimension(:,:), intent (in) :: z !! z values (a matrix)
character(len=*), intent (in) :: label !! plot label
character(len=*), intent (in) :: linestyle !! style of the plot line
integer, intent (in), optional :: linewidth !! width of the plot line
real(wp),dimension(:), intent (in), optional :: levels !! contour levels to plot
character(len=*), intent (in), optional :: color !! Color of the surface patches
character(len=*), intent (in), optional :: cmap !! colormap if filled=True (examples: 'jet', 'bone')
logical, intent (in), optional :: colorbar !! add a colorbar (default=False)
logical, intent (in), optional :: antialiased !! The surface is made opaque by using antialiased=False
integer, intent (out), optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: ystr !! y values stringified
character(len=:), allocatable :: zstr !! z values stringified
character(len=:), allocatable :: levelstr !! levels vector stringified
character(len=:), allocatable :: antialiasedstr !! antialiased stringified
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
character(len=*), parameter :: zname = 'z' !! z variable name for script
character(len=*), parameter :: xname_ = 'X' !! X variable name for contour
character(len=*), parameter :: yname_ = 'Y' !! Y variable name for contour
character(len=*), parameter :: zname_ = 'Z' !! Z variable name for contour
character(len=:), allocatable :: extras !! optional stuff
if (allocated(me%str)) then
if (present(istat)) istat = 0
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
call matrix_to_string(z, me%real_fmt, zstr, me%use_numpy)
if (present(levels)) call vec_to_string(levels, me%real_fmt, levelstr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(linewidth, iline, '3')
call optional_logical_to_string(antialiased, antialiasedstr, 'False')
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
call me%add_str(trim(zname)//' = '//zstr)
call me%add_str('')
!convert inputs for contour plotting:
call me%add_str(xname_//', '//yname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
call me%add_str(zname_//' = np.transpose('//zname//')')
!optional arguments:
extras = ''
if (present(levels)) extras = extras//','//'levels='//levelstr
if (present(color)) extras = extras//','//'colors='//trim(me%raw_str_token)//'"'//color//'"'
if (present(linewidth)) extras = extras//','//'linewidths='//trim(adjustl(iline))
if (present(cmap)) extras = extras//','//'cmap='//trim(me%raw_str_token)//'"'//cmap//'"'
!write the plot statement:
call me%add_str('CS = ax.plot_surface'//'('//xname_//','//yname_//','//zname_//','//&
'label='//trim(me%raw_str_token)//'"'//trim(label)//'",'//&
'antialiased='//trim(antialiasedstr)//','//&
'linestyles='//trim(me%raw_str_token)//'"'//trim(adjustl(linestyle))//'"'//&
extras//')')
if (present(colorbar)) then
if (colorbar) call me%add_str('fig.colorbar(CS)')
end if
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
end if
end subroutine plot_surface
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a 3D x,y,z plot.
!
!@note Must initialize the class with ```mplot3d=.true.```
subroutine add_3d_plot(me, x, y, z, label, linestyle, markersize, linewidth, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp), dimension(:), intent (in) :: x !! x values
real(wp), dimension(:), intent (in) :: y !! y values
real(wp), dimension(:), intent (in) :: z !! z values
character(len=*), intent (in) :: label !! plot label
character(len=*), intent (in) :: linestyle !! style of the plot line
integer, intent (in), optional :: markersize !! size of the plot markers
integer, intent (in), optional :: linewidth !! width of the plot line
integer, intent (out), optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: ystr !! y values stringified
character(len=:), allocatable :: zstr !! z values stringified
character(len=max_int_len) :: imark !! actual markers size
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
character(len=*), parameter :: zname = 'z' !! z variable name for script
if (allocated(me%str)) then
if (present(istat)) istat = 0
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
call vec_to_string(z, me%real_fmt, zstr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(markersize, imark, '3')
call optional_int_to_string(linewidth, iline, '3')
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
call me%add_str(trim(zname)//' = '//zstr)
call me%add_str('')
!write the plot statement:
call me%add_str('ax.plot('//&
trim(xname)//','//&
trim(yname)//','//&
trim(zname)//','//&
trim(me%raw_str_token)//'"'//trim(linestyle)//'",'//&
'linewidth='//trim(adjustl(iline))//','//&
'markersize='//trim(adjustl(imark))//','//&
'label='//trim(me%raw_str_token)//'"'//trim(label)//'")')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_3d_plot: pyplot class not properly initialized.'
end if
end subroutine add_3d_plot
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a sphere to a 3D x,y,z plot.
!
!@note Must initialize the class with `mplot3d=.true.` and `use_numpy=.true.`.
subroutine add_sphere(me, r, xc, yc, zc, n_facets, linewidth, antialiased, color, istat)
implicit none
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp), intent (in) :: r !! radius of the sphere
real(wp), intent (in) :: xc !! x value of sphere center
real(wp), intent (in) :: yc !! y value of sphere center
real(wp), intent (in) :: zc !! z value of sphere center
integer, intent (in), optional :: n_facets !! [default is 100]
integer, intent (in), optional :: linewidth !! line width
logical, intent (in), optional :: antialiased !! enabled anti-aliasing
character(len=*), intent (in), optional :: color !! color of the contour line
integer, intent (out), optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: rstr !! `r` value stringified
character(len=:), allocatable :: xcstr !! `xc` value stringified
character(len=:), allocatable :: ycstr !! `yc` value stringified
character(len=:), allocatable :: zcstr !! `zc` value stringified
character(len=*), parameter :: xname = 'x' !! `x` variable name for script
character(len=*), parameter :: yname = 'y' !! `y` variable name for script
character(len=*), parameter :: zname = 'z' !! `z` variable name for script
character(len=max_int_len) :: linewidth_str !! `linewidth` input stringified
character(len=:), allocatable :: antialiased_str !! `antialised` input stringified
character(len=max_int_len) :: n_facets_str !! `n_facets` input stringified
character(len=:), allocatable :: extras !! optional stuff string
if (allocated(me%str)) then
!get optional inputs (if not present, set default value):
call optional_int_to_string(n_facets, n_facets_str, '100')
extras = ''
if (present(linewidth)) then
call optional_int_to_string(linewidth, linewidth_str, '1')
extras = extras//','//'linewidth='//linewidth_str
end if
if (present(antialiased)) then
call optional_logical_to_string(antialiased, antialiased_str, 'False')
extras = extras//','//'antialiased='//antialiased_str
end if
if (present(color)) then
extras = extras//','//'color='//trim(me%raw_str_token)//'"'//trim(color)//'"'
end if
if (present(istat)) istat = 0
!convert the arrays to strings:
call real_to_string(r , me%real_fmt, rstr)
call real_to_string(xc, me%real_fmt, xcstr)
call real_to_string(yc, me%real_fmt, ycstr)
call real_to_string(zc, me%real_fmt, zcstr)
! sphere code:
call me%add_str('u = np.linspace(0, 2 * np.pi, '//n_facets_str//')')
call me%add_str('v = np.linspace(0, np.pi, '//n_facets_str//')')
call me%add_str(xname//' = '//xcstr//' + '//rstr//' * np.outer(np.cos(u), np.sin(v))')
call me%add_str(yname//' = '//ycstr//' + '//rstr//' * np.outer(np.sin(u), np.sin(v))')
call me%add_str(zname//' = '//zcstr//' + '//rstr//' * np.outer(np.ones(np.size(u)), np.cos(v))')
call me%add_str('ax.plot_surface('//xname//', '//yname//', '//zname//extras//')')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_sphere: pyplot class not properly initialized.'
end if
end subroutine add_sphere
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
!
! Add a bar plot.
subroutine add_bar(me, x, height, label, width, bottom, color, &
yerr, align, xlim, ylim, xscale, yscale, istat)
class(pyplot), intent(inout) :: me !! pyplot handler
real(wp), dimension(:), intent(in) :: x !! x bar values
real(wp), dimension(:), intent(in) :: height !! height bar values
character(len=*), intent(in) :: label !! plot label
real(wp), dimension(:), intent(in), optional :: width !! width values
real(wp), dimension(:), intent(in), optional :: bottom !! bottom values
character(len=*), intent(in), optional :: color !! plot color
real(wp), dimension(:), intent(in), optional :: yerr !! yerr values
character(len=*), intent(in), optional :: align !! default: 'center'
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
integer, intent (out),optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: xstr !! x axis values stringified
character(len=:), allocatable :: ystr !! y axis values stringified
character(len=:), allocatable :: xlimstr !! xlim values stringified
character(len=:), allocatable :: ylimstr !! ylim values stringified
character(len=:), allocatable :: wstr !! width values stringified
character(len=:), allocatable :: bstr !! bottom values stringified
character(len=:), allocatable :: plt_str !! plot string
character(len=:), allocatable :: yerr_str !! yerr values stringified
character(len=*), parameter :: xname = 'x' !! x axis name
character(len=*), parameter :: yname = 'y' !! y axis name
character(len=*), parameter :: wname = 'w' !! width name
character(len=*), parameter :: bname = 'b' !! bottom name
character(len=*), parameter :: yerrname = 'yerr' !! yerr name
if (allocated(me%str)) then
if (present(istat)) istat = 0
!axis limits (optional):
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(height, me%real_fmt, ystr, me%use_numpy)
if (present(width)) call vec_to_string(width, me%real_fmt, wstr, me%use_numpy)
if (present(bottom)) call vec_to_string(bottom, me%real_fmt, bstr, me%use_numpy)
if (present(yerr)) call vec_to_string(yerr, me%real_fmt, yerr_str, me%use_numpy)
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
if (present(yerr)) call me%add_str(trim(yerrname)//' = '//yerr_str)
call me%add_str('')
!create the plot string:
plt_str = 'ax.bar('//&
'x='//trim(xname)//','//&
'height='//trim(yname)//','
if (present(yerr)) plt_str=plt_str//'yerr='//trim(yerrname)//','
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
if (present(color)) plt_str=plt_str//'color='//trim(me%raw_str_token)//'"'//trim(color)//'",'
if (present(align)) plt_str=plt_str//'align='//trim(me%raw_str_token)//'"'//trim(align)//'",'
plt_str=plt_str//'label='//trim(me%raw_str_token)//'"'//trim(label)//'")'
!write the plot statement:
call me%add_str(plt_str)
!axis limits:
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
!axis scales:
if (present(xscale)) call me%add_str('ax.set_xscale('//trim(me%raw_str_token)//'"'//xscale//'")')
if (present(yscale)) call me%add_str('ax.set_yscale('//trim(me%raw_str_token)//'"'//yscale//'")')
call me%add_str('')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_bar: pyplot class not properly initialized.'
end if
end subroutine add_bar
!*****************************************************************************************
!*****************************************************************************************
!>
! Add an image plot using `imshow`.
!
!### Note
! * Based on code by Ricardo Torres, 4/2/2017.
subroutine add_imshow(me, x, xlim, ylim, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp),dimension(:,:),intent (in) :: x !! x values
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
integer, intent (out),optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: xstr !! x values stringified
character(len=*), parameter :: xname = 'x' !! x variable name for script
!axis limits (optional):
character(len=:), allocatable :: xlimstr !! xlim values stringified
character(len=:), allocatable :: ylimstr !! ylim values stringified
if (allocated(me%str)) then
if (present(istat)) istat = 0
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
!convert the arrays to strings:
call matrix_to_string(x, me%real_fmt, xstr, me%use_numpy)
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str('')
!write the plot statement:
call me%add_str('ax.imshow('//trim(xname)//')')
call me%add_str('')
!axis limits:
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
else
if (present(istat)) istat = -1
write(error_unit,'(A)') 'Error in add_imshow: pyplot class not properly initialized.'
end if
end subroutine add_imshow
!*****************************************************************************************
!*****************************************************************************************
!> author: Alexander Sandrock
!
! Add an x,y plot with errorbars.
subroutine add_errorbar(me, x, y, label, linestyle, xerr, yerr, markersize, linewidth, xlim, ylim, xscale, yscale, color, istat)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp), dimension(:), intent (in) :: x !! x values
real(wp), dimension(:), intent (in) :: y !! y values
character(len=*), intent (in) :: label !! plot label
character(len=*), intent (in) :: linestyle !! style of the plot line
real(wp), dimension(:), intent (in), optional :: xerr !! x errorbar sizes
real(wp), dimension(:), intent (in), optional :: yerr !! y errorbar sizes
integer, intent (in), optional :: markersize !! size of the plot markers
integer, intent (in), optional :: linewidth !! width of the plot line
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
real(wp),dimension(:), intent (in), optional :: color !! RGB color tuple [0-1,0-1,0-1]
integer, intent (out),optional :: istat !! status output (0 means no problems)
character(len=:), allocatable :: arg_str !! the arguments to pass to `plot`
character(len=:), allocatable :: xstr !! x values stringified
character(len=:), allocatable :: ystr !! y values stringified
character(len=:), allocatable :: xlimstr !! xlim values stringified
character(len=:), allocatable :: ylimstr !! ylim values stringified
character(len=:), allocatable :: xerrstr !! xerr values stringified
character(len=:), allocatable :: yerrstr !! yerr values stringified
character(len=:), allocatable :: color_str !! color values stringified
character(len=max_int_len) :: imark !! actual markers size
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
character(len=*), parameter :: xerrname = 'xerr' !! xerr variable name for script
character(len=*), parameter :: yerrname = 'yerr' !! yerr variable name for script
if (allocated(me%str)) then
if (present(istat)) istat = 0
!axis limits (optional):
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
!errorbar sizes (optional):
if (present(xerr)) call vec_to_string(xerr, me%real_fmt, xerrstr, me%use_numpy)
if (present(yerr)) call vec_to_string(yerr, me%real_fmt, yerrstr, me%use_numpy)
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(markersize, imark, '3')