| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: Access Matplotlib from Perl; providing consistent user interface between different plot types |
|
2
|
2
|
|
|
2
|
|
126416
|
use strict; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
66
|
|
|
3
|
2
|
|
|
2
|
|
8
|
use feature 'say'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
2496
|
|
|
4
|
2
|
|
|
2
|
|
18
|
use warnings FATAL => 'all'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
182
|
|
|
5
|
2
|
|
|
2
|
|
10
|
use autodie ':all'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
14
|
|
|
6
|
2
|
|
|
2
|
|
14110
|
use DDP { output => 'STDOUT', array_max => 10, show_memsize => 1 }; |
|
|
2
|
|
|
|
|
62096
|
|
|
|
2
|
|
|
|
|
22
|
|
|
7
|
2
|
|
|
2
|
|
1448
|
use Devel::Confess 'color'; |
|
|
2
|
|
|
|
|
13494
|
|
|
|
2
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Matplotlib::Simple; |
|
10
|
|
|
|
|
|
|
require 5.010; |
|
11
|
|
|
|
|
|
|
our $VERSION = 0.25; |
|
12
|
2
|
|
|
2
|
|
220
|
use Scalar::Util 'looks_like_number'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
128
|
|
|
13
|
2
|
|
|
2
|
|
14
|
use List::Util qw(max sum min); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
98
|
|
|
14
|
2
|
|
|
2
|
|
1538
|
use Term::ANSIColor; |
|
|
2
|
|
|
|
|
15750
|
|
|
|
2
|
|
|
|
|
150
|
|
|
15
|
2
|
|
|
2
|
|
12
|
use Cwd 'getcwd'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
66
|
|
|
16
|
2
|
|
|
2
|
|
8
|
use File::Temp; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
176
|
|
|
17
|
2
|
|
|
2
|
|
12
|
use DDP { output => 'STDOUT', array_max => 10, show_memsize => 1 }; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
16
|
|
|
18
|
2
|
|
|
2
|
|
186
|
use Devel::Confess 'color'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
10
|
|
|
19
|
2
|
|
|
2
|
|
1646
|
use FindBin '$RealScript'; |
|
|
2
|
|
|
|
|
2012
|
|
|
|
2
|
|
|
|
|
298
|
|
|
20
|
2
|
|
|
2
|
|
24
|
use Exporter 'import'; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
48
|
|
|
21
|
2
|
|
|
2
|
|
984
|
use Capture::Tiny 'capture'; |
|
|
2
|
|
|
|
|
11994
|
|
|
|
2
|
|
|
|
|
96
|
|
|
22
|
2
|
|
|
2
|
|
706
|
use JSON::MaybeXS; |
|
|
2
|
|
|
|
|
18828
|
|
|
|
2
|
|
|
|
|
118
|
|
|
23
|
2
|
|
|
2
|
|
1042
|
use MIME::Base64; |
|
|
2
|
|
|
|
|
1574
|
|
|
|
2
|
|
|
|
|
54666
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT = qw(plt bar barh boxplot colored_table hexbin hist hist2d imshow pie plot scatter violinplot violin wide); |
|
25
|
|
|
|
|
|
|
our @EXPORT_OK = @EXPORT; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my @ax_methods = ( |
|
28
|
|
|
|
|
|
|
'ArtistList', 'add_child_axes', 'add_collection', 'add_container', |
|
29
|
|
|
|
|
|
|
'add_image', 'add_line', 'add_patch', 'add_table', 'apply_aspect', |
|
30
|
|
|
|
|
|
|
'autoscale_view', 'axison', 'bxp', 'callbacks', 'can_pan', 'can_zoom', |
|
31
|
|
|
|
|
|
|
'child_axes', 'collections', 'containers', 'contains_point', 'dataLim', |
|
32
|
|
|
|
|
|
|
'drag_pan', 'end_pan', 'fmt_xdata', 'fmt_ydata', 'format_coord', |
|
33
|
|
|
|
|
|
|
'format_xdata', 'format_ydata','hexbin', 'hist', 'hist2d', 'hlines', |
|
34
|
|
|
|
|
|
|
'ignore_existing_data_limits', 'in_axes', 'indicate_inset', |
|
35
|
|
|
|
|
|
|
'indicate_inset_zoom', 'inset_axes', 'invert_xaxis', 'invert_yaxis', |
|
36
|
|
|
|
|
|
|
'label_outer', 'legend_', 'name', 'pcolorfast', 'redraw_in_frame', 'relim', |
|
37
|
|
|
|
|
|
|
'reset_position', 'scatter', 'secondary_xaxis', 'secondary_yaxis', 'set_adjustable', |
|
38
|
|
|
|
|
|
|
'set_anchor', 'set_aspect', 'set_autoscale_on', 'set_autoscalex_on', 'set_autoscaley_on', |
|
39
|
|
|
|
|
|
|
'set_axes_locator', 'set_axis_off', 'set_axis_on', 'set_axisbelow', |
|
40
|
|
|
|
|
|
|
'set_box_aspect', 'set_fc', 'set_forward_navigation_events', |
|
41
|
|
|
|
|
|
|
'set_frame_on', 'set_mouseover( ', 'set_navigate', 'set_navigate_mode', |
|
42
|
|
|
|
|
|
|
'set_position', 'set_prop_cycle', 'set_rasterization_zorder', |
|
43
|
|
|
|
|
|
|
'set_subplotspec', 'set_title', 'set_xbound', 'set_xlabel', |
|
44
|
|
|
|
|
|
|
'set_xlim', # ax.set_xlim(left, right), or ax.set_xlim(right = 180) |
|
45
|
|
|
|
|
|
|
'set_xmargin', 'set_xscale', 'set_xticklabels', 'set_xticks', 'set_ybound', |
|
46
|
|
|
|
|
|
|
'set_ylabel', 'set_ylim', 'set_ymargin', 'set_yscale', 'set_yticklabels', |
|
47
|
|
|
|
|
|
|
'set_yticks', 'sharex', 'sharey', 'spines', 'start_pan', 'tables', |
|
48
|
|
|
|
|
|
|
'text', 'tick_params', 'ticklabel_format', 'titleOffsetTrans', 'transAxes', 'transData', 'transLimits', |
|
49
|
|
|
|
|
|
|
'transScale', 'update_datalim', 'use_sticky_edges', 'viewLim', 'vlines', 'violin', |
|
50
|
|
|
|
|
|
|
'xaxis', 'xaxis_date', 'xaxis_inverted', 'yaxis', 'yaxis_date', |
|
51
|
|
|
|
|
|
|
'yaxis_inverted' |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
my @fig_methods = ( |
|
54
|
|
|
|
|
|
|
'add_artist', 'add_axes', 'add_axobserver', 'add_callback', 'add_gridspec', |
|
55
|
|
|
|
|
|
|
'add_subfigure', 'add_subplot', 'align_labels', 'align_titles', |
|
56
|
|
|
|
|
|
|
'align_xlabels', 'align_ylabels', 'artists', 'autofmt_xdate', #'axes', # same as plt |
|
57
|
|
|
|
|
|
|
'bbox', 'bbox_inches', 'canvas', 'clear', 'clf', 'clipbox', |
|
58
|
|
|
|
|
|
|
'colorbar', # same name as in plt, have to use on case-by-case |
|
59
|
|
|
|
|
|
|
'contains', 'convert_xunits', 'convert_yunits', 'delaxes', 'dpi', |
|
60
|
|
|
|
|
|
|
'dpi_scale_trans', 'draw', 'draw_artist', 'draw_without_rendering', |
|
61
|
|
|
|
|
|
|
'figbbox', 'figimage', # 'figure', 'findobj', |
|
62
|
|
|
|
|
|
|
'format_cursor_data', 'frameon', 'have_units', 'images', 'is_transform_set', # 'legend', legends', |
|
63
|
|
|
|
|
|
|
'lines', 'mouseover', 'number', 'patch', 'patches', 'pchanged', 'pick', |
|
64
|
|
|
|
|
|
|
'pickable', 'properties', 'remove', |
|
65
|
|
|
|
|
|
|
'remove_callback', #'savefig', keeping plt instead |
|
66
|
|
|
|
|
|
|
'sca', 'set', 'set_agg_filter', 'set_alpha', 'set_animated', 'set_canvas', |
|
67
|
|
|
|
|
|
|
'set_clip_box', 'set_clip_on', 'set_clip_path', 'set_constrained_layout', |
|
68
|
|
|
|
|
|
|
'set_constrained_layout_pads', 'set_dpi', 'set_edgecolor', 'set_facecolor', |
|
69
|
|
|
|
|
|
|
'set_figheight' |
|
70
|
|
|
|
|
|
|
, # default 4.8 # 'set_figure', # deprecated as of matplotlib 3.10.0 |
|
71
|
|
|
|
|
|
|
'set_figwidth', # default 6.4 |
|
72
|
|
|
|
|
|
|
'set_frameon', 'set_gid', 'set_in_layout', 'set_label', |
|
73
|
|
|
|
|
|
|
'set_layout_engine', 'set_linewidth', 'set_mouseover', 'set_path_effects', |
|
74
|
|
|
|
|
|
|
'set_picker', 'set_rasterized', 'set_size_inches', 'set_sketch_params', |
|
75
|
|
|
|
|
|
|
'set_snap', 'set_tight_layout', 'set_transform', 'set_url', 'set_visible', |
|
76
|
|
|
|
|
|
|
'set_zorder', # 'show', # keeping plt instead |
|
77
|
|
|
|
|
|
|
'stale', 'stale_callback', 'sticky_edges', 'subfigs', |
|
78
|
|
|
|
|
|
|
'subfigures', # subplot_mosaic', |
|
79
|
|
|
|
|
|
|
'subplotpars', # subplots','subplots_adjust', |
|
80
|
|
|
|
|
|
|
'suppressComposite', # 'suptitle', # keeping plt instead |
|
81
|
|
|
|
|
|
|
'supxlabel', 'supylabel', #'text', |
|
82
|
|
|
|
|
|
|
'texts', #'tight_layout', |
|
83
|
|
|
|
|
|
|
'transFigure', 'transSubfigure', 'update', |
|
84
|
|
|
|
|
|
|
'update_from', #'waitforbuttonpress', |
|
85
|
|
|
|
|
|
|
'zorder' |
|
86
|
|
|
|
|
|
|
); |
|
87
|
|
|
|
|
|
|
my @plt_methods = ( |
|
88
|
|
|
|
|
|
|
'AbstractContextManager', 'Annotation', 'Arrow', 'Artist', 'AutoLocator', |
|
89
|
|
|
|
|
|
|
'AxLine', 'Axes', 'BackendFilter', 'Button', 'Circle', 'Colorizer', |
|
90
|
|
|
|
|
|
|
'ColorizingArtist', 'Colormap', 'Enum', 'ExitStack', 'Figure', |
|
91
|
|
|
|
|
|
|
'FigureBase', 'FigureCanvasBase', 'FigureManagerBase', ' FixedFormatter', |
|
92
|
|
|
|
|
|
|
'FixedLocator', 'FormatStrFormatter', 'Formatter', 'FuncFormatter', |
|
93
|
|
|
|
|
|
|
'GridSpec', 'IndexLocator', 'Line2D', 'LinearLocator', 'Locator', |
|
94
|
|
|
|
|
|
|
'LogFormatter', 'LogFormatterExponent', 'LogFormatterMathtext', |
|
95
|
|
|
|
|
|
|
'LogLocator', 'MaxNLocator', 'MouseButton', 'MultipleLocator', 'Normalize', |
|
96
|
|
|
|
|
|
|
'NullFormatter', 'NullLocator', 'PolarAxes', 'Polygon', 'Rectangle', |
|
97
|
|
|
|
|
|
|
'ScalarFormatter', 'Slider', 'Subplot', 'SubplotSpec', 'TYPE_CHECKING', |
|
98
|
|
|
|
|
|
|
'Text', 'TickHelper', 'Widget', 'acorr', 'angle_spectrum', 'annotate', |
|
99
|
|
|
|
|
|
|
'annotations', 'arrow', 'autoscale', 'autumn', 'axes', 'axhline', |
|
100
|
|
|
|
|
|
|
'axhspan', 'axis', 'axline', 'axvline', 'axvspan', 'backend_registry', |
|
101
|
|
|
|
|
|
|
'bar_label', 'barbs', 'bone', 'box', 'boxplot', |
|
102
|
|
|
|
|
|
|
'broken_barh', 'cast', 'cbook', 'cla', 'clabel' |
|
103
|
|
|
|
|
|
|
, #'clf', # I don't think you'd ever do that, also redundant with fig |
|
104
|
|
|
|
|
|
|
'clim', 'close', 'cm', 'cohere', 'color_sequences', 'colorbar', |
|
105
|
|
|
|
|
|
|
'colormaps', 'connect', 'contour', 'contourf', 'cool', 'copper', 'csd', |
|
106
|
|
|
|
|
|
|
'cycler', 'delaxes', 'disconnect', 'draw', 'draw_all', |
|
107
|
|
|
|
|
|
|
'draw_if_interactive', 'ecdf', 'errorbar', 'eventplot', 'figaspect', |
|
108
|
|
|
|
|
|
|
'figimage', 'figlegend', 'fignum_exists', 'figtext', 'figure', 'fill', |
|
109
|
|
|
|
|
|
|
'fill_between', 'fill_betweenx', 'findobj', 'flag', 'functools', 'gca', |
|
110
|
|
|
|
|
|
|
'gcf', 'gci', 'get', 'get_backend', 'get_cmap', 'get_current_fig_manager', |
|
111
|
|
|
|
|
|
|
'get_figlabels', 'get_fignums', 'get_plot_commands', 'get_scale_names', |
|
112
|
|
|
|
|
|
|
'getp', 'ginput', 'gray', 'grid', |
|
113
|
|
|
|
|
|
|
'hot', 'hsv', 'importlib', 'imread', 'imsave', 'inferno', |
|
114
|
|
|
|
|
|
|
'inspect', 'install_repl_displayhook', 'interactive', 'ioff', 'ion', |
|
115
|
|
|
|
|
|
|
'isinteractive', 'jet', 'legend', 'locator_params', 'logging', 'loglog', |
|
116
|
|
|
|
|
|
|
'magma', 'magnitude_spectrum', 'margins', 'matplotlib', 'matshow', |
|
117
|
|
|
|
|
|
|
'minorticks_off', 'minorticks_on', 'mlab', 'new_figure_manager', |
|
118
|
|
|
|
|
|
|
'nipy_spectral', 'np', 'overload', 'pause', 'pcolor', 'pcolormesh', |
|
119
|
|
|
|
|
|
|
'phase_spectrum', 'pie', 'pink', 'plasma', 'plot', 'plot_date', 'polar', |
|
120
|
|
|
|
|
|
|
'prism', 'psd', 'quiver', 'quiverkey', 'rc', 'rcParams', 'rcParamsDefault', |
|
121
|
|
|
|
|
|
|
'rcParamsOrig', 'rc_context', 'rcdefaults', 'rcsetup', 'rgrids', 'savefig', |
|
122
|
|
|
|
|
|
|
'sca', #'scatter', # taken by "ax" |
|
123
|
|
|
|
|
|
|
'sci', 'semilogx', 'semilogy', 'set_cmap', 'set_loglevel', 'setp', 'show', |
|
124
|
|
|
|
|
|
|
'specgram', 'spring', 'spy', 'stackplot', 'stairs', 'stem', 'step', |
|
125
|
|
|
|
|
|
|
'streamplot', 'style', |
|
126
|
|
|
|
|
|
|
'subplot', # nrows, ncols : int, default: 1 |
|
127
|
|
|
|
|
|
|
'subplot2grid', 'subplot_mosaic', 'subplot_tool', 'subplots', |
|
128
|
|
|
|
|
|
|
'subplots_adjust', 'summer', 'suptitle', 'switch_backend', 'sys', 'table', |
|
129
|
|
|
|
|
|
|
# 'text' |
|
130
|
|
|
|
|
|
|
, # text(x: 'float', y: 'float', s: 'str', fontdict: 'dict[str, Any] | None' = None, **kwargs) -> 'Text' |
|
131
|
|
|
|
|
|
|
'thetagrids', 'threading',# 'tick_params', |
|
132
|
|
|
|
|
|
|
'tight_layout', 'time', 'title', 'tricontour', 'tricontourf', 'tripcolor', |
|
133
|
|
|
|
|
|
|
'triplot', |
|
134
|
|
|
|
|
|
|
#'twinx', 'twiny', |
|
135
|
|
|
|
|
|
|
'uninstall_repl_displayhook', 'violinplot', |
|
136
|
|
|
|
|
|
|
'viridis', 'waitforbuttonpress', 'winter', 'xcorr', 'xkcd',# 'vlines' |
|
137
|
|
|
|
|
|
|
'xlabel','xscale','ylabel', 'ylim', 'yscale', |
|
138
|
|
|
|
|
|
|
# 'xlim','xticks','yticks' |
|
139
|
|
|
|
|
|
|
); |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my @arg = ('add', 'cmap', 'data', 'execute', 'fh','ncols', 'plot.type', 'plots', 'plot', 'output.file', 'nrows', 'scale', 'scalex', 'scaley', 'shared.colorbar', 'show', 'twinx'); |
|
142
|
|
|
|
|
|
|
my @cb_arg = ( |
|
143
|
|
|
|
|
|
|
'cbdrawedges', # for colarbar: Whether to draw lines at color boundaries |
|
144
|
|
|
|
|
|
|
'cblabel', # The label on the colorbar's long axis |
|
145
|
|
|
|
|
|
|
'cblocation', # of the colorbar None or {'left', 'right', 'top', 'bottom'} |
|
146
|
|
|
|
|
|
|
'cborientation', # None or {'vertical', 'horizontal'} |
|
147
|
|
|
|
|
|
|
'cbpad', # pad : float, default: 0.05 if vertical, 0.15 if horizontal; Fraction of original Axes between colorbar and new image Axes |
|
148
|
|
|
|
|
|
|
'cb_logscale'); |
|
149
|
|
|
|
|
|
|
my %opt = ( |
|
150
|
|
|
|
|
|
|
barplot_helper => [ |
|
151
|
|
|
|
|
|
|
'color' |
|
152
|
|
|
|
|
|
|
, # :mpltype:`color` or list of :mpltype:`color`, optional; The colors of the bar faces. This is an alias for *facecolor*. If both are given, *facecolor* takes precedence # if entering multiple colors, quoting isn't needed |
|
153
|
|
|
|
|
|
|
'edgecolor' , # optional; The colors of the bar edges. |
|
154
|
|
|
|
|
|
|
'key.order', # define the keys in an order (an array reference) |
|
155
|
|
|
|
|
|
|
'label', # an array of labels for grouped bar plots |
|
156
|
|
|
|
|
|
|
'linewidth', # float or array, optional; Width of the bar edge(s). If 0, don't draw edges |
|
157
|
|
|
|
|
|
|
'log' , # bool, default: False; If *True*, set the y-axis to be log scale. |
|
158
|
|
|
|
|
|
|
'logscale', # equivalent to "log" |
|
159
|
|
|
|
|
|
|
'stacked', # stack the groups on top of one another; default 0 = off |
|
160
|
|
|
|
|
|
|
'width', # float or array, default: 0.8; The width(s) of the bars. |
|
161
|
|
|
|
|
|
|
'xerr', # float or array-like of shape(N,) or shape(2, N), optional. If not *None*, add horizontal / vertical errorbars to the bar tips. The values are +/- sizes relative to the data: - scalar: symmetric +/- values for all bars # - shape(N,): symmetric +/- values for each bar # - shape(2, N): Separate - and + values for each bar. First row # contains the lower errors, the second row contains the upper # errors. # - *None*: No errorbar. (Default) |
|
162
|
|
|
|
|
|
|
'yerr', # same as xerr, but better with bar |
|
163
|
|
|
|
|
|
|
], |
|
164
|
|
|
|
|
|
|
boxplot_helper => [ |
|
165
|
|
|
|
|
|
|
'color', # a hash, where keys are the keys in data, and values are colors, e.g. X => 'blue' |
|
166
|
|
|
|
|
|
|
'colors', 'key.order', |
|
167
|
|
|
|
|
|
|
'logscale', # array of "x" and/or "y" |
|
168
|
|
|
|
|
|
|
'notch', # Whether to draw a notched boxplot (`True`), or a rectangular boxplot (`False`) |
|
169
|
|
|
|
|
|
|
'orientation',# {'vertical', 'horizontal'}, default: 'vertical' |
|
170
|
|
|
|
|
|
|
'showcaps', # bool: Show the caps on the ends of whiskers; default "True" |
|
171
|
|
|
|
|
|
|
'showfliers', #bool, default: :rc:`boxplot.showfliers`; Show the outliers beyond the caps. |
|
172
|
|
|
|
|
|
|
'showmeans' #bool, default: :rc:`boxplot.showmeans` |
|
173
|
|
|
|
|
|
|
], |
|
174
|
|
|
|
|
|
|
colored_table_helper => [@cb_arg, |
|
175
|
|
|
|
|
|
|
'col.labels', |
|
176
|
|
|
|
|
|
|
'cmap', # the cmap used for coloring |
|
177
|
|
|
|
|
|
|
'colorbar.on', # only draw if colorbar is on |
|
178
|
|
|
|
|
|
|
'default_undefined', # what value should undefined values be assigned to? |
|
179
|
|
|
|
|
|
|
'mirror', # $data{A}{B} = $data{B}{A} |
|
180
|
|
|
|
|
|
|
'row.labels', # row labels |
|
181
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
182
|
|
|
|
|
|
|
'show.numbers',# show the numbers or not, by default off. 0 = "off"; "show.numbers" > 0 => "on" |
|
183
|
|
|
|
|
|
|
'undef.color', # what color will undefined points be |
|
184
|
|
|
|
|
|
|
], |
|
185
|
|
|
|
|
|
|
hexbin_helper => [ @cb_arg, |
|
186
|
|
|
|
|
|
|
'cb_logscale', |
|
187
|
|
|
|
|
|
|
'cmap', # "gist_rainbow" by default |
|
188
|
|
|
|
|
|
|
'colorbar.on', # only draw colorbar if colorbar is on |
|
189
|
|
|
|
|
|
|
'key.order', # define the keys in an order (an array reference) |
|
190
|
|
|
|
|
|
|
'marginals', # If marginals is *True*, plot the marginal density as colormapped rectangles along the bottom of the x-axis and left of the y-axis. |
|
191
|
|
|
|
|
|
|
'mincnt' |
|
192
|
|
|
|
|
|
|
, # int >= 0, default: 0 If > 0, only display cells with at least *mincnt* number of points in the cell. |
|
193
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
194
|
|
|
|
|
|
|
'vmax' |
|
195
|
|
|
|
|
|
|
, # When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover |
|
196
|
|
|
|
|
|
|
'vmin' |
|
197
|
|
|
|
|
|
|
, # When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover |
|
198
|
|
|
|
|
|
|
'xbins', # default 15 |
|
199
|
|
|
|
|
|
|
'xscale.hexbin', # 'linear', 'log'}, default: 'linear': Use a linear or log10 scale on the horizontal axis. |
|
200
|
|
|
|
|
|
|
'ybins', # default 15 |
|
201
|
|
|
|
|
|
|
'yscale.hexbin', # 'linear', 'log'}, default: 'linear': Use a linear or log10 scale on the vertical axis. |
|
202
|
|
|
|
|
|
|
], |
|
203
|
|
|
|
|
|
|
hist_helper => [ |
|
204
|
|
|
|
|
|
|
'alpha', # default 0.5; same for all sets |
|
205
|
|
|
|
|
|
|
'bins' |
|
206
|
|
|
|
|
|
|
, # nt or sequence or str, default: :rc:`hist.bins`If *bins* is an integer, it defines the number of equal-width bins in the range. If *bins* is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, bins may be unequally spaced. All but the last (righthand-most) bin is half-open |
|
207
|
|
|
|
|
|
|
'color', # a hash, where keys are the keys in data, and values are colors, e.g. X => 'blue' |
|
208
|
|
|
|
|
|
|
'colorbar.on', # only draw colorbar if colorbar is on |
|
209
|
|
|
|
|
|
|
'logscale', # if set to > 1, the y-axis will be logarithmic |
|
210
|
|
|
|
|
|
|
'orientation', # {'vertical', 'horizontal'}, default: 'vertical' |
|
211
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
212
|
|
|
|
|
|
|
], |
|
213
|
|
|
|
|
|
|
hist2d_helper => [@cb_arg, |
|
214
|
|
|
|
|
|
|
'cb_logscale', |
|
215
|
|
|
|
|
|
|
'cmap', # "gist_rainbow" by default |
|
216
|
|
|
|
|
|
|
'cmax', # All bins that has count < *cmin* or > *cmax* will not be displayed |
|
217
|
|
|
|
|
|
|
'cmin', # color min |
|
218
|
|
|
|
|
|
|
'colorbar.on', # only draw colorbar if colorbar is on |
|
219
|
|
|
|
|
|
|
'density', # density : bool, default: False |
|
220
|
|
|
|
|
|
|
'key.order', # define the keys in an order (an array reference) |
|
221
|
|
|
|
|
|
|
'logscale', # logscale, an array of axes that will get log scale |
|
222
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
223
|
|
|
|
|
|
|
'show.colorbar', |
|
224
|
|
|
|
|
|
|
'vmax' |
|
225
|
|
|
|
|
|
|
, # When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover |
|
226
|
|
|
|
|
|
|
'vmin' |
|
227
|
|
|
|
|
|
|
, # When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover |
|
228
|
|
|
|
|
|
|
'xbins', # default 15 |
|
229
|
|
|
|
|
|
|
'xmin', 'xmax', 'ymin', 'ymax', |
|
230
|
|
|
|
|
|
|
'ybins', # default 15 |
|
231
|
|
|
|
|
|
|
], |
|
232
|
|
|
|
|
|
|
imshow_helper => [@cb_arg, |
|
233
|
|
|
|
|
|
|
'cblabel', # colorbar label |
|
234
|
|
|
|
|
|
|
'cbdrawedges', # for colorbar |
|
235
|
|
|
|
|
|
|
'cblocation', # of the colorbar None or {'left', 'right', 'top', 'bottom'} |
|
236
|
|
|
|
|
|
|
'cborientation', # None or {'vertical', 'horizontal'} |
|
237
|
|
|
|
|
|
|
'cmap', # The Colormap instance or registered colormap name used to map scalar data to colors |
|
238
|
|
|
|
|
|
|
'colorbar.on', # only draw if colorbar is on |
|
239
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
240
|
|
|
|
|
|
|
'stringmap', # 'H' => 'Alpha helix' |
|
241
|
|
|
|
|
|
|
'vmax', # float |
|
242
|
|
|
|
|
|
|
'vmin', # flat |
|
243
|
|
|
|
|
|
|
], |
|
244
|
|
|
|
|
|
|
pie_helper => [ |
|
245
|
|
|
|
|
|
|
'autopct', # percent wise |
|
246
|
|
|
|
|
|
|
#labeldistance and pctdistance are ratios of the radius; therefore they vary between 0 for the center of the pie and 1 for the edge of the pie, and can be set to greater than 1 to place text outside the pie https://matplotlib.org/stable/gallery/pie_and_polar_charts/pie_features.html |
|
247
|
|
|
|
|
|
|
'labeldistance', |
|
248
|
|
|
|
|
|
|
'pctdistance', |
|
249
|
|
|
|
|
|
|
], |
|
250
|
|
|
|
|
|
|
plot_helper => [ |
|
251
|
|
|
|
|
|
|
'key.order', # an array of key strings (which are defined in data) |
|
252
|
|
|
|
|
|
|
'logscale', # an array of "x" and/or "y" |
|
253
|
|
|
|
|
|
|
'show.legend', # be default on; should be 0 if off |
|
254
|
|
|
|
|
|
|
'set.options', |
|
255
|
|
|
|
|
|
|
'twinx.args' |
|
256
|
|
|
|
|
|
|
], |
|
257
|
|
|
|
|
|
|
scatter_helper => [ |
|
258
|
|
|
|
|
|
|
'color_key', # which of data keys is the color key |
|
259
|
|
|
|
|
|
|
'cmap', # for 3-set scatterplots; default "gist_rainbow" |
|
260
|
|
|
|
|
|
|
'colorbar.on', # only draw colorbar if colorbar is on |
|
261
|
|
|
|
|
|
|
'keys', # specify the order, otherwise alphabetical #'log', # if set to > 1, the y-axis will be logarithmic # 's', # float or array-like, shape (n, ), optional. The marker size in points**2 (typographic points are 1/72 in.). |
|
262
|
|
|
|
|
|
|
'logscale', # "x" and/or "y" as an aray |
|
263
|
|
|
|
|
|
|
'shared.colorbar', # array of 0-based indices for sharing a colorbar |
|
264
|
|
|
|
|
|
|
'set.options' # color = 'red', marker = 'v', etc. |
|
265
|
|
|
|
|
|
|
], |
|
266
|
|
|
|
|
|
|
violin_helper => [ |
|
267
|
|
|
|
|
|
|
'color', # a hash, where keys are the keys in data, and values are colors, e.g. X => 'blue' |
|
268
|
|
|
|
|
|
|
'colorbar.on',# only draw colorbar if colorbar is on |
|
269
|
|
|
|
|
|
|
'colors', |
|
270
|
|
|
|
|
|
|
'key.order', |
|
271
|
|
|
|
|
|
|
'logscale', # array: "x" and/or "y" |
|
272
|
|
|
|
|
|
|
'orientation',# {'vertical', 'horizontal'}, default: 'vertical' |
|
273
|
|
|
|
|
|
|
'whiskers' |
|
274
|
|
|
|
|
|
|
], |
|
275
|
|
|
|
|
|
|
wide_helper => [ |
|
276
|
|
|
|
|
|
|
'color', # a hash, with each key assigned to a color "blue" or something |
|
277
|
|
|
|
|
|
|
'show.legend', # be default on; should be 0 if off |
|
278
|
|
|
|
|
|
|
], |
|
279
|
|
|
|
|
|
|
); |
|
280
|
|
|
|
|
|
|
sub write_data { |
|
281
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
282
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1]; |
|
283
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
284
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => ... })\""; |
|
285
|
|
|
|
|
|
|
} |
|
286
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
287
|
|
|
|
|
|
|
'data', # args to original function (scalar, hashref, or arrayref) |
|
288
|
|
|
|
|
|
|
'fh', # file handle |
|
289
|
|
|
|
|
|
|
'name' # python variable name |
|
290
|
|
|
|
|
|
|
); |
|
291
|
0
|
|
|
|
|
|
my @undef_args = grep { not defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
292
|
0
|
0
|
|
|
|
|
if (scalar @undef_args > 0) { |
|
293
|
0
|
|
|
|
|
|
p @undef_args; |
|
294
|
0
|
|
|
|
|
|
die "the above args are required for $current_sub, but weren't defined"; |
|
295
|
|
|
|
|
|
|
} |
|
296
|
|
|
|
|
|
|
# 1. Create the JSON Encoder; allow_nonref: allows scalars (strings/numbers) to be encoded |
|
297
|
0
|
|
|
|
|
|
my $json_encoder = JSON::MaybeXS->new->utf8->allow_nonref; |
|
298
|
|
|
|
|
|
|
# 2. Serialize Perl Data -> JSON String; Passing data directly. JSON::MaybeXS handles refs + scalars automatically. |
|
299
|
0
|
|
|
|
|
|
my $json_string = $json_encoder->encode($args->{data}); |
|
300
|
|
|
|
|
|
|
# 3. Base64 Encode the JSON String, not the reference |
|
301
|
0
|
|
|
|
|
|
my $b64_data = encode_base64($json_string, ''); |
|
302
|
|
|
|
|
|
|
# Assign the b64 string to a temp python variable |
|
303
|
0
|
|
|
|
|
|
say {$args->{fh}} "$args->{name}_b64 = '$b64_data'"; |
|
|
0
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
# Decode b64 -> bytes -> utf8 string -> json load -> python object |
|
305
|
0
|
|
|
|
|
|
say {$args->{fh}} "$args->{name} = json.loads(base64.b64decode($args->{name}_b64).decode('utf-8'))"; |
|
|
0
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
} |
|
307
|
|
|
|
|
|
|
sub plot_args { # this is a helper function to other matplotlib subroutines |
|
308
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
309
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1]; |
|
310
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
311
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
312
|
|
|
|
|
|
|
} |
|
313
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
314
|
|
|
|
|
|
|
'ax', # ax1, ax2, etc. when there are multiple plots |
|
315
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
316
|
|
|
|
|
|
|
'args', # args to original function |
|
317
|
|
|
|
|
|
|
); |
|
318
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
319
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
320
|
0
|
|
|
|
|
|
p @undef_args; |
|
321
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
322
|
|
|
|
|
|
|
} |
|
323
|
0
|
|
|
|
|
|
my @defined_args = ( @reqd_args, @ax_methods, @fig_methods, @plt_methods, @arg, @cb_arg ); |
|
324
|
|
|
|
|
|
|
my @bad_args = grep { |
|
325
|
0
|
|
|
|
|
|
my $key = $_; |
|
326
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @defined_args |
|
|
0
|
|
|
|
|
|
|
|
327
|
0
|
|
|
|
|
|
} keys %{$args}; |
|
|
0
|
|
|
|
|
|
|
|
328
|
0
|
0
|
|
|
|
|
if ( scalar @bad_args > 0 ) { |
|
329
|
0
|
|
|
|
|
|
p @bad_args, array_max => scalar @bad_args; |
|
330
|
0
|
|
|
|
|
|
say 'the above arguments are not recognized.'; |
|
331
|
0
|
|
|
|
|
|
p @defined_args, array_max => scalar @defined_args; |
|
332
|
0
|
|
|
|
|
|
die 'The above args are accepted.'; |
|
333
|
|
|
|
|
|
|
} |
|
334
|
0
|
|
0
|
|
|
|
$args->{ax} = $args->{ax} // 'ax'; |
|
335
|
0
|
|
|
|
|
|
foreach my $item ( |
|
336
|
0
|
|
|
|
|
|
grep { defined $args->{args}{$_} } ( # no quotes! |
|
337
|
|
|
|
|
|
|
'set_title', 'set_xlabel', 'set_ylabel', 'suptitle', |
|
338
|
|
|
|
|
|
|
'xlabel', 'ylabel', 'title' |
|
339
|
|
|
|
|
|
|
) |
|
340
|
|
|
|
|
|
|
) |
|
341
|
|
|
|
|
|
|
{ |
|
342
|
0
|
0
|
|
|
|
|
if ( $args->{args}{$item} =~ m/^([^\"\',]+)$/ ) { |
|
343
|
0
|
|
|
|
|
|
$args->{args}{$item} = "'$args->{args}{$item}'"; |
|
344
|
|
|
|
|
|
|
} |
|
345
|
|
|
|
|
|
|
} |
|
346
|
0
|
|
|
|
|
|
my @obj = ( $args->{ax}, 'fig', 'plt' ); |
|
347
|
0
|
|
|
|
|
|
my @args = ( \@ax_methods, \@fig_methods, \@plt_methods ); |
|
348
|
0
|
|
|
|
|
|
foreach my $i ( 0 .. $#args ) { |
|
349
|
0
|
|
|
|
|
|
foreach my $method ( grep { defined $args->{args}{$_} } @{ $args[$i] } ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
350
|
0
|
|
|
|
|
|
my $ref = ref $args->{args}{$method}; |
|
351
|
0
|
0
|
0
|
|
|
|
if ( ( $ref ne 'ARRAY' ) && ( $ref ne '' ) ) { |
|
352
|
0
|
|
|
|
|
|
die "$current_sub only accepts scalar or array types, but \"$ref\" was entered."; |
|
353
|
|
|
|
|
|
|
} |
|
354
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { |
|
355
|
0
|
0
|
|
|
|
|
if ($method eq 'show') { |
|
356
|
0
|
|
|
|
|
|
say {$args->{fh}} "$obj[$i].$method()" . '#line' . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
} else { |
|
358
|
0
|
|
|
|
|
|
say {$args->{fh}} "$obj[$i].$method($args->{args}{$method}) #line" . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
} |
|
360
|
0
|
|
|
|
|
|
next; |
|
361
|
|
|
|
|
|
|
} |
|
362
|
|
|
|
|
|
|
# can only be ARRAY |
|
363
|
0
|
|
|
|
|
|
foreach my $j ( @{ $args->{args}{$method} } ) { |
|
|
0
|
|
|
|
|
|
|
|
364
|
0
|
|
|
|
|
|
say { $args->{fh} } "$obj[$i].$method($j) #line" . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
} |
|
366
|
|
|
|
|
|
|
} |
|
367
|
|
|
|
|
|
|
} |
|
368
|
0
|
0
|
|
|
|
|
return unless defined $args->{ax}; |
|
369
|
0
|
|
0
|
|
|
|
my $legend = $args->{args}{legend} // ''; |
|
370
|
0
|
|
|
|
|
|
my $pie_plot = 0; |
|
371
|
0
|
0
|
0
|
|
|
|
if ( ( defined $args->{args}{'plot.type'} ) |
|
372
|
|
|
|
|
|
|
&& ( $args->{args}{'plot.type'} eq 'pie' ) ) { |
|
373
|
0
|
|
|
|
|
|
$pie_plot = 1; |
|
374
|
|
|
|
|
|
|
} |
|
375
|
0
|
0
|
|
|
|
|
return 1 if $pie_plot == 1; |
|
376
|
|
|
|
|
|
|
# pie charts don't get legends |
|
377
|
0
|
|
|
|
|
|
say { $args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
"handles, labels = $args->{ax}.get_legend_handles_labels()"; |
|
379
|
0
|
|
|
|
|
|
say { $args->{fh} } 'if len(labels) > 0:'; |
|
|
0
|
|
|
|
|
|
|
|
380
|
0
|
|
|
|
|
|
say { $args->{fh} } "\t$args->{ax}.legend($legend)"; |
|
|
0
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
} |
|
382
|
|
|
|
|
|
|
sub barplot_helper { # this is a helper function to other matplotlib subroutines |
|
383
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
384
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
385
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
386
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
387
|
0
|
|
|
|
|
|
die |
|
388
|
|
|
|
|
|
|
"args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
389
|
|
|
|
|
|
|
} |
|
390
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
391
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
392
|
|
|
|
|
|
|
'plot', # args to original function |
|
393
|
|
|
|
|
|
|
); |
|
394
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
395
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
396
|
0
|
|
|
|
|
|
p @undef_args; |
|
397
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
398
|
|
|
|
|
|
|
} |
|
399
|
0
|
|
|
|
|
|
my @opt = ('ax', @reqd_args, @ax_methods, @plt_methods, @fig_methods, @arg, @{ $opt{$current_sub} } ); |
|
|
0
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
401
|
|
|
|
|
|
|
my @bad_opt = grep { |
|
402
|
0
|
|
|
|
|
|
my $key = $_; |
|
403
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
404
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
405
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
406
|
0
|
0
|
|
|
|
|
if ( scalar @bad_opt > 0 ) { |
|
407
|
0
|
|
|
|
|
|
p @bad_opt; |
|
408
|
0
|
|
|
|
|
|
die |
|
409
|
|
|
|
|
|
|
"The above arguments aren't defined for $plot->{'plot.type'} at plot position $ax"; |
|
410
|
|
|
|
|
|
|
} |
|
411
|
0
|
|
|
|
|
|
my ( %ref_counts, $plot_type ); |
|
412
|
0
|
|
|
|
|
|
foreach my $set ( keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
413
|
0
|
|
|
|
|
|
$ref_counts{ ref $plot->{data}{$set} }++; |
|
414
|
|
|
|
|
|
|
} |
|
415
|
0
|
0
|
|
|
|
|
if ( scalar keys %ref_counts > 1 ) { |
|
416
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
417
|
0
|
|
|
|
|
|
p %ref_counts; |
|
418
|
0
|
|
|
|
|
|
die |
|
419
|
|
|
|
|
|
|
"different kinds of data were entered to plot $ax which should be simple hash or hash of arrays."; |
|
420
|
|
|
|
|
|
|
} |
|
421
|
0
|
0
|
|
|
|
|
if ( defined $ref_counts{''} ) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
422
|
0
|
|
|
|
|
|
$plot_type = 'simple'; |
|
423
|
|
|
|
|
|
|
} elsif ( defined $ref_counts{'ARRAY'} ) { |
|
424
|
0
|
|
|
|
|
|
$plot_type = 'grouped'; |
|
425
|
|
|
|
|
|
|
} elsif ( defined $ref_counts{'HASH'} ) { |
|
426
|
0
|
|
|
|
|
|
$plot_type = 'grouped'; # now make the hash of hash into a ARRAY structure |
|
427
|
0
|
|
|
|
|
|
my %key2; |
|
428
|
0
|
|
|
|
|
|
foreach my $key1 ( keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
429
|
0
|
|
|
|
|
|
foreach my $key2 ( keys %{ $plot->{data}{$key1} } ) { |
|
|
0
|
|
|
|
|
|
|
|
430
|
0
|
|
|
|
|
|
$key2{$key2}++; |
|
431
|
|
|
|
|
|
|
} |
|
432
|
|
|
|
|
|
|
} |
|
433
|
0
|
|
|
|
|
|
my @key2 = sort { lc $a cmp lc $b } keys %key2; |
|
|
0
|
|
|
|
|
|
|
|
434
|
0
|
|
|
|
|
|
my %new_structure; |
|
435
|
0
|
|
|
|
|
|
foreach my $k1 ( keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
436
|
0
|
|
|
|
|
|
@{ $new_structure{$k1} } = @{ $plot->{data}{$k1} }{@key2}; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
} |
|
438
|
0
|
|
|
|
|
|
@{ $plot->{label} } = @key2; |
|
|
0
|
|
|
|
|
|
|
|
439
|
0
|
|
|
|
|
|
$plot->{data} = \%new_structure; |
|
440
|
|
|
|
|
|
|
} else { |
|
441
|
0
|
|
|
|
|
|
p %ref_counts; |
|
442
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
443
|
0
|
|
|
|
|
|
die 'the above plot type is not yet programmed in to bar/barh'; |
|
444
|
|
|
|
|
|
|
} |
|
445
|
0
|
|
0
|
|
|
|
$plot->{stacked} = $plot->{stacked} // 0; |
|
446
|
0
|
0
|
0
|
|
|
|
if ( ( $plot_type eq 'grouped' ) |
|
|
|
|
0
|
|
|
|
|
|
447
|
|
|
|
|
|
|
&& ( defined $plot->{width} ) |
|
448
|
|
|
|
|
|
|
&& ( $plot->{stacked} == 0 ) ) |
|
449
|
|
|
|
|
|
|
{ |
|
450
|
0
|
|
|
|
|
|
say STDERR 'grouped, non-stacked barplots ignore width settings'; |
|
451
|
0
|
|
|
|
|
|
delete $plot->{width}; |
|
452
|
|
|
|
|
|
|
} |
|
453
|
0
|
|
|
|
|
|
my @key_order; |
|
454
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
455
|
0
|
|
|
|
|
|
@key_order = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
} else { |
|
457
|
0
|
|
|
|
|
|
@key_order = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
} |
|
459
|
0
|
|
|
|
|
|
my $options = ''; # these args go to the plt.bar call |
|
460
|
0
|
0
|
0
|
|
|
|
if ( $plot->{'log'} || $plot->{logscale}) { |
|
461
|
0
|
|
|
|
|
|
$options .= ', log = True'; |
|
462
|
|
|
|
|
|
|
} # args that can be either arrays or strings below; STRINGS: |
|
463
|
0
|
|
|
|
|
|
foreach my $c ( grep { defined $plot->{$_} } ( 'color', 'edgecolor' ) ) { |
|
|
0
|
|
|
|
|
|
|
|
464
|
0
|
0
|
0
|
|
|
|
next if ( ( $c eq 'color' ) && ( $plot_type eq 'grouped' ) ); |
|
465
|
0
|
|
|
|
|
|
my $ref = ref $plot->{$c}; |
|
466
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { # single color |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
467
|
0
|
|
|
|
|
|
$options .= ", $c = '$plot->{$c}'"; |
|
468
|
|
|
|
|
|
|
} elsif ( $ref eq 'ARRAY' ) { |
|
469
|
0
|
|
|
|
|
|
$options .= ", $c = [\"" . join( '","', @{ $plot->{$c} } ) . '"]'; |
|
|
0
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
} elsif ( $ref eq 'HASH') { |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
} else { |
|
473
|
0
|
|
|
|
|
|
die "ref \"$ref\" isn't defined"; |
|
474
|
|
|
|
|
|
|
} |
|
475
|
|
|
|
|
|
|
} # args that can be either arrays or strings below; NUMERIC: |
|
476
|
0
|
|
|
|
|
|
foreach my $c ( grep { defined $plot->{$_} } ('linewidth') ) { |
|
|
0
|
|
|
|
|
|
|
|
477
|
0
|
|
|
|
|
|
my $ref = ref $plot->{$c}; |
|
478
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { # single color |
|
|
|
0
|
|
|
|
|
|
|
479
|
0
|
|
|
|
|
|
$options .= ", $c = $plot->{$c}"; |
|
480
|
|
|
|
|
|
|
} elsif ( $ref eq 'ARRAY' ) { |
|
481
|
0
|
|
|
|
|
|
$options .= ", $c = [" . join( ',', @{ $plot->{$c} } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
} else { |
|
483
|
0
|
|
|
|
|
|
p $args; |
|
484
|
0
|
|
|
|
|
|
die "$ref for $c isn't acceptable"; |
|
485
|
|
|
|
|
|
|
} |
|
486
|
|
|
|
|
|
|
} |
|
487
|
0
|
|
|
|
|
|
foreach my $err ( grep { defined $plot->{$_} } ( 'xerr', 'yerr' ) ) { |
|
|
0
|
|
|
|
|
|
|
|
488
|
0
|
|
|
|
|
|
my $ref = ref $plot->{$err}; |
|
489
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { |
|
|
|
0
|
|
|
|
|
|
|
490
|
0
|
|
|
|
|
|
$options .= ", $err = $plot->{$err}"; |
|
491
|
|
|
|
|
|
|
} elsif ( $ref eq 'HASH' ) { # I assume that it's all defined |
|
492
|
0
|
|
|
|
|
|
my ( @low, @high ); |
|
493
|
0
|
|
|
|
|
|
foreach my $i (@key_order) { |
|
494
|
0
|
0
|
|
|
|
|
if ( scalar @{ $plot->{$err}{$i} } != 2 ) { |
|
|
0
|
|
|
|
|
|
|
|
495
|
0
|
|
|
|
|
|
p $plot->{$err}{$i}; |
|
496
|
0
|
|
|
|
|
|
die "$err/$i should have exactly 2 items: low and high error bars"; |
|
497
|
|
|
|
|
|
|
} |
|
498
|
0
|
|
|
|
|
|
push @low, $plot->{$err}{$i}[0]; |
|
499
|
0
|
|
|
|
|
|
push @high, $plot->{$err}{$i}[1]; |
|
500
|
|
|
|
|
|
|
} |
|
501
|
|
|
|
|
|
|
$options .= |
|
502
|
0
|
|
|
|
|
|
", $err = [[" |
|
503
|
|
|
|
|
|
|
. join( ',', @low ) . '],[' |
|
504
|
|
|
|
|
|
|
. join( ',', @high ) . ']]'; |
|
505
|
|
|
|
|
|
|
} else { |
|
506
|
0
|
|
|
|
|
|
p $args; |
|
507
|
0
|
|
|
|
|
|
die "$ref for $err isn't acceptable"; |
|
508
|
|
|
|
|
|
|
} |
|
509
|
|
|
|
|
|
|
} |
|
510
|
0
|
0
|
|
|
|
|
if ( $plot_type eq 'simple' ) { # a simple hash -> simple bar plot |
|
|
|
0
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
write_data({ |
|
512
|
|
|
|
|
|
|
data => \@key_order, |
|
513
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
514
|
0
|
|
|
|
|
|
name => 'labels' |
|
515
|
|
|
|
|
|
|
}); |
|
516
|
0
|
|
|
|
|
|
say { $args->{fh} } 'vals = [' . join( ',', @{ $plot->{data} }{@key_order} ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
517
|
0
|
0
|
0
|
|
|
|
if ((defined $plot->{color}) && (ref $plot->{color} eq 'HASH')) { |
|
518
|
0
|
|
|
|
|
|
@undef_args = grep {not defined $plot->{color}{$_}} @key_order; |
|
|
0
|
|
|
|
|
|
|
|
519
|
0
|
0
|
|
|
|
|
if (scalar @undef_args > 0) { |
|
520
|
0
|
|
|
|
|
|
p @undef_args; |
|
521
|
0
|
|
|
|
|
|
die 'the above keys were not defined in the colors hash'; |
|
522
|
|
|
|
|
|
|
} |
|
523
|
0
|
|
|
|
|
|
$options .= ',color = ["' . join ('","', @{ $plot->{color} }{@key_order} ) . '"]'; |
|
|
0
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
} |
|
525
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.$plot->{'plot.type'}(labels, vals $options)"; |
|
|
0
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
} elsif ( $plot_type eq 'grouped' ) { # grouped bar plot; hash of array |
|
527
|
0
|
|
|
|
|
|
my @val; |
|
528
|
0
|
|
|
|
|
|
foreach my $k (@key_order) { |
|
529
|
0
|
|
|
|
|
|
foreach my $i ( 0 .. scalar @{ $plot->{data}{$k} } - 1 ) { |
|
|
0
|
|
|
|
|
|
|
|
530
|
0
|
|
|
|
|
|
push @{ $val[$i] }, $plot->{data}{$k}[$i]; |
|
|
0
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
} |
|
532
|
|
|
|
|
|
|
} |
|
533
|
0
|
|
0
|
|
|
|
my $barwidth = $plot->{width} // 0.8; |
|
534
|
0
|
|
0
|
|
|
|
$plot->{stacked} = $plot->{stacked} // 0; |
|
535
|
0
|
0
|
|
|
|
|
if ( $plot->{stacked} == 0 ) { |
|
536
|
0
|
|
|
|
|
|
$barwidth /= ( (scalar keys %ref_counts) + 3 ); |
|
537
|
|
|
|
|
|
|
} |
|
538
|
0
|
|
|
|
|
|
my @xticks = 0 .. scalar @{ $val[0] } - 1; |
|
|
0
|
|
|
|
|
|
|
|
539
|
0
|
|
|
|
|
|
my @mean_pos = map { 0 } 0 .. scalar @{ $val[0] } - 1; # initialize |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
540
|
0
|
|
|
|
|
|
my $hw = 'height'; |
|
541
|
0
|
0
|
|
|
|
|
$hw = 'width' if $plot->{'plot.type'} eq 'bar'; |
|
542
|
0
|
|
|
|
|
|
my @bottom = map { 0 } 0 .. scalar @{ $val[0] } - 1; # initialize |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
543
|
0
|
|
|
|
|
|
my $i = 0; |
|
544
|
0
|
|
|
|
|
|
foreach my $arr (@val) { |
|
545
|
0
|
|
|
|
|
|
my $x = '[' . join( ',', @xticks ) . ']'; |
|
546
|
0
|
|
|
|
|
|
foreach my $p ( 0 .. $#mean_pos ) { |
|
547
|
0
|
|
|
|
|
|
$mean_pos[$p] += $xticks[$p]; |
|
548
|
|
|
|
|
|
|
} |
|
549
|
0
|
|
|
|
|
|
my $set_options = ''; |
|
550
|
0
|
|
|
|
|
|
foreach |
|
551
|
0
|
|
|
|
|
|
my $f ( grep { defined $plot->{$_}[$i] } ( 'color', 'label' ) ) |
|
552
|
|
|
|
|
|
|
{ |
|
553
|
0
|
|
|
|
|
|
$set_options .= ", $f = '$plot->{$f}[$i]'"; |
|
554
|
|
|
|
|
|
|
} |
|
555
|
0
|
0
|
|
|
|
|
if ( $plot->{stacked} > 0 ) { |
|
556
|
0
|
|
|
|
|
|
$set_options .= ', bottom = [' . join( ',', @bottom ) . ']'; |
|
557
|
|
|
|
|
|
|
} |
|
558
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.$plot->{'plot.type'}($x, [" |
|
559
|
0
|
|
|
|
|
|
. join( ',', @{$arr} ) |
|
|
0
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
. "], $hw = $barwidth $options $set_options)"; |
|
561
|
|
|
|
|
|
|
@bottom = |
|
562
|
0
|
|
|
|
|
|
map { $bottom[$_] + $arr->[$_] } 0 .. scalar @{ $val[0] } - 1 |
|
|
0
|
|
|
|
|
|
|
|
563
|
0
|
0
|
|
|
|
|
if $plot->{stacked} > 0; |
|
564
|
0
|
|
|
|
|
|
@xticks = map { $_ + $barwidth } @xticks |
|
565
|
0
|
0
|
|
|
|
|
if $plot->{stacked} <= 0; # for next iteration |
|
566
|
0
|
|
|
|
|
|
$i++; |
|
567
|
|
|
|
|
|
|
} |
|
568
|
0
|
|
|
|
|
|
my $xticks = '["' . join( '","', @key_order ) . '"]'; |
|
569
|
0
|
|
|
|
|
|
my $ticks = 'yticks'; |
|
570
|
0
|
0
|
|
|
|
|
$ticks = 'xticks' if $plot->{'plot.type'} eq 'bar'; |
|
571
|
0
|
|
|
|
|
|
$_ /= scalar @val for @mean_pos; |
|
572
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.set_$ticks([" |
|
|
0
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
. join( ',', @mean_pos ) |
|
574
|
|
|
|
|
|
|
. "], $xticks)"; |
|
575
|
|
|
|
|
|
|
} else { |
|
576
|
0
|
|
|
|
|
|
die |
|
577
|
|
|
|
|
|
|
"\$plot_type = $plot_type & stacked = $plot->{stacked}and isn't defined."; |
|
578
|
|
|
|
|
|
|
} |
|
579
|
|
|
|
|
|
|
} |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
sub boxplot_helper { |
|
582
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
583
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
584
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
585
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
586
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
587
|
|
|
|
|
|
|
} |
|
588
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
589
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
590
|
|
|
|
|
|
|
'plot', # args to original function |
|
591
|
|
|
|
|
|
|
); |
|
592
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
593
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
594
|
0
|
|
|
|
|
|
p @undef_args; |
|
595
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
596
|
|
|
|
|
|
|
} |
|
597
|
|
|
|
|
|
|
my @opt = ( |
|
598
|
0
|
|
|
|
|
|
@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} } |
|
|
0
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
); |
|
600
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
601
|
|
|
|
|
|
|
my @bad_opt = grep { |
|
602
|
0
|
|
|
|
|
|
my $key = $_; |
|
603
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
604
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
605
|
0
|
0
|
|
|
|
|
if ( scalar @bad_opt > 0 ) { |
|
606
|
0
|
|
|
|
|
|
p @bad_opt; |
|
607
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'} using $current_sub"; |
|
608
|
|
|
|
|
|
|
} |
|
609
|
0
|
|
0
|
|
|
|
$plot->{orientation} = $plot->{orientation} // 'vertical'; |
|
610
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} !~ m/^(?:horizontal|vertical)$/ ) { |
|
611
|
0
|
|
|
|
|
|
die |
|
612
|
|
|
|
|
|
|
"$current_sub needs either \"horizontal\" or \"vertical\", not \"$plot->{orientation}\""; |
|
613
|
|
|
|
|
|
|
} |
|
614
|
0
|
|
|
|
|
|
my ( @xticks, @key_order ); |
|
615
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
616
|
0
|
|
|
|
|
|
@key_order = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
} else { |
|
618
|
0
|
|
|
|
|
|
@key_order = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
} |
|
620
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
621
|
|
|
|
|
|
|
# $plot->{medians} = $plot->{medians} // 1; # by default, show median values |
|
622
|
0
|
|
0
|
|
|
|
$plot->{notch} = $plot->{notch} // 'False'; |
|
623
|
0
|
|
0
|
|
|
|
$plot->{showcaps} = $plot->{showcaps} // 'True'; |
|
624
|
0
|
|
0
|
|
|
|
$plot->{showfliers} = $plot->{showfliers} // 'True'; |
|
625
|
0
|
|
0
|
|
|
|
$plot->{showmeans} = $plot->{showmeans} // 'True'; |
|
626
|
0
|
|
|
|
|
|
my $options = "orientation = '$plot->{orientation}'"; |
|
627
|
0
|
|
|
|
|
|
foreach my $arg ( 'showcaps', 'showfliers', 'showmeans', 'notch') { |
|
628
|
0
|
|
|
|
|
|
$options .= ", $arg = $plot->{$arg}"; |
|
629
|
|
|
|
|
|
|
} |
|
630
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
631
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
632
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
633
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
634
|
|
|
|
|
|
|
} |
|
635
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
} |
|
637
|
0
|
|
|
|
|
|
say { $args->{fh} } 'd = []'; |
|
|
0
|
|
|
|
|
|
|
|
638
|
0
|
|
|
|
|
|
foreach my $key (@key_order) { |
|
639
|
0
|
|
|
|
|
|
@{ $plot->{data}{$key} } = grep { defined } @{ $plot->{data}{$key} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
640
|
0
|
|
|
|
|
|
say { $args->{fh} } 'd.append([' |
|
641
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{$key} } ) . '])'; |
|
|
0
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
} |
|
643
|
0
|
|
|
|
|
|
say { $args->{fh} } "bp = ax$ax.boxplot(d, patch_artist = True, $options)"; |
|
|
0
|
|
|
|
|
|
|
|
644
|
0
|
0
|
|
|
|
|
if ( defined $plot->{colors} ){ # every hash key should have its own color defined |
|
645
|
|
|
|
|
|
|
# the below code helps to provide better error messages in case I make an error in calling the sub |
|
646
|
|
|
|
|
|
|
my @bad_keys = |
|
647
|
0
|
|
|
|
|
|
grep { not defined $plot->{colors}{$_} } keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
648
|
0
|
0
|
|
|
|
|
if ( scalar @bad_keys > 0 ) { |
|
649
|
0
|
|
|
|
|
|
p @bad_keys; |
|
650
|
0
|
|
|
|
|
|
die 'the above data keys have no defined color'; |
|
651
|
|
|
|
|
|
|
} |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
# list of pre-defined colors: https://matplotlib.org/stable/gallery/color/named_colors.html |
|
654
|
0
|
|
|
|
|
|
print { $args->{fh} } 'colors = ["' |
|
655
|
0
|
|
|
|
|
|
. join( '","', @{ $plot->{colors} }{@key_order} ) . '"]' . "\n"; |
|
|
0
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
# the above color list will have the same order, via the above hash slice |
|
658
|
0
|
|
|
|
|
|
say { $args->{fh} } 'for patch, color in zip(bp["boxes"], colors):'; |
|
|
0
|
|
|
|
|
|
|
|
659
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpatch.set_facecolor(color)"; |
|
|
0
|
|
|
|
|
|
|
|
660
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpatch.set_edgecolor('black')"; |
|
|
0
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
} else { |
|
662
|
0
|
|
|
|
|
|
say { $args->{fh} } 'for pc in bp["boxes"]:'; |
|
|
0
|
|
|
|
|
|
|
|
663
|
0
|
0
|
|
|
|
|
if ( defined $plot->{color} ) { |
|
664
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_facecolor('$plot->{color}')"; |
|
|
0
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
} |
|
666
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_edgecolor('black')"; |
|
|
0
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
} |
|
668
|
0
|
|
|
|
|
|
foreach my $key (@key_order) { |
|
669
|
|
|
|
|
|
|
push @xticks, "$key (" |
|
670
|
0
|
|
|
|
|
|
. format_commas( scalar @{ $plot->{data}{$key} }, '%.0u' ) . ')'; |
|
|
0
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
} |
|
672
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} eq 'vertical' ) { |
|
673
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.set_xticks([" |
|
|
0
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
. join( ',', 1 .. scalar @key_order ) . '], ["' |
|
675
|
|
|
|
|
|
|
. join( '","', @xticks ) . '"])'; |
|
676
|
|
|
|
|
|
|
} else { |
|
677
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.set_yticks([" |
|
|
0
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
. join( ',', 1 .. scalar @key_order ) . '], ["' |
|
679
|
|
|
|
|
|
|
. join( '","', @xticks ) . '"])'; |
|
680
|
|
|
|
|
|
|
} |
|
681
|
|
|
|
|
|
|
} |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
sub colored_table_helper { |
|
684
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
685
|
0
|
|
|
|
|
|
my $current_sub = (split(/::/,(caller(0))[3]))[-1]; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
686
|
0
|
0
|
|
|
|
|
if (ref $args ne 'HASH') { |
|
687
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
688
|
|
|
|
|
|
|
} |
|
689
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
690
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
691
|
|
|
|
|
|
|
'plot', # args to original function |
|
692
|
|
|
|
|
|
|
); |
|
693
|
0
|
|
|
|
|
|
my @undef_args = grep {!defined $args->{$_}} @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
694
|
0
|
0
|
|
|
|
|
if (scalar @undef_args > 0) { |
|
695
|
0
|
|
|
|
|
|
p @undef_args; |
|
696
|
0
|
|
|
|
|
|
die "The arguments above are necessary for proper function of $current_sub and weren't defined."; |
|
697
|
|
|
|
|
|
|
} |
|
698
|
|
|
|
|
|
|
# optional args are below |
|
699
|
|
|
|
|
|
|
my @defined_args = (@reqd_args, @ax_methods, @plt_methods, @fig_methods, @arg, |
|
700
|
0
|
|
|
|
|
|
'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
701
|
0
|
|
|
|
|
|
my @bad_args = grep { my $key = $_; not grep {$_ eq $key} @defined_args} keys %{ $args }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
702
|
0
|
0
|
|
|
|
|
if (scalar @bad_args > 0) { |
|
703
|
0
|
|
|
|
|
|
p @bad_args; |
|
704
|
0
|
|
|
|
|
|
say 'the above arguments are not recognized.'; |
|
705
|
0
|
|
|
|
|
|
p @defined_args; |
|
706
|
0
|
|
|
|
|
|
die 'The above args are accepted.' |
|
707
|
|
|
|
|
|
|
} |
|
708
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
709
|
0
|
|
0
|
|
|
|
$plot->{default_undefined} = $plot->{default_undefined} // 0; |
|
710
|
0
|
|
0
|
|
|
|
$plot->{mirror} = $plot->{mirror} // 0; |
|
711
|
|
|
|
|
|
|
# my @data; |
|
712
|
0
|
|
|
|
|
|
my (@cols, @rows, %data); |
|
713
|
0
|
0
|
|
|
|
|
if (defined $plot->{'col.labels'}) { |
|
714
|
0
|
|
|
|
|
|
@cols = @{ $plot->{'col.labels'} }; |
|
|
0
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
} else { |
|
716
|
0
|
|
|
|
|
|
@cols = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
} |
|
718
|
0
|
|
|
|
|
|
foreach my $k1 (@cols) { |
|
719
|
0
|
|
|
|
|
|
foreach my $k2 (keys %{ $plot->{data}{$k1} }) { |
|
|
0
|
|
|
|
|
|
|
|
720
|
0
|
|
|
|
|
|
$data{$k1}{$k2} = $plot->{data}{$k1}{$k2}; |
|
721
|
0
|
0
|
|
|
|
|
$data{$k2}{$k1} = $data{$k1}{$k2} if $plot->{mirror} > 0; |
|
722
|
|
|
|
|
|
|
} |
|
723
|
|
|
|
|
|
|
} |
|
724
|
0
|
0
|
|
|
|
|
if (defined $plot->{'row.labels'}) { |
|
725
|
0
|
|
|
|
|
|
@rows = @{ $plot->{'row.labels'} }; |
|
|
0
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
} else { |
|
727
|
0
|
|
|
|
|
|
@rows = sort keys %data; |
|
728
|
|
|
|
|
|
|
} |
|
729
|
0
|
|
|
|
|
|
my ($min, $max) = ('inf', '-inf'); |
|
730
|
0
|
|
|
|
|
|
say {$args->{fh}} 'd = []'; |
|
|
0
|
|
|
|
|
|
|
|
731
|
0
|
|
|
|
|
|
say {$args->{fh}} 'import numpy as np'; |
|
|
0
|
|
|
|
|
|
|
|
732
|
0
|
|
|
|
|
|
foreach my $k1 (@cols) { |
|
733
|
0
|
|
|
|
|
|
foreach my $k2 (grep {!defined $data{$k1}{$_}} @cols) { |
|
|
0
|
|
|
|
|
|
|
|
734
|
0
|
|
|
|
|
|
$data{$k1}{$k2} = 'np.nan';#$plot->{default_undefined}; |
|
735
|
0
|
|
|
|
|
|
$data{$k2}{$k1} = 'np.nan';#$plot->{default_undefined}; |
|
736
|
|
|
|
|
|
|
} |
|
737
|
0
|
|
|
|
|
|
foreach my $k2 (grep {looks_like_number($data{$k1}{$_})} @cols) { |
|
|
0
|
|
|
|
|
|
|
|
738
|
0
|
|
|
|
|
|
$min = min($min, $data{$k1}{$k2}); |
|
739
|
0
|
|
|
|
|
|
$max = max($max, $data{$k1}{$k2}); |
|
740
|
|
|
|
|
|
|
} |
|
741
|
0
|
|
|
|
|
|
say {$args->{fh}} 'd.append([' . join (',', @{ $data{$k1} }{@cols}) . '])'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
} |
|
743
|
0
|
|
0
|
|
|
|
$min = $args->{cb_min} // $min; |
|
744
|
0
|
|
0
|
|
|
|
$max = $args->{cb_max} // $max; |
|
745
|
0
|
|
0
|
|
|
|
$plot->{cmap} = $plot->{cmap} // 'gist_rainbow'; |
|
746
|
0
|
|
0
|
|
|
|
$plot->{cb_logscale} = $plot->{cb_logscale} // 0; |
|
747
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
748
|
0
|
0
|
|
|
|
|
say {$args->{fh}} 'from matplotlib import colors' if $plot->{cb_logscale} > 0; |
|
|
0
|
|
|
|
|
|
|
|
749
|
0
|
|
0
|
|
|
|
$plot->{'undef.color'} = $plot->{'undef.color'} // 'gray'; |
|
750
|
0
|
|
|
|
|
|
say {$args->{fh}} 'plt.cm.gist_rainbow.set_bad("' . $plot->{'undef.color'} . '")'; |
|
|
0
|
|
|
|
|
|
|
|
751
|
0
|
|
|
|
|
|
say {$args->{fh}} "norm = plt.Normalize($min, $max)"; |
|
|
0
|
|
|
|
|
|
|
|
752
|
0
|
|
|
|
|
|
say {$args->{fh}} 'datacolors = plt.cm.gist_rainbow(norm(d))'; |
|
|
0
|
|
|
|
|
|
|
|
753
|
0
|
|
|
|
|
|
my @options; |
|
754
|
0
|
|
|
|
|
|
my %translate = (cb_min => 'vmin', cb_max => 'vmax'); |
|
755
|
0
|
|
|
|
|
|
foreach my $opt (grep {defined $plot->{$_}} 'cb_min', 'cb_max'){ |
|
|
0
|
|
|
|
|
|
|
|
756
|
0
|
0
|
|
|
|
|
unless (looks_like_number( $plot->{$opt} )) { |
|
757
|
0
|
|
|
|
|
|
die "\"$opt\" = $plot->{$opt} must be a number"; |
|
758
|
|
|
|
|
|
|
} |
|
759
|
0
|
|
|
|
|
|
push @options, "$translate{$opt} = $plot->{$opt}"; |
|
760
|
|
|
|
|
|
|
} |
|
761
|
0
|
|
|
|
|
|
my $opt = join (',', @options); |
|
762
|
0
|
0
|
|
|
|
|
if ($plot->{cb_logscale}) { |
|
763
|
0
|
|
|
|
|
|
say {$args->{fh}} "img = ax$ax.imshow(d, cmap='$plot->{cmap}', norm=colors.LogNorm($opt))"; |
|
|
0
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
} else { |
|
765
|
0
|
|
|
|
|
|
say {$args->{fh}} "img = ax$ax.imshow(d, cmap='$plot->{cmap}' $opt)"; |
|
|
0
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
} |
|
767
|
0
|
|
0
|
|
|
|
$plot->{'colorbar.on'} = $plot->{'colorbar.on'} // 1; |
|
768
|
0
|
0
|
|
|
|
|
if (defined $plot->{cblabel}) { |
|
769
|
0
|
|
|
|
|
|
say {$args->{fh}} "fig.colorbar(img, label = '$plot->{cblabel}')"; |
|
|
0
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
} else { |
|
771
|
0
|
0
|
|
|
|
|
say {$args->{fh}} "fig.colorbar(img)" if $plot->{'colorbar.on'}; |
|
|
0
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
} |
|
773
|
0
|
|
|
|
|
|
say {$args->{fh}} 'img.set_visible(False)'; |
|
|
0
|
|
|
|
|
|
|
|
774
|
0
|
|
0
|
|
|
|
$plot->{'show.numbers'} = $plot->{'show.numbers'} // 0; |
|
775
|
0
|
|
|
|
|
|
say {$args->{fh}} 'for ri, row in enumerate(d):'; |
|
|
0
|
|
|
|
|
|
|
|
776
|
0
|
|
|
|
|
|
say {$args->{fh}} ' for ii, item in enumerate(row):'; |
|
|
0
|
|
|
|
|
|
|
|
777
|
0
|
|
|
|
|
|
say {$args->{fh}} ' if np.isnan(item):'; |
|
|
0
|
|
|
|
|
|
|
|
778
|
0
|
|
|
|
|
|
say {$args->{fh}} ' d[ri][ii] = ""'; |
|
|
0
|
|
|
|
|
|
|
|
779
|
0
|
0
|
|
|
|
|
if ($plot->{'show.numbers'}) { |
|
780
|
0
|
|
|
|
|
|
say {$args->{fh}} "table = ax$ax" . '.table(cellText=d, rowLabels=["' . join ('","', @rows) . '"], colLabels = ["' . join ('","', @cols) . '"], cellColours = datacolors, loc = "center", bbox=[0,0,1,1])'; |
|
|
0
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
} else { |
|
782
|
0
|
|
|
|
|
|
say {$args->{fh}} "table = ax$ax" . '.table(rowLabels=["' . join ('","', @rows) . '"], colLabels = ["' . join ('","', @cols) . '"], cellColours = datacolors, loc = "center", bbox=[0,0,1,1])'; |
|
|
0
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
} |
|
784
|
0
|
|
|
|
|
|
foreach my $arg (grep {defined $plot->{$_}} ('title')) { |
|
|
0
|
|
|
|
|
|
|
|
785
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.$arg('$plot->{$arg}')"; |
|
|
0
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
} |
|
787
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
788
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
789
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
790
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
791
|
|
|
|
|
|
|
} |
|
792
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
} |
|
794
|
|
|
|
|
|
|
# say {$args->{fh}} "fig.clim(vmin = $plot->{cb_min})" if defined $plot->{cb_min}; |
|
795
|
|
|
|
|
|
|
# say {$args->{fh}} "fig.clim(vmax = $plot->{cb_max})" if defined $plot->{cb_max}; |
|
796
|
0
|
|
|
|
|
|
foreach my $axis ('x','y') { |
|
797
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_${axis}ticks" . '([])'; |
|
|
0
|
|
|
|
|
|
|
|
798
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_${axis}ticklabels" . '([])'; |
|
|
0
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
} |
|
800
|
|
|
|
|
|
|
} |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
sub hexbin_helper { |
|
803
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
804
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
805
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
806
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
807
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
808
|
|
|
|
|
|
|
} |
|
809
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
810
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
811
|
|
|
|
|
|
|
'plot', # args to original function |
|
812
|
|
|
|
|
|
|
); |
|
813
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
814
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
815
|
0
|
|
|
|
|
|
p @undef_args; |
|
816
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
817
|
|
|
|
|
|
|
} |
|
818
|
|
|
|
|
|
|
my @opt = ( |
|
819
|
|
|
|
|
|
|
@ax_methods, @fig_methods, @arg, @plt_methods, |
|
820
|
0
|
|
|
|
|
|
'ax', @{ $opt{$current_sub} } |
|
|
0
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
); |
|
822
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
823
|
|
|
|
|
|
|
@undef_args = grep { |
|
824
|
0
|
|
|
|
|
|
my $key = $_; |
|
825
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
826
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
827
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
828
|
0
|
|
|
|
|
|
p @undef_args; |
|
829
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
830
|
|
|
|
|
|
|
} |
|
831
|
0
|
|
0
|
|
|
|
$plot->{cb_logscale} = $plot->{cb_logscale} // 0; |
|
832
|
0
|
|
0
|
|
|
|
$plot->{marginals} = $plot->{marginals} // 0; |
|
833
|
0
|
|
0
|
|
|
|
$plot->{xbins} = $plot->{xbins} // 15; |
|
834
|
0
|
|
0
|
|
|
|
$plot->{ybins} = $plot->{ybins} // 15; |
|
835
|
0
|
|
|
|
|
|
$plot->{xbins} = int $plot->{xbins}; |
|
836
|
0
|
|
|
|
|
|
$plot->{ybins} = int $plot->{ybins}; |
|
837
|
0
|
0
|
0
|
|
|
|
if ( ( $plot->{xbins} == 0 ) || ( $plot->{ybins} == 0 ) ) { |
|
838
|
0
|
|
|
|
|
|
p $plot; |
|
839
|
0
|
|
|
|
|
|
die "# of bins cannot be 0 in $current_sub"; |
|
840
|
|
|
|
|
|
|
} |
|
841
|
0
|
|
|
|
|
|
my @keys; |
|
842
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
843
|
0
|
|
|
|
|
|
@keys = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
} else { |
|
845
|
0
|
|
|
|
|
|
@keys = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
} |
|
847
|
0
|
0
|
|
|
|
|
if ( scalar @keys != 2 ) { |
|
848
|
0
|
|
|
|
|
|
p @keys; |
|
849
|
0
|
|
|
|
|
|
die "There must be exactly 2 keys for $current_sub"; |
|
850
|
|
|
|
|
|
|
} |
|
851
|
0
|
|
|
|
|
|
my $n_points = scalar @{ $plot->{data}{ $keys[0] } }; |
|
|
0
|
|
|
|
|
|
|
|
852
|
0
|
0
|
|
|
|
|
if ( scalar @{ $plot->{data}{ $keys[1] } } != $n_points ) { |
|
|
0
|
|
|
|
|
|
|
|
853
|
0
|
|
|
|
|
|
say "\"$keys[0]\" has $n_points points."; |
|
854
|
|
|
|
|
|
|
say "\"$keys[1]\" has " |
|
855
|
0
|
|
|
|
|
|
. scalar @{ $plot->{data}{ $keys[1] } } |
|
|
0
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
. " points."; |
|
857
|
0
|
|
|
|
|
|
die 'The length of both keys must be equal.'; |
|
858
|
|
|
|
|
|
|
} |
|
859
|
0
|
|
0
|
|
|
|
$plot->{xlabel} = $plot->{xlabel} // $keys[0]; |
|
860
|
0
|
|
0
|
|
|
|
$plot->{ylabel} = $plot->{ylabel} // $keys[1]; |
|
861
|
0
|
|
0
|
|
|
|
$plot->{cmap} = $plot->{cmap} // 'gist_rainbow'; |
|
862
|
0
|
|
|
|
|
|
my $options = |
|
863
|
|
|
|
|
|
|
", gridsize = ($plot->{xbins}, $plot->{ybins}), cmap = '$plot->{cmap}'" |
|
864
|
|
|
|
|
|
|
; # these args go to the plt.hist call |
|
865
|
0
|
0
|
|
|
|
|
if ( $plot->{cb_logscale} ) { |
|
866
|
0
|
|
|
|
|
|
say { $args->{fh} } 'from matplotlib.colors import LogNorm'; |
|
|
0
|
|
|
|
|
|
|
|
867
|
0
|
|
|
|
|
|
$options .= ', norm = LogNorm()'; |
|
868
|
|
|
|
|
|
|
} |
|
869
|
0
|
|
|
|
|
|
foreach my $opt ( |
|
870
|
0
|
|
|
|
|
|
grep { defined $plot->{$_} } ('xrange', 'yrange', 'vmin', 'vmax', 'mincnt') |
|
871
|
|
|
|
|
|
|
) |
|
872
|
|
|
|
|
|
|
{ |
|
873
|
0
|
|
|
|
|
|
$options .= ", $opt = $plot->{$opt}"; |
|
874
|
|
|
|
|
|
|
} |
|
875
|
0
|
|
|
|
|
|
foreach my $opt (grep {defined $plot->{$_} } ('xscale.hexbin', 'yscale.hexbin')) { |
|
|
0
|
|
|
|
|
|
|
|
876
|
0
|
0
|
0
|
|
|
|
if (($plot->{$opt} ne 'log') && ($plot->{$opt} ne 'linear')) { |
|
877
|
0
|
|
|
|
|
|
die "\"$opt\" is neither \"log\" nor \"linear\""; |
|
878
|
|
|
|
|
|
|
} |
|
879
|
0
|
|
|
|
|
|
my $opth = $opt; |
|
880
|
0
|
|
|
|
|
|
$opth =~ s/\.\w+$//; |
|
881
|
0
|
|
|
|
|
|
$options .= ", $opth = '$plot->{$opt}'"; |
|
882
|
|
|
|
|
|
|
} |
|
883
|
0
|
0
|
0
|
|
|
|
if ((defined $plot->{marginals}) && ($plot->{marginals} > 0)) { |
|
884
|
0
|
|
|
|
|
|
$options .= ', marginals = True'; |
|
885
|
|
|
|
|
|
|
} |
|
886
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' |
|
887
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{ $keys[0] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
888
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' |
|
889
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{ $keys[1] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
890
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
891
|
0
|
|
|
|
|
|
say { $args->{fh} } "im$ax = ax$ax.hexbin(x, y $options)\n"; |
|
|
0
|
|
|
|
|
|
|
|
892
|
0
|
|
|
|
|
|
my $opts = ''; |
|
893
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cblabel', 'cblocation', 'cborientation')) { #str |
|
|
0
|
|
|
|
|
|
|
|
894
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
895
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
896
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = '$plot->{$o}'"; |
|
897
|
|
|
|
|
|
|
} |
|
898
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cbdrawedges', 'cbpad')) { # numeric |
|
|
0
|
|
|
|
|
|
|
|
899
|
0
|
0
|
|
|
|
|
die "$o = $plot->{$o} must be numeric" unless (looks_like_number($plot->{$o})); |
|
900
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
901
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
902
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = $plot->{$o}"; |
|
903
|
|
|
|
|
|
|
} |
|
904
|
0
|
|
0
|
|
|
|
$plot->{'colorbar.on'} = $plot->{'colorbar.on'} // 1; |
|
905
|
0
|
0
|
0
|
|
|
|
if (($plot->{'colorbar.on'}) && (defined $plot->{'shared.colorbar'})) { |
|
906
|
0
|
|
|
|
|
|
my @ax = map {"ax$_"} @{ $plot->{'shared.colorbar'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
907
|
0
|
|
|
|
|
|
$opts .= ', ax = [' . join (',', @ax) . '] '; |
|
908
|
|
|
|
|
|
|
} |
|
909
|
0
|
0
|
|
|
|
|
if ( defined $plot->{cblabel} ) { |
|
910
|
|
|
|
|
|
|
write_data({ |
|
911
|
|
|
|
|
|
|
data => $plot->{cblabel}, |
|
912
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
913
|
0
|
|
|
|
|
|
name => 'cblabel', |
|
914
|
|
|
|
|
|
|
}); |
|
915
|
0
|
|
|
|
|
|
say { $args->{fh} } "plt.colorbar(im$ax, label = cblabel $opts)"; |
|
|
0
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
} else { |
|
917
|
0
|
|
|
|
|
|
say { $args->{fh} } "plt.colorbar(im$ax, label = 'Density' $opts)"; |
|
|
0
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
} |
|
919
|
|
|
|
|
|
|
} |
|
920
|
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
sub format_commas |
|
922
|
|
|
|
|
|
|
{ #($n, $format = '.%02d') { # https://stackoverflow.com/questions/33442240/perl-printf-to-use-commas-as-thousands-separator |
|
923
|
|
|
|
|
|
|
# $format should be '%.0u' for integers |
|
924
|
0
|
|
|
0
|
0
|
|
my ( $n, $format ) = @_; |
|
925
|
0
|
0
|
|
|
|
|
$format = '.%02d' if not defined $format; |
|
926
|
|
|
|
|
|
|
return |
|
927
|
0
|
|
|
|
|
|
reverse( join( ",", unpack( "(A3)*", reverse int($n) ) ) ) |
|
928
|
|
|
|
|
|
|
. sprintf( $format, int( 100 * ( .005 + ( $n - int($n) ) ) ) ); |
|
929
|
|
|
|
|
|
|
} |
|
930
|
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
sub hist_helper { |
|
932
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
933
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
934
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
935
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
936
|
0
|
|
|
|
|
|
die |
|
937
|
|
|
|
|
|
|
"args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
938
|
|
|
|
|
|
|
} |
|
939
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
940
|
|
|
|
|
|
|
'ax', # used for multiple plots |
|
941
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
942
|
|
|
|
|
|
|
'plot', # args to original function |
|
943
|
|
|
|
|
|
|
); |
|
944
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
945
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
946
|
0
|
|
|
|
|
|
p @undef_args; |
|
947
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
948
|
|
|
|
|
|
|
} |
|
949
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
950
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
951
|
|
|
|
|
|
|
my @undef_opt = grep { |
|
952
|
0
|
|
|
|
|
|
my $key = $_; |
|
953
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
954
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
955
|
0
|
0
|
|
|
|
|
if ( scalar @undef_opt > 0 ) { |
|
956
|
0
|
|
|
|
|
|
p @undef_opt; |
|
957
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'}"; |
|
958
|
|
|
|
|
|
|
} |
|
959
|
0
|
|
|
|
|
|
my $options = ''; # these args go to the plt.hist call |
|
960
|
0
|
|
0
|
|
|
|
$plot->{alpha} = $plot->{alpha} // 0.5; |
|
961
|
0
|
|
|
|
|
|
foreach my $arg ( grep { defined $plot->{$_} } ( 'bins', 'orientation' ) ) { |
|
|
0
|
|
|
|
|
|
|
|
962
|
0
|
0
|
|
|
|
|
next if ref $plot->{$arg} eq 'HASH'; # set-specific setting exists |
|
963
|
0
|
|
|
|
|
|
my $ref = ref $plot->{$arg}; |
|
964
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { # single color |
|
|
|
0
|
|
|
|
|
|
|
965
|
0
|
0
|
|
|
|
|
if ( $plot->{$arg} =~ m/^[A-Za-z]+$/ ) { # "Red" needs quotes |
|
966
|
0
|
|
|
|
|
|
$options .= ", $arg = '$plot->{$arg}'"; |
|
967
|
|
|
|
|
|
|
} else { # I'm assuming numeric |
|
968
|
0
|
|
|
|
|
|
$options .= ", $arg = $plot->{$arg}"; |
|
969
|
|
|
|
|
|
|
} |
|
970
|
|
|
|
|
|
|
} elsif ( $ref eq 'ARRAY' ) { |
|
971
|
0
|
|
|
|
|
|
$options .= ", $arg = [" . join( ',', @{ $plot->{$arg} } ) . '"]'; |
|
|
0
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
} else { |
|
973
|
0
|
|
|
|
|
|
p $plot; |
|
974
|
0
|
|
|
|
|
|
die "$ref for $arg isn't acceptable"; |
|
975
|
|
|
|
|
|
|
} |
|
976
|
|
|
|
|
|
|
} |
|
977
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
978
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
979
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
980
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
981
|
|
|
|
|
|
|
} |
|
982
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$args->{ax}.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
} |
|
984
|
0
|
|
|
|
|
|
foreach my $set ( sort keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
985
|
0
|
|
|
|
|
|
my $set_options = ''; |
|
986
|
0
|
|
|
|
|
|
foreach |
|
987
|
0
|
|
|
|
|
|
my $arg ( grep { ref $plot->{$_} eq 'HASH' } ( 'bins', 'color' ) ) |
|
988
|
|
|
|
|
|
|
{ |
|
989
|
0
|
0
|
|
|
|
|
next unless defined $plot->{$arg}{$set}; |
|
990
|
0
|
0
|
|
|
|
|
if ( $plot->{$arg}{$set} =~ m/^[A-Za-z]+$/ ) { # "Red" needs quotes |
|
991
|
0
|
|
|
|
|
|
$set_options .= ", $arg = '$plot->{$arg}{$set}'"; |
|
992
|
|
|
|
|
|
|
} else { # I'm assuming numeric; "10" doesn't need quotes |
|
993
|
0
|
|
|
|
|
|
$set_options .= ", $arg = $plot->{$arg}{$set}"; |
|
994
|
|
|
|
|
|
|
} |
|
995
|
|
|
|
|
|
|
} |
|
996
|
|
|
|
|
|
|
write_data({ |
|
997
|
|
|
|
|
|
|
data => $plot->{data}{$set}, |
|
998
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
999
|
0
|
|
|
|
|
|
name => 'd' |
|
1000
|
|
|
|
|
|
|
}); |
|
1001
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$args->{ax}.hist(d, alpha = $plot->{alpha}, label = '$set' $options $set_options)"; |
|
|
0
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
} |
|
1003
|
|
|
|
|
|
|
} |
|
1004
|
|
|
|
|
|
|
|
|
1005
|
|
|
|
|
|
|
sub hist2d_helper { |
|
1006
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1007
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1008
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1009
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1010
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1011
|
|
|
|
|
|
|
} |
|
1012
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1013
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1014
|
|
|
|
|
|
|
'plot', # args to original function |
|
1015
|
|
|
|
|
|
|
); |
|
1016
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1017
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1018
|
0
|
|
|
|
|
|
p @undef_args; |
|
1019
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1020
|
|
|
|
|
|
|
} |
|
1021
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1022
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1023
|
|
|
|
|
|
|
@undef_args = grep { |
|
1024
|
0
|
|
|
|
|
|
my $key = $_; |
|
1025
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1026
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1027
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1028
|
0
|
|
|
|
|
|
p @undef_args; |
|
1029
|
0
|
|
|
|
|
|
die |
|
1030
|
|
|
|
|
|
|
"The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
1031
|
|
|
|
|
|
|
} |
|
1032
|
0
|
|
0
|
|
|
|
$plot->{cb_logscale} = $plot->{cb_logscale} // 0; |
|
1033
|
0
|
|
0
|
|
|
|
$plot->{'show.colorbar'} = $plot->{'show.colorbar'} // 1; |
|
1034
|
0
|
|
0
|
|
|
|
$plot->{xbins} = int( $plot->{xbins} // 15 ); |
|
1035
|
0
|
|
0
|
|
|
|
$plot->{ybins} = int( $plot->{ybins} // 15 ); |
|
1036
|
0
|
0
|
0
|
|
|
|
if ( ( $plot->{xbins} == 0 ) || ( $plot->{ybins} == 0 ) ) { |
|
1037
|
0
|
|
|
|
|
|
p $plot; |
|
1038
|
0
|
|
|
|
|
|
die "# of bins cannot be 0 in $current_sub"; |
|
1039
|
|
|
|
|
|
|
} |
|
1040
|
0
|
|
|
|
|
|
my @keys; |
|
1041
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
1042
|
0
|
|
|
|
|
|
@keys = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
} else { |
|
1044
|
0
|
|
|
|
|
|
@keys = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
} |
|
1046
|
0
|
0
|
|
|
|
|
if ( scalar @keys != 2 ) { |
|
1047
|
0
|
|
|
|
|
|
p @keys; |
|
1048
|
0
|
|
|
|
|
|
die "There must be exactly 2 keys for $current_sub"; |
|
1049
|
|
|
|
|
|
|
} |
|
1050
|
0
|
|
|
|
|
|
my $n_points = scalar @{ $plot->{data}{ $keys[0] } }; |
|
|
0
|
|
|
|
|
|
|
|
1051
|
0
|
0
|
|
|
|
|
if ( scalar @{ $plot->{data}{ $keys[1] } } != $n_points ) { |
|
|
0
|
|
|
|
|
|
|
|
1052
|
0
|
|
|
|
|
|
say "$keys[0] has $n_points points."; |
|
1053
|
|
|
|
|
|
|
say "$keys[1] has " |
|
1054
|
0
|
|
|
|
|
|
. scalar @{ $plot->{data}{ $keys[1] } } |
|
|
0
|
|
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
. " points."; |
|
1056
|
0
|
|
|
|
|
|
die 'The length of both keys must be equal.'; |
|
1057
|
|
|
|
|
|
|
} |
|
1058
|
0
|
0
|
|
|
|
|
if ($plot->{xlabel}) { |
|
1059
|
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
} |
|
1061
|
0
|
|
0
|
|
|
|
$plot->{xlabel} = $plot->{xlabel} // $keys[0]; |
|
1062
|
0
|
|
0
|
|
|
|
$plot->{ylabel} = $plot->{ylabel} // $keys[1]; |
|
1063
|
0
|
|
0
|
|
|
|
$plot->{cmap} = $plot->{cmap} // 'gist_rainbow'; |
|
1064
|
0
|
|
|
|
|
|
my $options = ", cmap = '$plot->{cmap}'"; # these args go to the plt.hist call |
|
1065
|
0
|
0
|
|
|
|
|
if ( $plot->{cb_logscale} ) { |
|
1066
|
0
|
|
|
|
|
|
say {$args->{fh}} 'from matplotlib.colors import LogNorm'; |
|
|
0
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
# prevents "ValueError: Passing a Normalize instance simultaneously with vmin/vmax is not supported. Please pass vmin/vmax directly to the norm when creating it" |
|
1068
|
0
|
|
|
|
|
|
my @logNorm_opt; |
|
1069
|
0
|
|
|
|
|
|
foreach my $arg (grep {defined $plot->{$_}} ('vmin', 'vmax')) { |
|
|
0
|
|
|
|
|
|
|
|
1070
|
0
|
0
|
|
|
|
|
if (not looks_like_number($plot->{$arg})) { |
|
1071
|
0
|
|
|
|
|
|
die "$arg must be numeric for $current_sub, but was given \"$plot->{$arg}\""; |
|
1072
|
|
|
|
|
|
|
} |
|
1073
|
0
|
|
|
|
|
|
push @logNorm_opt, "$arg = $plot->{$arg}"; |
|
1074
|
0
|
|
|
|
|
|
delete $plot->{$arg}; |
|
1075
|
|
|
|
|
|
|
} |
|
1076
|
0
|
|
|
|
|
|
$options .= ', norm = LogNorm(' . join (',', @logNorm_opt) . ')'; |
|
1077
|
|
|
|
|
|
|
} |
|
1078
|
0
|
|
|
|
|
|
foreach my $opt ( grep { defined $plot->{$_} } ( 'cmin', 'cmax', 'density', 'vmin', 'vmax' ) ) |
|
|
0
|
|
|
|
|
|
|
|
1079
|
|
|
|
|
|
|
{ |
|
1080
|
0
|
|
|
|
|
|
$options .= ", $opt = $plot->{$opt}"; |
|
1081
|
|
|
|
|
|
|
} |
|
1082
|
0
|
|
|
|
|
|
my @bad_indices; |
|
1083
|
0
|
|
|
|
|
|
my $bad_pts = 0; |
|
1084
|
0
|
|
|
|
|
|
foreach my $i (0,1) { |
|
1085
|
0
|
|
|
|
|
|
@{ $bad_indices[$i] } = grep {not defined $plot->{data}{$keys[$i]}[$_]} 0..$n_points-1; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1086
|
0
|
|
|
|
|
|
$bad_pts += scalar @{ $bad_indices[$i] }; |
|
|
0
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
} |
|
1088
|
0
|
0
|
|
|
|
|
if ($bad_pts > 0) { |
|
1089
|
0
|
|
|
|
|
|
say STDERR "the above args have the following indices undefined ($n_points total)"; |
|
1090
|
0
|
|
|
|
|
|
p @bad_indices; |
|
1091
|
0
|
|
|
|
|
|
die "Cannot proceed as there are $bad_pts undefined points."; |
|
1092
|
|
|
|
|
|
|
} |
|
1093
|
0
|
|
|
|
|
|
foreach my $i (0,1) { |
|
1094
|
0
|
|
|
|
|
|
@{ $bad_indices[$i] } = grep {not looks_like_number($plot->{data}{$keys[$i]}[$_])} 0..$n_points-1; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1095
|
0
|
|
|
|
|
|
$bad_pts += scalar @{ $bad_indices[$i] }; |
|
|
0
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
} |
|
1097
|
0
|
0
|
|
|
|
|
if ($bad_pts > 0) { |
|
1098
|
0
|
|
|
|
|
|
p $args; |
|
1099
|
0
|
|
|
|
|
|
say STDERR "the above args have the following indices non-numeric ($n_points total)"; |
|
1100
|
0
|
|
|
|
|
|
p @bad_indices; |
|
1101
|
0
|
|
|
|
|
|
die "Cannot proceed as there are $bad_pts non-numeric points."; |
|
1102
|
|
|
|
|
|
|
} |
|
1103
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' |
|
1104
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{ $keys[0] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1105
|
0
|
|
0
|
|
|
|
$plot->{xmin} = $plot->{xmin} // min( @{ $plot->{data}{ $keys[0] } } ); |
|
|
0
|
|
|
|
|
|
|
|
1106
|
0
|
|
0
|
|
|
|
$plot->{xmax} = $plot->{xmax} // max( @{ $plot->{data}{ $keys[0] } } ); |
|
|
0
|
|
|
|
|
|
|
|
1107
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' |
|
1108
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{ $keys[1] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1109
|
0
|
|
0
|
|
|
|
$plot->{ymin} = $plot->{ymin} // min( @{ $plot->{data}{ $keys[1] } } ); |
|
|
0
|
|
|
|
|
|
|
|
1110
|
0
|
|
0
|
|
|
|
$plot->{ymax} = $plot->{ymax} // max( @{ $plot->{data}{ $keys[1] } } ); |
|
|
0
|
|
|
|
|
|
|
|
1111
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
1112
|
|
|
|
|
|
|
# the range argument ensures that there are no empty parts of the plot |
|
1113
|
0
|
|
|
|
|
|
my $range = ", range = [($plot->{xmin}, $plot->{xmax}), ($plot->{ymin}, $plot->{ymax})]"; |
|
1114
|
|
|
|
|
|
|
# logscale complications |
|
1115
|
0
|
|
|
|
|
|
say {$args->{fh}} 'import numpy as np'; |
|
|
0
|
|
|
|
|
|
|
|
1116
|
0
|
0
|
|
|
|
|
if ($plot->{logscale}) { |
|
1117
|
0
|
|
|
|
|
|
my %linear_axes = ('x' => 1, 'y' => 1); |
|
1118
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
1119
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
1120
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
1121
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
1122
|
|
|
|
|
|
|
} |
|
1123
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
1124
|
0
|
|
|
|
|
|
my $min = $plot->{$axis . 'min'}; |
|
1125
|
0
|
|
|
|
|
|
my $max = $plot->{$axis . 'max'}; |
|
1126
|
0
|
|
|
|
|
|
my $key = "${axis}bins"; |
|
1127
|
0
|
|
|
|
|
|
say {$args->{fh}} "${axis}bins = np.logspace(np.log10($min), np.log10($max), $plot->{$key})"; |
|
|
0
|
|
|
|
|
|
|
|
1128
|
0
|
|
|
|
|
|
delete $linear_axes{$axis}; # so that I don't make a linear space for it |
|
1129
|
|
|
|
|
|
|
} |
|
1130
|
0
|
|
|
|
|
|
foreach my $axis ( keys %linear_axes ) { |
|
1131
|
0
|
|
|
|
|
|
my $min = $plot->{$axis . 'min'}; |
|
1132
|
0
|
|
|
|
|
|
my $max = $plot->{$axis . 'max'}; |
|
1133
|
0
|
|
|
|
|
|
my $key = "${axis}bins"; |
|
1134
|
0
|
|
|
|
|
|
say {$args->{fh}} "${axis}bins = np.linspace($min, $max, $plot->{$key})"; |
|
|
0
|
|
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
} |
|
1136
|
0
|
|
|
|
|
|
$options = ", bins = [xbins, ybins] $options"; |
|
1137
|
|
|
|
|
|
|
} else { |
|
1138
|
0
|
|
|
|
|
|
$options = ", ($plot->{xbins}, $plot->{ybins}) $options"; |
|
1139
|
|
|
|
|
|
|
} |
|
1140
|
0
|
|
|
|
|
|
say {$args->{fh}} "hist2d_n, hist2d_xedges, hist2d_yedges, im$ax = ax$ax.hist2d(x, y $options $range)"; |
|
|
0
|
|
|
|
|
|
|
|
1141
|
0
|
|
|
|
|
|
say {$args->{fh}} 'max_hist2d_box = np.max(hist2d_n)'; |
|
|
0
|
|
|
|
|
|
|
|
1142
|
0
|
|
|
|
|
|
say {$args->{fh}} 'min_hist2d_box = np.min(hist2d_n)'; |
|
|
0
|
|
|
|
|
|
|
|
1143
|
0
|
|
|
|
|
|
say {$args->{fh}} "print(f'plot $ax hist2d density range = [{min_hist2d_box}, {max_hist2d_box}]')"; |
|
|
0
|
|
|
|
|
|
|
|
1144
|
0
|
0
|
|
|
|
|
return 0 if $plot->{'show.colorbar'} == 0; |
|
1145
|
0
|
|
|
|
|
|
my $opts = ''; |
|
1146
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cblabel', 'cblocation', 'cborientation')) { #str |
|
|
0
|
|
|
|
|
|
|
|
1147
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
1148
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
1149
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = '$plot->{$o}'"; |
|
1150
|
|
|
|
|
|
|
} |
|
1151
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cbdrawedges', 'cbpad')) { # numeric |
|
|
0
|
|
|
|
|
|
|
|
1152
|
0
|
0
|
|
|
|
|
die "$o = $plot->{$o} must be numeric" unless (looks_like_number($plot->{$o})); |
|
1153
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
1154
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
1155
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = $plot->{$o}"; |
|
1156
|
|
|
|
|
|
|
} |
|
1157
|
0
|
|
0
|
|
|
|
$plot->{'colorbar.on'} = $plot->{'colorbar.on'} // 1; |
|
1158
|
0
|
0
|
0
|
|
|
|
if (($plot->{'colorbar.on'}) && (defined $plot->{'shared.colorbar'})) { |
|
1159
|
0
|
|
|
|
|
|
my @ax = map {"ax$_"} @{ $plot->{'shared.colorbar'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1160
|
0
|
|
|
|
|
|
$opts .= ', ax = [' . join (',', @ax) . '] '; |
|
1161
|
|
|
|
|
|
|
} |
|
1162
|
|
|
|
|
|
|
# say { $args->{fh} } "cbar = fig.colorbar(im$ax $opts)" if $plot->{'colorbar.on'}; |
|
1163
|
0
|
0
|
|
|
|
|
if ( defined $plot->{cblabel} ) { |
|
1164
|
0
|
|
|
|
|
|
say { $args->{fh} } "plt.colorbar(im$ax, label = '$plot->{cblabel}' $opts)"; |
|
|
0
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
} else { |
|
1166
|
0
|
|
|
|
|
|
say { $args->{fh} } "plt.colorbar(im$ax, label = 'Density' $opts)"; |
|
|
0
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
} |
|
1168
|
|
|
|
|
|
|
} |
|
1169
|
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
sub imshow_helper { |
|
1171
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1172
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1173
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1174
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1175
|
0
|
|
|
|
|
|
die |
|
1176
|
|
|
|
|
|
|
"args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1177
|
|
|
|
|
|
|
} |
|
1178
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1179
|
|
|
|
|
|
|
'ax', |
|
1180
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1181
|
|
|
|
|
|
|
'plot', # args to original function |
|
1182
|
|
|
|
|
|
|
); |
|
1183
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1184
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1185
|
0
|
|
|
|
|
|
p @undef_args; |
|
1186
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1187
|
|
|
|
|
|
|
} |
|
1188
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1189
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1190
|
|
|
|
|
|
|
@undef_args = grep { |
|
1191
|
0
|
|
|
|
|
|
my $key = $_; |
|
1192
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1193
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1194
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1195
|
0
|
|
|
|
|
|
p @undef_args; |
|
1196
|
0
|
|
|
|
|
|
die |
|
1197
|
|
|
|
|
|
|
"The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
1198
|
|
|
|
|
|
|
} |
|
1199
|
0
|
|
|
|
|
|
my $data_ref = ref $plot->{data}; |
|
1200
|
0
|
0
|
|
|
|
|
if ($data_ref ne 'ARRAY') { |
|
1201
|
0
|
|
|
|
|
|
p $args; |
|
1202
|
0
|
|
|
|
|
|
die "$current_sub can only accept 2-D arrays as input in \"data\", but received $data_ref"; |
|
1203
|
|
|
|
|
|
|
} |
|
1204
|
0
|
|
|
|
|
|
my $non_numeric_data = 0; |
|
1205
|
0
|
|
|
|
|
|
foreach my $row (@{ $plot->{data} }) { |
|
|
0
|
|
|
|
|
|
|
|
1206
|
0
|
0
|
|
|
|
|
if (grep {not looks_like_number($_)} @{ $row }) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1207
|
0
|
|
|
|
|
|
$non_numeric_data = 1; |
|
1208
|
0
|
|
|
|
|
|
last; |
|
1209
|
|
|
|
|
|
|
} |
|
1210
|
|
|
|
|
|
|
} |
|
1211
|
0
|
0
|
0
|
|
|
|
if (($non_numeric_data) && (not defined $plot->{stringmap})) { |
|
1212
|
0
|
|
|
|
|
|
p $args; |
|
1213
|
0
|
|
|
|
|
|
die "$current_sub needs a map to translate strings to integers"; |
|
1214
|
|
|
|
|
|
|
} |
|
1215
|
0
|
|
|
|
|
|
my (@ytick_labels, %intmap); |
|
1216
|
|
|
|
|
|
|
# assign integers for each key in stringmap |
|
1217
|
0
|
|
|
|
|
|
foreach my $string (sort keys %{ $plot->{stringmap} }) { |
|
|
0
|
|
|
|
|
|
|
|
1218
|
0
|
|
|
|
|
|
$intmap{$string} = scalar @ytick_labels; |
|
1219
|
0
|
|
|
|
|
|
push @ytick_labels, $plot->{stringmap}{$string}; |
|
1220
|
|
|
|
|
|
|
} |
|
1221
|
0
|
0
|
|
|
|
|
if ($non_numeric_data) { |
|
1222
|
0
|
|
|
|
|
|
foreach my $row (@{ $plot->{data} }) { |
|
|
0
|
|
|
|
|
|
|
|
1223
|
0
|
|
|
|
|
|
@{ $row } = map { $intmap{$_} } @{ $row }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1224
|
|
|
|
|
|
|
} |
|
1225
|
|
|
|
|
|
|
} |
|
1226
|
0
|
|
|
|
|
|
my ($min_val, $max_val) = ('inf', '-inf'); |
|
1227
|
0
|
|
|
|
|
|
my $opts = ''; |
|
1228
|
0
|
0
|
|
|
|
|
if ($non_numeric_data) { |
|
1229
|
0
|
|
|
|
|
|
say { $args->{fh} } 'from matplotlib.colors import ListedColormap'; |
|
|
0
|
|
|
|
|
|
|
|
1230
|
0
|
|
|
|
|
|
my @prop_cycle = ('#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', |
|
1231
|
|
|
|
|
|
|
'#7f7f7f', '#bcbd22', '#17becf'); #plt.rcParams['axes.prop_cycle'] |
|
1232
|
0
|
|
|
|
|
|
say { $args->{fh} } 'colors = ["' . join ('","', @prop_cycle[0..(scalar keys %intmap) - 1]) . '"]'; |
|
|
0
|
|
|
|
|
|
|
|
1233
|
0
|
|
|
|
|
|
say { $args->{fh} } 'this_cmap = ListedColormap(colors)'; |
|
|
0
|
|
|
|
|
|
|
|
1234
|
0
|
0
|
|
|
|
|
warn "deleting \"$plot->{cmap}\" because string data is present" if defined $plot->{cmap}; |
|
1235
|
0
|
|
|
|
|
|
delete $plot->{cmap}; |
|
1236
|
0
|
|
|
|
|
|
$min_val = 0; |
|
1237
|
0
|
|
|
|
|
|
$max_val = max(values %intmap); |
|
1238
|
0
|
|
|
|
|
|
$opts .= ', cmap = this_cmap'; |
|
1239
|
|
|
|
|
|
|
} |
|
1240
|
|
|
|
|
|
|
write_data({ |
|
1241
|
|
|
|
|
|
|
data => $plot->{data}, |
|
1242
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
1243
|
0
|
|
|
|
|
|
name => 'd', |
|
1244
|
|
|
|
|
|
|
}); |
|
1245
|
0
|
|
|
|
|
|
foreach my $row (@{ $plot->{data} }) { # write data to the python file |
|
|
0
|
|
|
|
|
|
|
|
1246
|
0
|
0
|
|
|
|
|
next if $non_numeric_data; # strings don't have max and min |
|
1247
|
0
|
|
|
|
|
|
$min_val = min(@{ $row }, $min_val); |
|
|
0
|
|
|
|
|
|
|
|
1248
|
0
|
|
|
|
|
|
$max_val = max(@{ $row }, $max_val); |
|
|
0
|
|
|
|
|
|
|
|
1249
|
|
|
|
|
|
|
} |
|
1250
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
1251
|
0
|
|
0
|
|
|
|
$plot->{vmax} = $plot->{vmax} // $max_val; |
|
1252
|
0
|
|
0
|
|
|
|
$plot->{vmin} = $plot->{vmin} // $min_val; |
|
1253
|
0
|
|
|
|
|
|
foreach my $opt (grep {defined $plot->{$_}} ('cmap')) { # strings |
|
|
0
|
|
|
|
|
|
|
|
1254
|
0
|
|
|
|
|
|
$opts .= ", $opt = '$plot->{$opt}'"; |
|
1255
|
|
|
|
|
|
|
} |
|
1256
|
0
|
|
|
|
|
|
foreach my $opt (grep {defined $plot->{$_}} ('vmax', 'vmin')) { # numeric |
|
|
0
|
|
|
|
|
|
|
|
1257
|
0
|
|
|
|
|
|
$opts .= ", $opt = $plot->{$opt}"; |
|
1258
|
|
|
|
|
|
|
} |
|
1259
|
0
|
|
|
|
|
|
say { $args->{fh} } "im$ax = ax$ax.imshow(d, aspect = 'auto' $opts)"; |
|
|
0
|
|
|
|
|
|
|
|
1260
|
0
|
|
|
|
|
|
$opts = ''; |
|
1261
|
0
|
0
|
|
|
|
|
if ($non_numeric_data) { |
|
1262
|
0
|
|
|
|
|
|
$opts .= ', ticks = [' . join (',', map {$_ + 0.5 - $_/$max_val} 0..$max_val) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1263
|
|
|
|
|
|
|
} |
|
1264
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cblabel', 'cblocation', 'cborientation')) { #str |
|
|
0
|
|
|
|
|
|
|
|
1265
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
1266
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
1267
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = '$plot->{$o}'"; |
|
1268
|
|
|
|
|
|
|
} |
|
1269
|
0
|
|
|
|
|
|
foreach my $o (grep {defined $plot->{$_}} ('cbdrawedges', 'cbpad')) { # numeric |
|
|
0
|
|
|
|
|
|
|
|
1270
|
0
|
0
|
|
|
|
|
die "$o = $plot->{$o} must be numeric" unless (looks_like_number($plot->{$o})); |
|
1271
|
0
|
|
|
|
|
|
my $mpl_opt = $o; |
|
1272
|
0
|
|
|
|
|
|
$mpl_opt =~ s/^cb//; |
|
1273
|
0
|
|
|
|
|
|
$opts .= ", $mpl_opt = $plot->{$o}"; |
|
1274
|
|
|
|
|
|
|
} |
|
1275
|
0
|
|
0
|
|
|
|
$plot->{'colorbar.on'} = $plot->{'colorbar.on'} // 1; |
|
1276
|
0
|
0
|
0
|
|
|
|
if (($plot->{'colorbar.on'}) && (defined $plot->{'shared.colorbar'})) { |
|
1277
|
0
|
|
|
|
|
|
my @ax = map {"ax$_"} @{ $plot->{'shared.colorbar'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1278
|
0
|
|
|
|
|
|
$opts .= ', ax = [' . join (',', @ax) . '] '; |
|
1279
|
|
|
|
|
|
|
} |
|
1280
|
0
|
0
|
|
|
|
|
say { $args->{fh} } "cbar = fig.colorbar(im$ax $opts)" if $plot->{'colorbar.on'}; |
|
|
0
|
|
|
|
|
|
|
|
1281
|
0
|
0
|
0
|
|
|
|
if (($non_numeric_data) && ($plot->{'colorbar.on'})) { |
|
1282
|
0
|
|
|
|
|
|
say { $args->{fh} } 'cbar.ax.set_yticklabels(["' . join ('","', @ytick_labels) . '"])'; |
|
|
0
|
|
|
|
|
|
|
|
1283
|
|
|
|
|
|
|
} |
|
1284
|
|
|
|
|
|
|
} |
|
1285
|
|
|
|
|
|
|
|
|
1286
|
|
|
|
|
|
|
sub pie_helper { |
|
1287
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1288
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1289
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1290
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1291
|
0
|
|
|
|
|
|
die |
|
1292
|
|
|
|
|
|
|
"args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1293
|
|
|
|
|
|
|
} |
|
1294
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1295
|
|
|
|
|
|
|
'ax', |
|
1296
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1297
|
|
|
|
|
|
|
'plot', # args to original function |
|
1298
|
|
|
|
|
|
|
); |
|
1299
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1300
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1301
|
0
|
|
|
|
|
|
p @undef_args; |
|
1302
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1303
|
|
|
|
|
|
|
} |
|
1304
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1305
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1306
|
|
|
|
|
|
|
my @undef_opt = grep { |
|
1307
|
0
|
|
|
|
|
|
my $key = $_; |
|
1308
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1309
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1310
|
0
|
0
|
|
|
|
|
if ( scalar @undef_opt > 0 ) { |
|
1311
|
0
|
|
|
|
|
|
p @undef_opt; |
|
1312
|
0
|
|
|
|
|
|
die |
|
1313
|
|
|
|
|
|
|
"The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
1314
|
|
|
|
|
|
|
} |
|
1315
|
0
|
|
|
|
|
|
my @key_order; |
|
1316
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
1317
|
0
|
|
|
|
|
|
@key_order = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
1318
|
|
|
|
|
|
|
} else { |
|
1319
|
0
|
|
|
|
|
|
@key_order = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1320
|
|
|
|
|
|
|
} |
|
1321
|
0
|
|
0
|
|
|
|
$plot->{autopct} = $plot->{autopct} // ''; |
|
1322
|
0
|
|
|
|
|
|
my $opt = ''; |
|
1323
|
0
|
0
|
|
|
|
|
if ( $plot->{autopct} ne '' ) { |
|
1324
|
0
|
|
|
|
|
|
$opt .= ", autopct = '$plot->{autopct}'"; |
|
1325
|
|
|
|
|
|
|
} |
|
1326
|
0
|
|
|
|
|
|
foreach my $arg ( grep { defined $plot->{$_} } 'labeldistance', 'pctdistance' ) { |
|
|
0
|
|
|
|
|
|
|
|
1327
|
0
|
|
|
|
|
|
$opt .= ", $arg = $plot->{$arg}"; |
|
1328
|
|
|
|
|
|
|
} |
|
1329
|
|
|
|
|
|
|
write_data({ |
|
1330
|
|
|
|
|
|
|
data => \@key_order, |
|
1331
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
1332
|
0
|
|
|
|
|
|
name => 'labels', |
|
1333
|
|
|
|
|
|
|
}); |
|
1334
|
0
|
|
|
|
|
|
say { $args->{fh} } 'vals = [' |
|
1335
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data} }{@key_order} ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1336
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
1337
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.pie(vals, labels = labels $opt)"; |
|
|
0
|
|
|
|
|
|
|
|
1338
|
|
|
|
|
|
|
} |
|
1339
|
|
|
|
|
|
|
|
|
1340
|
|
|
|
|
|
|
sub plot_helper { |
|
1341
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1342
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1343
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1344
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1345
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1346
|
|
|
|
|
|
|
} |
|
1347
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1348
|
|
|
|
|
|
|
'ax', 'fh',# e.g. $py, $fh, which will be passed by the subroutine |
|
1349
|
|
|
|
|
|
|
'plot', # args to original function |
|
1350
|
|
|
|
|
|
|
); |
|
1351
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1352
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1353
|
0
|
|
|
|
|
|
p @undef_args; |
|
1354
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1355
|
|
|
|
|
|
|
} |
|
1356
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @fig_methods, @arg, @plt_methods, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1357
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1358
|
|
|
|
|
|
|
my @bad_opt = grep { |
|
1359
|
0
|
|
|
|
|
|
my $key = $_; |
|
1360
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1361
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1362
|
0
|
0
|
|
|
|
|
if ( scalar @bad_opt > 0 ) { |
|
1363
|
0
|
|
|
|
|
|
p $args; |
|
1364
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1365
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
1366
|
|
|
|
|
|
|
} |
|
1367
|
0
|
|
0
|
|
|
|
$plot->{'show.legend'} = $plot->{'show.legend'} // 1; |
|
1368
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
1369
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
1370
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
1371
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
1372
|
|
|
|
|
|
|
} |
|
1373
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$args->{ax}.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
1374
|
|
|
|
|
|
|
} |
|
1375
|
0
|
|
|
|
|
|
my @twinx; |
|
1376
|
0
|
0
|
|
|
|
|
if (ref $plot->{data} eq 'ARRAY') { |
|
1377
|
0
|
0
|
|
|
|
|
if (defined $plot->{'set.options'}) { |
|
1378
|
0
|
|
|
|
|
|
my $ref_type = ref $plot->{'set.options'}; |
|
1379
|
0
|
0
|
|
|
|
|
if ($ref_type ne 'ARRAY') { |
|
1380
|
0
|
|
|
|
|
|
p $args; |
|
1381
|
0
|
|
|
|
|
|
die "\"set.options\" must also be an array when the data is an array, but \"$ref_type\" was given." ; |
|
1382
|
|
|
|
|
|
|
} |
|
1383
|
0
|
|
|
|
|
|
my $n_set_opt = scalar @{ $plot->{'set.options'} }; |
|
|
0
|
|
|
|
|
|
|
|
1384
|
0
|
|
|
|
|
|
my $n_data = scalar @{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1385
|
0
|
0
|
|
|
|
|
if ($n_set_opt > $n_data) { |
|
1386
|
0
|
|
|
|
|
|
p $args; |
|
1387
|
0
|
|
|
|
|
|
die "there are $n_set_opt sets for options, but only $n_data data points."; |
|
1388
|
|
|
|
|
|
|
} |
|
1389
|
|
|
|
|
|
|
} |
|
1390
|
0
|
0
|
|
|
|
|
if (defined $plot->{twinx}) { |
|
1391
|
0
|
0
|
|
|
|
|
if (ref $plot->{twinx} eq '') { |
|
|
|
0
|
|
|
|
|
|
|
1392
|
0
|
0
|
|
|
|
|
die "twinx must be an array index, not \"$plot->{twinx}\"" unless $plot->{twinx} =~ m/^\d+$/; |
|
1393
|
0
|
|
|
|
|
|
@twinx = $plot->{twinx}; |
|
1394
|
|
|
|
|
|
|
} elsif (ref $plot->{twinx} eq 'ARRAY') { |
|
1395
|
0
|
|
|
|
|
|
@bad_opt = grep {$_ !~ m/^\d+$/} @{ $plot->{twinx} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1396
|
0
|
0
|
|
|
|
|
if (scalar @bad_opt > 0) { |
|
1397
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1398
|
0
|
|
|
|
|
|
die 'data that could not possibly be an array index for twinx is shown above'; |
|
1399
|
|
|
|
|
|
|
} |
|
1400
|
0
|
|
|
|
|
|
@twinx = @{ $plot->{twinx} }; |
|
|
0
|
|
|
|
|
|
|
|
1401
|
|
|
|
|
|
|
} |
|
1402
|
|
|
|
|
|
|
} |
|
1403
|
0
|
0
|
|
|
|
|
if (defined $plot->{'twinx.args'}) { |
|
1404
|
0
|
|
|
|
|
|
my $ref = ref $plot->{'twinx.args'}; |
|
1405
|
0
|
0
|
|
|
|
|
die "\"twinx.args\" must be a hash, but $ref was entered" unless $ref eq 'HASH'; |
|
1406
|
0
|
|
|
|
|
|
@bad_opt = grep {$_ !~ m/^\d+$/} keys %{ $plot->{'twinx.args'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1407
|
0
|
0
|
|
|
|
|
if (scalar @bad_opt > 0) { |
|
1408
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1409
|
0
|
|
|
|
|
|
die 'the above keys are not hash indices'; |
|
1410
|
|
|
|
|
|
|
} |
|
1411
|
0
|
|
|
|
|
|
foreach my $idx (keys %{ $plot->{'twinx.args'} }) { |
|
|
0
|
|
|
|
|
|
|
|
1412
|
0
|
0
|
|
|
|
|
next if grep {$idx == $_} @twinx; |
|
|
0
|
|
|
|
|
|
|
|
1413
|
0
|
|
|
|
|
|
push @twinx, $idx; |
|
1414
|
|
|
|
|
|
|
} |
|
1415
|
|
|
|
|
|
|
} |
|
1416
|
0
|
|
|
|
|
|
my $arr_i = 0; |
|
1417
|
0
|
|
|
|
|
|
foreach my $arr (@{ $plot->{data} }) { |
|
|
0
|
|
|
|
|
|
|
|
1418
|
0
|
|
|
|
|
|
my $ref = ref $arr; |
|
1419
|
0
|
0
|
|
|
|
|
if ($ref ne 'ARRAY') { |
|
1420
|
0
|
|
|
|
|
|
p $plot->{data}[$arr_i]; |
|
1421
|
0
|
|
|
|
|
|
die "index $arr_i is \"$ref\" but must be \"ARRAY\""; |
|
1422
|
|
|
|
|
|
|
} |
|
1423
|
0
|
|
|
|
|
|
my $n_elem = scalar @{ $arr }; |
|
|
0
|
|
|
|
|
|
|
|
1424
|
0
|
0
|
|
|
|
|
if ($n_elem != 2) { |
|
1425
|
0
|
|
|
|
|
|
p $arr; |
|
1426
|
0
|
|
|
|
|
|
die "there must be 2 array references (x, y) but index $arr_i has $n_elem"; |
|
1427
|
|
|
|
|
|
|
} |
|
1428
|
0
|
|
|
|
|
|
@bad_opt = map { scalar @{ $arr->[$_] } } (0,1); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1429
|
0
|
0
|
|
|
|
|
if ($bad_opt[0] != $bad_opt[1]) { |
|
1430
|
0
|
|
|
|
|
|
say STDERR "index $arr_i must have arrays/array refs of equal length, but these lengths were given:"; |
|
1431
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1432
|
0
|
|
|
|
|
|
die 'the array/array ref lengths must be equal'; |
|
1433
|
|
|
|
|
|
|
} |
|
1434
|
0
|
|
|
|
|
|
foreach my $i (0,1) { |
|
1435
|
0
|
|
|
|
|
|
my $max_i = scalar @{ $arr->[$i] } - 1; |
|
|
0
|
|
|
|
|
|
|
|
1436
|
0
|
|
|
|
|
|
@bad_opt = grep {not looks_like_number($arr->[$i][$_])} 0..$max_i; |
|
|
0
|
|
|
|
|
|
|
|
1437
|
0
|
0
|
|
|
|
|
next if scalar @bad_opt == 0; # it's fine, don't worry |
|
1438
|
0
|
|
|
|
|
|
p $plot->{data}[$arr_i]; |
|
1439
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1440
|
0
|
|
|
|
|
|
@bad_opt = @{ $arr->[$i] }[@bad_opt]; |
|
|
0
|
|
|
|
|
|
|
|
1441
|
0
|
|
|
|
|
|
say STDERR 'have these non-numeric values:'; |
|
1442
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1443
|
0
|
|
|
|
|
|
die "Array index $arr_i/axis $i has non-numeric values (above)"; |
|
1444
|
|
|
|
|
|
|
} |
|
1445
|
0
|
|
|
|
|
|
my $options = ''; |
|
1446
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' . join( ',', @{ $arr->[0] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1447
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' . join( ',', @{ $arr->[1] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1448
|
0
|
0
|
0
|
|
|
|
if ( ( defined $plot->{'set.options'} ) |
|
1449
|
|
|
|
|
|
|
&& ( ref $plot->{'set.options'} eq '' ) ) |
|
1450
|
|
|
|
|
|
|
{ |
|
1451
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}"; |
|
1452
|
|
|
|
|
|
|
} |
|
1453
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'set.options'}[$arr_i] ) { |
|
1454
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}[$arr_i]"; |
|
1455
|
|
|
|
|
|
|
} |
|
1456
|
0
|
|
|
|
|
|
my $ax = "ax$args->{ax}"; |
|
1457
|
0
|
0
|
|
|
|
|
if (grep {$arr_i == $_} @twinx) { |
|
|
0
|
|
|
|
|
|
|
|
1458
|
0
|
|
|
|
|
|
say { $args->{fh} } "twinx_$ax = $ax.twinx()# " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1459
|
0
|
|
|
|
|
|
say { $args->{fh} } "twinx_$ax.plot(x, y $options) # " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1460
|
0
|
0
|
|
|
|
|
if (defined $plot->{'twinx.args'}{$arr_i}) { |
|
1461
|
|
|
|
|
|
|
plot_args({ |
|
1462
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
1463
|
0
|
|
|
|
|
|
args => $plot->{'twinx.args'}{$arr_i}, |
|
1464
|
|
|
|
|
|
|
ax => "twinx_$ax" |
|
1465
|
|
|
|
|
|
|
}); |
|
1466
|
|
|
|
|
|
|
} |
|
1467
|
|
|
|
|
|
|
} else { |
|
1468
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$args->{ax}.plot(x, y $options) # " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1469
|
|
|
|
|
|
|
} |
|
1470
|
0
|
|
|
|
|
|
$arr_i++; |
|
1471
|
|
|
|
|
|
|
} |
|
1472
|
0
|
|
|
|
|
|
return 1; # the rest only applies if $plot->{data} is a hash |
|
1473
|
|
|
|
|
|
|
} |
|
1474
|
0
|
|
|
|
|
|
my @key_order; |
|
1475
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
1476
|
0
|
|
|
|
|
|
@key_order = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
} else { |
|
1478
|
0
|
|
|
|
|
|
@key_order = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1479
|
|
|
|
|
|
|
} |
|
1480
|
0
|
0
|
0
|
|
|
|
if ((defined $plot->{'set.options'}) && (ref $plot->{'set.options'} eq 'HASH')) { |
|
1481
|
0
|
|
|
|
|
|
@bad_opt = sort grep {!defined $plot->{data}{$_}} keys %{ $plot->{'set.options'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1482
|
0
|
0
|
|
|
|
|
if (scalar @bad_opt > 0) { |
|
1483
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1484
|
0
|
|
|
|
|
|
die "the above options are defined for undefined data sets in $current_sub."; |
|
1485
|
|
|
|
|
|
|
} |
|
1486
|
|
|
|
|
|
|
} |
|
1487
|
0
|
0
|
|
|
|
|
if (defined $plot->{twinx}) { |
|
1488
|
0
|
0
|
|
|
|
|
if (ref $plot->{twinx} eq '') { |
|
|
|
0
|
|
|
|
|
|
|
1489
|
0
|
0
|
|
|
|
|
die "twinx must be an hash index, not \"$plot->{twinx}\"" unless $plot->{twinx} =~ m/^\d+$/; |
|
1490
|
0
|
|
|
|
|
|
@twinx = $plot->{twinx}; |
|
1491
|
|
|
|
|
|
|
} elsif (ref $plot->{twinx} eq 'HASH') { |
|
1492
|
0
|
|
|
|
|
|
@bad_opt = sort grep {!defined $plot->{data}{$_}} keys %{ $plot->{twinx} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1493
|
0
|
0
|
|
|
|
|
if (scalar @bad_opt > 0) { |
|
1494
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1495
|
0
|
|
|
|
|
|
die 'data for undefined data keys is shown above'; |
|
1496
|
|
|
|
|
|
|
} |
|
1497
|
0
|
|
|
|
|
|
@twinx = sort keys %{ $plot->{twinx} }; |
|
|
0
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
} |
|
1499
|
|
|
|
|
|
|
} |
|
1500
|
0
|
0
|
|
|
|
|
if (defined $plot->{'twinx.args'}) { |
|
1501
|
0
|
|
|
|
|
|
my $ref = ref $plot->{'twinx.args'}; |
|
1502
|
0
|
0
|
|
|
|
|
die "\"twinx.args\" must be a hash, but $ref was entered" unless $ref eq 'HASH'; |
|
1503
|
0
|
|
|
|
|
|
@bad_opt = sort grep {!defined $plot->{data}{$_}} keys %{ $plot->{'twinx.args'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1504
|
0
|
0
|
|
|
|
|
if (scalar @bad_opt > 0) { |
|
1505
|
0
|
|
|
|
|
|
p @bad_opt; |
|
1506
|
0
|
|
|
|
|
|
die 'the above keys are not present in data keys'; |
|
1507
|
|
|
|
|
|
|
} |
|
1508
|
0
|
|
|
|
|
|
foreach my $set (keys %{ $plot->{'twinx.args'} }) { |
|
|
0
|
|
|
|
|
|
|
|
1509
|
0
|
0
|
|
|
|
|
next if grep {$set eq $_} @twinx; |
|
|
0
|
|
|
|
|
|
|
|
1510
|
0
|
|
|
|
|
|
push @twinx, $set; |
|
1511
|
|
|
|
|
|
|
} |
|
1512
|
|
|
|
|
|
|
} |
|
1513
|
0
|
|
|
|
|
|
my $set_i = 0; |
|
1514
|
0
|
|
|
|
|
|
foreach my $set (@key_order) { |
|
1515
|
0
|
|
|
|
|
|
my $set_ref = ref $plot->{data}{$set}; |
|
1516
|
0
|
0
|
|
|
|
|
if ( $set_ref ne 'ARRAY' ) { |
|
1517
|
0
|
|
|
|
|
|
p $plot->{data}{$set}; |
|
1518
|
0
|
|
|
|
|
|
die "$set must have two arrays, x and y coordinates, but instead has a $set_ref"; |
|
1519
|
|
|
|
|
|
|
} |
|
1520
|
0
|
|
|
|
|
|
my $n_arrays = scalar @{ $plot->{data}{$set} }; |
|
|
0
|
|
|
|
|
|
|
|
1521
|
0
|
0
|
|
|
|
|
if ( $n_arrays != 2 ) { |
|
1522
|
0
|
|
|
|
|
|
p $plot->{data}{$set}; |
|
1523
|
0
|
|
|
|
|
|
die "$n_arrays were entered for $set, but there must be exactly 2"; |
|
1524
|
|
|
|
|
|
|
} |
|
1525
|
0
|
|
|
|
|
|
my @n_elem = map { scalar @{ $plot->{data}{$set}[$_] }} (0,1); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1526
|
0
|
0
|
|
|
|
|
if ( $n_elem[0] != $n_elem[1] ) { |
|
1527
|
0
|
|
|
|
|
|
p $plot->{data}{$set}; |
|
1528
|
0
|
|
|
|
|
|
p @n_elem; |
|
1529
|
0
|
|
|
|
|
|
die "$set has length = $n_elem[0] for x; length = $n_elem[1] for y: x & y must be of equal length"; |
|
1530
|
|
|
|
|
|
|
} |
|
1531
|
0
|
|
|
|
|
|
foreach my $ax (0,1) { |
|
1532
|
0
|
|
|
|
|
|
my $n = scalar @{ $plot->{data}{$set}[$ax] }; |
|
|
0
|
|
|
|
|
|
|
|
1533
|
0
|
|
|
|
|
|
my @undef_i = grep {not defined $plot->{data}{$set}[$ax][$_]} 0..$n-1; |
|
|
0
|
|
|
|
|
|
|
|
1534
|
0
|
0
|
|
|
|
|
if (scalar @undef_i > 0) { |
|
1535
|
0
|
|
|
|
|
|
p $plot; |
|
1536
|
0
|
|
|
|
|
|
p @undef_i; |
|
1537
|
0
|
|
|
|
|
|
my $max_i = scalar @{ $plot->{data}{$set}[$ax] }; |
|
|
0
|
|
|
|
|
|
|
|
1538
|
0
|
|
|
|
|
|
die "set $set axis $ax has undefined indices, of max index $max_i in $current_sub"; |
|
1539
|
|
|
|
|
|
|
} |
|
1540
|
|
|
|
|
|
|
} |
|
1541
|
0
|
|
|
|
|
|
my $options = ''; |
|
1542
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' . join( ',', @{ $plot->{data}{$set}[0] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1543
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' . join( ',', @{ $plot->{data}{$set}[1] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1544
|
0
|
0
|
0
|
|
|
|
if ( ( defined $plot->{'set.options'} ) |
|
1545
|
|
|
|
|
|
|
&& ( ref $plot->{'set.options'} eq '' ) ) |
|
1546
|
|
|
|
|
|
|
{ |
|
1547
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}"; |
|
1548
|
|
|
|
|
|
|
} |
|
1549
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'set.options'}{$set} ) { |
|
1550
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}{$set}"; |
|
1551
|
|
|
|
|
|
|
} |
|
1552
|
0
|
|
|
|
|
|
my $label = ''; |
|
1553
|
0
|
0
|
|
|
|
|
if ( $plot->{'show.legend'} ) { |
|
1554
|
0
|
|
|
|
|
|
$label = ",label = '$set'"; |
|
1555
|
|
|
|
|
|
|
} |
|
1556
|
0
|
|
|
|
|
|
my $ax = "ax$args->{ax}"; |
|
1557
|
0
|
0
|
|
|
|
|
if (grep {$set eq $_} @twinx) { # this set has |
|
|
0
|
|
|
|
|
|
|
|
1558
|
0
|
|
|
|
|
|
say { $args->{fh} } "twinx_${ax}_$set_i = $ax.twinx()# " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1559
|
0
|
|
|
|
|
|
say { $args->{fh} } "twinx_${ax}_$set_i.plot(x, y $label $options) # " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1560
|
0
|
0
|
|
|
|
|
if (defined $plot->{'twinx.args'}{$set}) { |
|
1561
|
|
|
|
|
|
|
plot_args({ |
|
1562
|
|
|
|
|
|
|
fh => $args->{fh}, |
|
1563
|
0
|
|
|
|
|
|
args => $plot->{'twinx.args'}{$set}, |
|
1564
|
|
|
|
|
|
|
ax => "twinx_${ax}_$set_i" |
|
1565
|
|
|
|
|
|
|
}); |
|
1566
|
|
|
|
|
|
|
} |
|
1567
|
|
|
|
|
|
|
} else { |
|
1568
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$args->{ax}.plot(x, y $label $options) # " . __LINE__; |
|
|
0
|
|
|
|
|
|
|
|
1569
|
|
|
|
|
|
|
} |
|
1570
|
0
|
|
|
|
|
|
$set_i++; |
|
1571
|
|
|
|
|
|
|
} |
|
1572
|
0
|
|
|
|
|
|
return 1; |
|
1573
|
|
|
|
|
|
|
} |
|
1574
|
|
|
|
|
|
|
|
|
1575
|
|
|
|
|
|
|
sub scatter_helper { |
|
1576
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1577
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1578
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1579
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1580
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1581
|
|
|
|
|
|
|
} |
|
1582
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1583
|
|
|
|
|
|
|
'ax', 'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1584
|
|
|
|
|
|
|
'plot', # args to original function |
|
1585
|
|
|
|
|
|
|
); |
|
1586
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1587
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1588
|
0
|
|
|
|
|
|
p @undef_args; |
|
1589
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1590
|
|
|
|
|
|
|
} |
|
1591
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1592
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1593
|
|
|
|
|
|
|
@undef_args = grep { |
|
1594
|
0
|
|
|
|
|
|
my $key = $_; |
|
1595
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1596
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1597
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1598
|
0
|
|
|
|
|
|
p @undef_args; |
|
1599
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'} in $current_sub"; |
|
1600
|
|
|
|
|
|
|
} |
|
1601
|
0
|
|
|
|
|
|
my $overall_ref = ref $plot->{data}; |
|
1602
|
0
|
0
|
|
|
|
|
if ( $overall_ref ne 'HASH' ) { |
|
1603
|
0
|
|
|
|
|
|
die |
|
1604
|
|
|
|
|
|
|
"scatter only takes 1) hashes of arrays (single or 2) hash of hash of arrays; but $overall_ref was entered"; |
|
1605
|
|
|
|
|
|
|
} |
|
1606
|
0
|
|
|
|
|
|
my ( %ref_counts, $plot_type ); |
|
1607
|
0
|
|
|
|
|
|
foreach my $set ( keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
1608
|
0
|
|
|
|
|
|
$ref_counts{ ref $plot->{data}{$set} }++; |
|
1609
|
|
|
|
|
|
|
} |
|
1610
|
0
|
|
|
|
|
|
my $ax = $args->{ax}; |
|
1611
|
0
|
0
|
|
|
|
|
if ( scalar %ref_counts > 1 ) { |
|
1612
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
1613
|
0
|
|
|
|
|
|
die "different kinds of data were entered to plot $ax; should be simple hash or hash of arrays."; |
|
1614
|
|
|
|
|
|
|
} |
|
1615
|
0
|
0
|
|
|
|
|
if ( defined $ref_counts{ARRAY} ) { |
|
|
|
0
|
|
|
|
|
|
|
1616
|
0
|
|
|
|
|
|
$plot_type = 'single'; |
|
1617
|
|
|
|
|
|
|
} elsif ( defined $ref_counts{HASH} ) { |
|
1618
|
0
|
|
|
|
|
|
$plot_type = 'multiple'; |
|
1619
|
|
|
|
|
|
|
} else { |
|
1620
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
1621
|
0
|
|
|
|
|
|
p %ref_counts; |
|
1622
|
0
|
|
|
|
|
|
die 'Could not determine scatter type for the above data.'; |
|
1623
|
|
|
|
|
|
|
} |
|
1624
|
0
|
|
0
|
|
|
|
$plot->{cmap} = $plot->{cmap} // 'gist_rainbow'; |
|
1625
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
1626
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
1627
|
|
|
|
|
|
|
} |
|
1628
|
0
|
0
|
|
|
|
|
if ( $plot_type eq 'single' ) { # only a single set of data |
|
|
|
0
|
|
|
|
|
|
|
1629
|
0
|
|
|
|
|
|
my $options = ''; |
|
1630
|
0
|
|
|
|
|
|
my ( $color_key, @keys ); |
|
1631
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'keys'} ) { |
|
1632
|
0
|
|
|
|
|
|
@keys = @{ $plot->{'keys'} }; |
|
|
0
|
|
|
|
|
|
|
|
1633
|
|
|
|
|
|
|
} else { |
|
1634
|
0
|
|
|
|
|
|
@keys = sort { lc $a cmp lc $b } keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1635
|
|
|
|
|
|
|
} |
|
1636
|
0
|
|
|
|
|
|
my $n_keys = scalar keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1637
|
0
|
0
|
0
|
|
|
|
if ( ( $n_keys != 2 ) && ( $n_keys != 3 ) ) { |
|
1638
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
1639
|
0
|
|
|
|
|
|
die "scatterplots can only take 2 or 3 keys as data, but $current_sub received $n_keys"; |
|
1640
|
|
|
|
|
|
|
} |
|
1641
|
0
|
0
|
|
|
|
|
if ( defined $plot->{color_key} ) { |
|
|
|
0
|
|
|
|
|
|
|
1642
|
0
|
|
|
|
|
|
$color_key = $plot->{color_key}; |
|
1643
|
0
|
|
|
|
|
|
@keys = grep {$_ ne $plot->{color_key}} @keys; |
|
|
0
|
|
|
|
|
|
|
|
1644
|
|
|
|
|
|
|
} elsif ( scalar @keys == 3 ) { |
|
1645
|
0
|
|
|
|
|
|
$color_key = pop @keys; |
|
1646
|
|
|
|
|
|
|
} |
|
1647
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' . join( ',', @{ $plot->{data}{ $keys[0] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1648
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' . join( ',', @{ $plot->{data}{ $keys[1] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1649
|
0
|
0
|
0
|
|
|
|
if ( ( defined $plot->{'set.options'} ) |
|
1650
|
|
|
|
|
|
|
&& ( ref $plot->{'set.options'} eq '' ) ) |
|
1651
|
|
|
|
|
|
|
{ |
|
1652
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}"; |
|
1653
|
|
|
|
|
|
|
} |
|
1654
|
0
|
0
|
|
|
|
|
if ( defined $color_key ) { |
|
1655
|
0
|
|
|
|
|
|
say { $args->{fh} } 'z = [' |
|
1656
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{$color_key} } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1657
|
0
|
|
|
|
|
|
say { $args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
"im = ax$ax.scatter(x, y, c = z, cmap = 'gist_rainbow' $options)"; |
|
1659
|
0
|
|
|
|
|
|
say { $args->{fh} } "fig.colorbar(im, label = '$color_key')"; |
|
|
0
|
|
|
|
|
|
|
|
1660
|
|
|
|
|
|
|
} else { |
|
1661
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.scatter(x, y, $options)"; |
|
|
0
|
|
|
|
|
|
|
|
1662
|
|
|
|
|
|
|
} |
|
1663
|
0
|
|
0
|
|
|
|
$plot->{xlabel} = $plot->{xlabel} // $keys[0]; |
|
1664
|
0
|
|
0
|
|
|
|
$plot->{ylabel} = $plot->{ylabel} // $keys[1]; |
|
1665
|
|
|
|
|
|
|
} elsif ( $plot_type eq 'multiple' ) { # multiple sets |
|
1666
|
0
|
|
|
|
|
|
my @undefined_opts; |
|
1667
|
0
|
|
|
|
|
|
foreach my $set ( sort keys %{ $plot->{'set.options'} } ) { |
|
|
0
|
|
|
|
|
|
|
|
1668
|
0
|
0
|
|
|
|
|
next if grep { $set eq $_ } keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1669
|
0
|
|
|
|
|
|
push @undefined_opts, $set; |
|
1670
|
|
|
|
|
|
|
} |
|
1671
|
0
|
0
|
|
|
|
|
if ( scalar @undefined_opts > 0 ) { |
|
1672
|
0
|
|
|
|
|
|
p $plot->{data}; |
|
1673
|
0
|
|
|
|
|
|
p $plot; |
|
1674
|
0
|
|
|
|
|
|
say 'The data and options are above, but the following sets have options without data:'; |
|
1675
|
0
|
|
|
|
|
|
p @undefined_opts; |
|
1676
|
0
|
|
|
|
|
|
die 'no data was defined for the above options'; |
|
1677
|
|
|
|
|
|
|
} |
|
1678
|
0
|
|
|
|
|
|
my $color_key; |
|
1679
|
0
|
|
|
|
|
|
foreach my $set ( sort keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
1680
|
0
|
|
|
|
|
|
my $options = ''; |
|
1681
|
0
|
|
|
|
|
|
my @keys; |
|
1682
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'keys'} ) { |
|
1683
|
0
|
|
|
|
|
|
@keys = @{ $plot->{'keys'} }; |
|
|
0
|
|
|
|
|
|
|
|
1684
|
|
|
|
|
|
|
} else { # automatically take the key from the first; further sets should have the same labels |
|
1685
|
0
|
|
|
|
|
|
@keys = sort { lc $a cmp lc $b } keys %{ $plot->{data}{$set} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1686
|
|
|
|
|
|
|
} |
|
1687
|
0
|
|
|
|
|
|
my $n_keys = scalar keys %{ $plot->{data}{$set} }; |
|
|
0
|
|
|
|
|
|
|
|
1688
|
0
|
0
|
0
|
|
|
|
if ( ( $n_keys != 2 ) && ( $n_keys != 3 ) ) { |
|
1689
|
0
|
|
|
|
|
|
p $plot->{data}{$set}; |
|
1690
|
0
|
|
|
|
|
|
die "scatterplots can only take 2 or 3 keys as data, but $current_sub received $n_keys"; |
|
1691
|
|
|
|
|
|
|
} |
|
1692
|
0
|
|
|
|
|
|
foreach my $key (@keys) { |
|
1693
|
0
|
|
|
|
|
|
@undef_args = grep {!defined $plot->{data}{$set}{$key}[$_]} 0..scalar @{ $plot->{data}{$set}{$key} } - 1; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1694
|
0
|
0
|
|
|
|
|
if (scalar @undef_args > 0) { |
|
1695
|
0
|
|
|
|
|
|
p @undef_args; |
|
1696
|
0
|
|
|
|
|
|
die "the above indices for \"$key\" are undefined in $current_sub"; |
|
1697
|
|
|
|
|
|
|
} |
|
1698
|
|
|
|
|
|
|
} |
|
1699
|
0
|
0
|
0
|
|
|
|
if ( ( not defined $color_key ) && ( $n_keys == 3 ) ) { |
|
1700
|
0
|
|
|
|
|
|
$color_key = pop @keys; |
|
1701
|
|
|
|
|
|
|
} |
|
1702
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'set.options'}{$set} ) { |
|
1703
|
0
|
|
|
|
|
|
$options = ", $plot->{'set.options'}{$set}"; |
|
1704
|
|
|
|
|
|
|
} |
|
1705
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' . join( ',', @{ $plot->{data}{$set}{ $keys[0] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1706
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' . join( ',', @{ $plot->{data}{$set}{ $keys[1] } } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1707
|
0
|
0
|
|
|
|
|
if ( defined $color_key ) { |
|
1708
|
0
|
|
|
|
|
|
say { $args->{fh} } 'z = [' . join( ',', @{ $plot->{data}{$set}{$color_key} } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1709
|
0
|
0
|
|
|
|
|
unless ( $options =~ m/label\s*=/ ) { |
|
1710
|
0
|
|
|
|
|
|
$options .= ", label = '$set'"; |
|
1711
|
|
|
|
|
|
|
} |
|
1712
|
0
|
|
|
|
|
|
say { $args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1713
|
|
|
|
|
|
|
"im = ax$ax.scatter(x, y, c = z, cmap = '$plot->{cmap}' $options)"; |
|
1714
|
|
|
|
|
|
|
} else { |
|
1715
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.scatter(x, y, label = '$set' $options)"; |
|
|
0
|
|
|
|
|
|
|
|
1716
|
|
|
|
|
|
|
} |
|
1717
|
0
|
|
0
|
|
|
|
$plot->{xlabel} = $plot->{xlabel} // $keys[0]; |
|
1718
|
0
|
|
0
|
|
|
|
$plot->{ylabel} = $plot->{ylabel} // $keys[1]; |
|
1719
|
|
|
|
|
|
|
} |
|
1720
|
0
|
0
|
|
|
|
|
say { $args->{fh} } "plt.colorbar(im, label = '$color_key')" if defined $color_key; |
|
|
0
|
|
|
|
|
|
|
|
1721
|
|
|
|
|
|
|
} |
|
1722
|
|
|
|
|
|
|
} |
|
1723
|
|
|
|
|
|
|
|
|
1724
|
|
|
|
|
|
|
sub violin_helper { |
|
1725
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1726
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1727
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1728
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1729
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1730
|
|
|
|
|
|
|
} |
|
1731
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1732
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1733
|
|
|
|
|
|
|
'plot', # args to original function |
|
1734
|
|
|
|
|
|
|
); |
|
1735
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1736
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1737
|
0
|
|
|
|
|
|
p @undef_args; |
|
1738
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
1739
|
|
|
|
|
|
|
} |
|
1740
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1741
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1742
|
|
|
|
|
|
|
my @undef_opt = grep { |
|
1743
|
0
|
|
|
|
|
|
my $key = $_; |
|
1744
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1745
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1746
|
0
|
0
|
|
|
|
|
if ( scalar @undef_opt > 0 ) { |
|
1747
|
0
|
|
|
|
|
|
p @undef_opt; |
|
1748
|
0
|
|
|
|
|
|
die "The above arguments aren't defined for $plot->{'plot.type'} using $current_sub"; |
|
1749
|
|
|
|
|
|
|
} |
|
1750
|
0
|
|
0
|
|
|
|
$plot->{orientation} = $plot->{orientation} // 'vertical'; |
|
1751
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} !~ m/^(?:horizontal|vertical)$/ ) { |
|
1752
|
0
|
|
|
|
|
|
die "$current_sub needs either \"horizontal\" or \"vertical\", not \"$plot->{orientation}\""; |
|
1753
|
|
|
|
|
|
|
} |
|
1754
|
0
|
|
0
|
|
|
|
$args->{whiskers} = $args->{whiskers} // 1; # by default, make whiskers |
|
1755
|
0
|
|
|
|
|
|
my ( @xticks, @key_order ); |
|
1756
|
0
|
0
|
|
|
|
|
if ( defined $plot->{'key.order'} ) { |
|
1757
|
0
|
|
|
|
|
|
@key_order = @{ $plot->{'key.order'} }; |
|
|
0
|
|
|
|
|
|
|
|
1758
|
|
|
|
|
|
|
} else { |
|
1759
|
0
|
|
|
|
|
|
@key_order = sort keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
1760
|
|
|
|
|
|
|
} |
|
1761
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
1762
|
0
|
|
0
|
|
|
|
$plot->{medians} = $plot->{medians} // 1; # by default, show median values |
|
1763
|
0
|
|
0
|
|
|
|
$plot->{whiskers} = $plot->{whiskers} // 1; |
|
1764
|
0
|
|
0
|
|
|
|
$plot->{edgecolor} = $plot->{edgecolor} // 'black'; |
|
1765
|
0
|
|
|
|
|
|
my $options = ''; # these args go to the plt.hist call |
|
1766
|
0
|
0
|
|
|
|
|
if ( $plot->{'log'} ) { |
|
1767
|
0
|
|
|
|
|
|
$options .= ', log = True'; |
|
1768
|
|
|
|
|
|
|
} |
|
1769
|
0
|
|
|
|
|
|
say { $args->{fh} } 'd = []'; |
|
|
0
|
|
|
|
|
|
|
|
1770
|
0
|
|
|
|
|
|
my $min_n_points = 'inf'; |
|
1771
|
0
|
|
|
|
|
|
foreach my $key (@key_order) { |
|
1772
|
0
|
0
|
|
|
|
|
@{ $plot->{data}{$key} } = grep { defined && looks_like_number($_) } @{ $plot->{data}{$key} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1773
|
0
|
|
|
|
|
|
say { $args->{fh} } 'd.append([' |
|
1774
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{$key} } ) . '])'; |
|
|
0
|
|
|
|
|
|
|
|
1775
|
0
|
|
|
|
|
|
$min_n_points = min( scalar @{ $plot->{data}{$key} }, $min_n_points ); |
|
|
0
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
} |
|
1777
|
0
|
|
|
|
|
|
foreach my $axis (@{ $plot->{logscale} }) { # x, y |
|
|
0
|
|
|
|
|
|
|
|
1778
|
0
|
0
|
|
|
|
|
if ($axis =~ m/^([^xy])$/) { |
|
1779
|
0
|
|
|
|
|
|
p $plot->{logscale}; |
|
1780
|
0
|
|
|
|
|
|
die "only \"x\" and \"y\" are allowed in boxplot, not \"$axis\""; |
|
1781
|
|
|
|
|
|
|
} |
|
1782
|
0
|
|
|
|
|
|
say {$args->{fh}} "ax$ax.set_$axis" . 'scale("log")'; |
|
|
0
|
|
|
|
|
|
|
|
1783
|
|
|
|
|
|
|
} |
|
1784
|
0
|
|
|
|
|
|
say { $args->{fh} } "vp = ax$ax.violinplot(d, showmeans=False, points = $min_n_points, orientation = '$plot->{orientation}', showmedians = $plot->{medians})"; |
|
|
0
|
|
|
|
|
|
|
|
1785
|
0
|
0
|
|
|
|
|
if ( defined $plot->{colors} ) { # every hash key should have its own color defined |
|
1786
|
|
|
|
|
|
|
# the below code helps to provide better error messages in case I make an error in calling the sub |
|
1787
|
0
|
|
|
|
|
|
my @wrong_keys = grep { not defined $plot->{colors}{$_} } keys %{ $plot->{data} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1788
|
0
|
0
|
|
|
|
|
if ( scalar @wrong_keys > 0 ) { |
|
1789
|
0
|
|
|
|
|
|
p $plot; |
|
1790
|
0
|
|
|
|
|
|
p @wrong_keys; |
|
1791
|
0
|
|
|
|
|
|
die 'the above data keys have no defined color'; |
|
1792
|
|
|
|
|
|
|
} |
|
1793
|
|
|
|
|
|
|
# list of pre-defined colors: https://matplotlib.org/stable/gallery/color/named_colors.html |
|
1794
|
0
|
|
|
|
|
|
say { $args->{fh} } 'colors = ["' |
|
1795
|
0
|
|
|
|
|
|
. join( '","', @{ $plot->{colors} }{@key_order} ) . '"]'; |
|
|
0
|
|
|
|
|
|
|
|
1796
|
|
|
|
|
|
|
|
|
1797
|
|
|
|
|
|
|
# the above color list will have the same order, via the above hash slice |
|
1798
|
0
|
|
|
|
|
|
say { $args->{fh} } 'for i, pc in enumerate(vp["bodies"], 1):'; |
|
|
0
|
|
|
|
|
|
|
|
1799
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_facecolor(colors[i-1])"; |
|
|
0
|
|
|
|
|
|
|
|
1800
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_edgecolor('black')"; |
|
|
0
|
|
|
|
|
|
|
|
1801
|
|
|
|
|
|
|
} else { |
|
1802
|
0
|
|
|
|
|
|
say { $args->{fh} } 'for pc in vp["bodies"]:'; |
|
|
0
|
|
|
|
|
|
|
|
1803
|
0
|
0
|
|
|
|
|
if ( defined $plot->{color} ) { |
|
1804
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_facecolor('$plot->{color}')"; |
|
|
0
|
|
|
|
|
|
|
|
1805
|
|
|
|
|
|
|
} |
|
1806
|
0
|
|
|
|
|
|
say { $args->{fh} } "\tpc.set_edgecolor('black')"; |
|
|
0
|
|
|
|
|
|
|
|
1807
|
|
|
|
|
|
|
|
|
1808
|
|
|
|
|
|
|
# say {$args->{fh}} "\tpc.set_alpha(1)"; |
|
1809
|
|
|
|
|
|
|
} |
|
1810
|
0
|
0
|
|
|
|
|
if ( $plot->{whiskers} ) { |
|
1811
|
|
|
|
|
|
|
# https://matplotlib.org/stable/gallery/statistics/customized_violin.html |
|
1812
|
0
|
|
|
|
|
|
say {$args->{fh} } 'import numpy as np'; |
|
|
0
|
|
|
|
|
|
|
|
1813
|
0
|
|
|
|
|
|
say {$args->{fh} } 'def adjacent_values(vals, q1, q3):'; |
|
|
0
|
|
|
|
|
|
|
|
1814
|
0
|
|
|
|
|
|
say {$args->{fh} } ' upper_adjacent_value = q3 + (q3 - q1) * 1.5'; |
|
|
0
|
|
|
|
|
|
|
|
1815
|
0
|
|
|
|
|
|
say {$args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1816
|
|
|
|
|
|
|
' upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])'; |
|
1817
|
0
|
|
|
|
|
|
say {$args->{fh} } ' lower_adjacent_value = q1 - (q3 - q1) * 1.5'; |
|
|
0
|
|
|
|
|
|
|
|
1818
|
0
|
|
|
|
|
|
say {$args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1819
|
|
|
|
|
|
|
' lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)'; |
|
1820
|
0
|
|
|
|
|
|
say {$args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
' return lower_adjacent_value, upper_adjacent_value'; |
|
1822
|
0
|
|
|
|
|
|
say {$args->{fh} } 'np_data = np.array(d, dtype = object)'; |
|
|
0
|
|
|
|
|
|
|
|
1823
|
0
|
|
|
|
|
|
say {$args->{fh} } 'quartile1 = []'; |
|
|
0
|
|
|
|
|
|
|
|
1824
|
0
|
|
|
|
|
|
say {$args->{fh} } 'medians = []'; |
|
|
0
|
|
|
|
|
|
|
|
1825
|
0
|
|
|
|
|
|
say {$args->{fh} } 'quartile3 = []'; |
|
|
0
|
|
|
|
|
|
|
|
1826
|
0
|
|
|
|
|
|
say {$args->{fh} } 'for subset in list(range(0, len(np_data))):'; |
|
|
0
|
|
|
|
|
|
|
|
1827
|
0
|
|
|
|
|
|
say {$args->{fh}} ' local_quartile1, local_medians, local_quartile3 = np.percentile(d[subset], [25, 50, 75])' . "\n"; |
|
|
0
|
|
|
|
|
|
|
|
1828
|
0
|
|
|
|
|
|
say {$args->{fh}} ' quartile1.append(local_quartile1)'; |
|
|
0
|
|
|
|
|
|
|
|
1829
|
0
|
|
|
|
|
|
say {$args->{fh}} ' medians.append(local_medians)'; |
|
|
0
|
|
|
|
|
|
|
|
1830
|
0
|
|
|
|
|
|
say {$args->{fh}} ' quartile3.append(local_quartile3)'; |
|
|
0
|
|
|
|
|
|
|
|
1831
|
0
|
|
|
|
|
|
say {$args->{fh}} 'whiskers = np.array(['; |
|
|
0
|
|
|
|
|
|
|
|
1832
|
0
|
|
|
|
|
|
say {$args->{fh}} ' adjacent_values(sorted_array, q1, q3)'; |
|
|
0
|
|
|
|
|
|
|
|
1833
|
0
|
|
|
|
|
|
say {$args->{fh}} ' for sorted_array, q1, q3 in zip(d, quartile1, quartile3)])'; |
|
|
0
|
|
|
|
|
|
|
|
1834
|
0
|
|
|
|
|
|
say {$args->{fh}} 'whiskers_min, whiskers_max = whiskers[:, 0], whiskers[:, 1]'; |
|
|
0
|
|
|
|
|
|
|
|
1835
|
0
|
|
|
|
|
|
say {$args->{fh}} 'inds = np.arange(1, len(medians) + 1)'; |
|
|
0
|
|
|
|
|
|
|
|
1836
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} eq 'vertical' ) { |
|
1837
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax" |
|
|
0
|
|
|
|
|
|
|
|
1838
|
|
|
|
|
|
|
. '.vlines(inds, quartile1, quartile3, color="k", linestyle="-", lw=5)'; |
|
1839
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax" |
|
|
0
|
|
|
|
|
|
|
|
1840
|
|
|
|
|
|
|
. '.vlines(inds, whiskers_min, whiskers_max, color="k", linestyle="-", lw=1)'; |
|
1841
|
|
|
|
|
|
|
} else { |
|
1842
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax" |
|
|
0
|
|
|
|
|
|
|
|
1843
|
|
|
|
|
|
|
. '.hlines(inds, quartile1, quartile3, color="k", linestyle="-", lw=5)'; |
|
1844
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax" |
|
|
0
|
|
|
|
|
|
|
|
1845
|
|
|
|
|
|
|
. '.hlines(inds, whiskers_min, whiskers_max, color="k", linestyle="-", lw=1)'; |
|
1846
|
|
|
|
|
|
|
} |
|
1847
|
|
|
|
|
|
|
} |
|
1848
|
0
|
|
|
|
|
|
foreach my $key (@key_order) { |
|
1849
|
|
|
|
|
|
|
push @xticks, "$key (" |
|
1850
|
0
|
|
|
|
|
|
. format_commas( scalar @{ $plot->{data}{$key} }, '%.0u' ) . ')'; |
|
|
0
|
|
|
|
|
|
|
|
1851
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} eq 'vertical' ) { |
|
1852
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(" |
|
1853
|
|
|
|
|
|
|
. scalar @xticks . ', ' |
|
1854
|
0
|
|
|
|
|
|
. ( sum( @{ $plot->{data}{$key} } ) / |
|
1855
|
0
|
|
|
|
|
|
scalar @{ $plot->{data}{$key} } ) |
|
|
0
|
|
|
|
|
|
|
|
1856
|
|
|
|
|
|
|
. ', "ro")'; # plot mean point, which is red |
|
1857
|
|
|
|
|
|
|
} else { # orientation = horizontal |
|
1858
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(" |
|
1859
|
0
|
|
|
|
|
|
. ( sum( @{ $plot->{data}{$key} } ) / |
|
1860
|
0
|
|
|
|
|
|
scalar @{ $plot->{data}{$key} } ) |
|
|
0
|
|
|
|
|
|
|
|
1861
|
|
|
|
|
|
|
. ', ' |
|
1862
|
|
|
|
|
|
|
. scalar @xticks |
|
1863
|
|
|
|
|
|
|
. ', "ro")'; # plot mean point, which is red |
|
1864
|
|
|
|
|
|
|
} |
|
1865
|
|
|
|
|
|
|
} |
|
1866
|
0
|
0
|
|
|
|
|
if ( $plot->{orientation} eq 'vertical' ) { |
|
1867
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.set_xticks([" |
|
|
0
|
|
|
|
|
|
|
|
1868
|
|
|
|
|
|
|
. join( ',', 1 .. scalar @key_order ) . '], ["' |
|
1869
|
|
|
|
|
|
|
. join( '","', @xticks ) . '"])'; |
|
1870
|
|
|
|
|
|
|
} else { |
|
1871
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.set_yticks([" |
|
|
0
|
|
|
|
|
|
|
|
1872
|
|
|
|
|
|
|
. join( ',', 1 .. scalar @key_order ) . '], ["' |
|
1873
|
|
|
|
|
|
|
. join( '","', @xticks ) . '"])'; |
|
1874
|
|
|
|
|
|
|
} |
|
1875
|
|
|
|
|
|
|
} |
|
1876
|
|
|
|
|
|
|
|
|
1877
|
|
|
|
|
|
|
sub wide_helper { |
|
1878
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1879
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1880
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1881
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1882
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1883
|
|
|
|
|
|
|
} |
|
1884
|
0
|
|
|
|
|
|
my @reqd_args = ( |
|
1885
|
|
|
|
|
|
|
'fh', # e.g. $py, $fh, which will be passed by the subroutine |
|
1886
|
|
|
|
|
|
|
'plot', # args to original function |
|
1887
|
|
|
|
|
|
|
); |
|
1888
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
1889
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
1890
|
0
|
|
|
|
|
|
p @undef_args; |
|
1891
|
0
|
|
|
|
|
|
die "the above args are necessary for $current_sub, but were not defined."; |
|
1892
|
|
|
|
|
|
|
} |
|
1893
|
0
|
|
|
|
|
|
my @opt = (@ax_methods, @plt_methods, @fig_methods, @arg, 'ax', @{ $opt{$current_sub} }); |
|
|
0
|
|
|
|
|
|
|
|
1894
|
0
|
|
|
|
|
|
my $plot = $args->{plot}; |
|
1895
|
0
|
|
0
|
|
|
|
$plot->{'show.legend'} = $plot->{'show.legend'} // 1; |
|
1896
|
|
|
|
|
|
|
my @undef_opt = grep { |
|
1897
|
0
|
|
|
|
|
|
my $key = $_; |
|
1898
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @opt |
|
|
0
|
|
|
|
|
|
|
|
1899
|
0
|
|
|
|
|
|
} keys %{$plot}; |
|
|
0
|
|
|
|
|
|
|
|
1900
|
0
|
0
|
|
|
|
|
if ( scalar @undef_opt > 0 ) { |
|
1901
|
0
|
|
|
|
|
|
p @undef_opt; |
|
1902
|
0
|
|
|
|
|
|
die |
|
1903
|
|
|
|
|
|
|
"The above arguments aren't defined for $plot->{'plot.type'} using $current_sub"; |
|
1904
|
|
|
|
|
|
|
} |
|
1905
|
0
|
|
|
|
|
|
say { $args->{fh} } 'import numpy as np'; |
|
|
0
|
|
|
|
|
|
|
|
1906
|
0
|
|
0
|
|
|
|
my $ax = $args->{ax} // ''; |
|
1907
|
0
|
|
|
|
|
|
my $ref_type = ref $plot->{data}; |
|
1908
|
0
|
0
|
|
|
|
|
if ( $ref_type eq 'HASH' ) { # multiple groups, no label |
|
|
|
0
|
|
|
|
|
|
|
1909
|
0
|
|
|
|
|
|
foreach my $group ( keys %{ $plot->{data} } ) { |
|
|
0
|
|
|
|
|
|
|
|
1910
|
0
|
|
0
|
|
|
|
my $color = $plot->{color}{$group} // 'b'; |
|
1911
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys = []'; |
|
|
0
|
|
|
|
|
|
|
|
1912
|
0
|
|
|
|
|
|
my ( $min_x, $max_x ) = ( 'inf', '-inf' ); |
|
1913
|
0
|
|
|
|
|
|
foreach my $run ( 0 .. scalar @{ $plot->{data}{$group} } - 1 ) { |
|
|
0
|
|
|
|
|
|
|
|
1914
|
0
|
|
|
|
|
|
$min_x = min( $min_x, @{ $plot->{data}{$group}[$run][0] } ); |
|
|
0
|
|
|
|
|
|
|
|
1915
|
0
|
|
|
|
|
|
$max_x = max( $max_x, @{ $plot->{data}{$group}[$run][0] } ); |
|
|
0
|
|
|
|
|
|
|
|
1916
|
|
|
|
|
|
|
} |
|
1917
|
0
|
|
|
|
|
|
say { $args->{fh} } "base_y = np.linspace($max_x, $min_x, 101)"; |
|
|
0
|
|
|
|
|
|
|
|
1918
|
0
|
|
|
|
|
|
foreach my $run ( 0 .. scalar @{ $plot->{data}{$group} } - 1 ) { |
|
|
0
|
|
|
|
|
|
|
|
1919
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' |
|
1920
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{$group}[$run][0] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1921
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' |
|
1922
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}{$group}[$run][1] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1923
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(x, y, '$color', alpha=0.15)"; |
|
|
0
|
|
|
|
|
|
|
|
1924
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = np.interp(base_y, x, y)'; |
|
|
0
|
|
|
|
|
|
|
|
1925
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys.append(y)'; |
|
|
0
|
|
|
|
|
|
|
|
1926
|
|
|
|
|
|
|
} |
|
1927
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys = np.array(ys)'; |
|
|
0
|
|
|
|
|
|
|
|
1928
|
0
|
|
|
|
|
|
say { $args->{fh} } 'mean_ys = ys.mean(axis=0)'; |
|
|
0
|
|
|
|
|
|
|
|
1929
|
0
|
|
|
|
|
|
say { $args->{fh} } 'std = ys.std(axis=0)'; |
|
|
0
|
|
|
|
|
|
|
|
1930
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys_upper = np.minimum(mean_ys + std, 1)'; |
|
|
0
|
|
|
|
|
|
|
|
1931
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys_lower = mean_ys - std'; |
|
|
0
|
|
|
|
|
|
|
|
1932
|
0
|
0
|
|
|
|
|
if ( $plot->{'show.legend'} > 0 ) { |
|
1933
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(base_y, mean_ys, '$color', label = '$group')"; |
|
|
0
|
|
|
|
|
|
|
|
1934
|
|
|
|
|
|
|
} else { |
|
1935
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(base_y, mean_ys, '$color')"; |
|
|
0
|
|
|
|
|
|
|
|
1936
|
|
|
|
|
|
|
} |
|
1937
|
0
|
|
|
|
|
|
say { $args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1938
|
|
|
|
|
|
|
"ax$ax.fill_between(base_y, ys_lower, ys_upper, color='$color', alpha=0.3)"; |
|
1939
|
|
|
|
|
|
|
} |
|
1940
|
|
|
|
|
|
|
} elsif ( $ref_type eq 'ARRAY' ) { |
|
1941
|
0
|
|
0
|
|
|
|
my $color = $plot->{color} // 'b'; |
|
1942
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys = []'; |
|
|
0
|
|
|
|
|
|
|
|
1943
|
0
|
|
|
|
|
|
my ( $min_x, $max_x ) = ( 'inf', '-inf' ); |
|
1944
|
0
|
|
|
|
|
|
foreach my $run ( 0 .. scalar @{ $plot->{data} } - 1 ) { |
|
|
0
|
|
|
|
|
|
|
|
1945
|
0
|
|
|
|
|
|
$min_x = min( $min_x, @{ $plot->{data}[$run][0] } ); |
|
|
0
|
|
|
|
|
|
|
|
1946
|
0
|
|
|
|
|
|
$max_x = max( $max_x, @{ $plot->{data}[$run][0] } ); |
|
|
0
|
|
|
|
|
|
|
|
1947
|
|
|
|
|
|
|
} |
|
1948
|
0
|
|
|
|
|
|
say { $args->{fh} } "base_y = np.linspace($max_x, $min_x, 101)"; |
|
|
0
|
|
|
|
|
|
|
|
1949
|
0
|
|
|
|
|
|
foreach my $run ( 0 .. scalar @{ $plot->{data} } - 1 ) { |
|
|
0
|
|
|
|
|
|
|
|
1950
|
0
|
|
|
|
|
|
say { $args->{fh} } 'x = [' |
|
1951
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}[$run][0] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1952
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = [' |
|
1953
|
0
|
|
|
|
|
|
. join( ',', @{ $plot->{data}[$run][1] } ) . ']'; |
|
|
0
|
|
|
|
|
|
|
|
1954
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(x, y, '$color', alpha=0.15)"; |
|
|
0
|
|
|
|
|
|
|
|
1955
|
0
|
|
|
|
|
|
say { $args->{fh} } 'y = np.interp(base_y, x, y)'; |
|
|
0
|
|
|
|
|
|
|
|
1956
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys.append(y)'; |
|
|
0
|
|
|
|
|
|
|
|
1957
|
|
|
|
|
|
|
} |
|
1958
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys = np.array(ys)'; |
|
|
0
|
|
|
|
|
|
|
|
1959
|
0
|
|
|
|
|
|
say { $args->{fh} } 'mean_ys = ys.mean(axis=0)'; |
|
|
0
|
|
|
|
|
|
|
|
1960
|
0
|
|
|
|
|
|
say { $args->{fh} } 'std = ys.std(axis=0)'; |
|
|
0
|
|
|
|
|
|
|
|
1961
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys_upper = np.minimum(mean_ys + std, 1)'; |
|
|
0
|
|
|
|
|
|
|
|
1962
|
0
|
|
|
|
|
|
say { $args->{fh} } 'ys_lower = mean_ys - std'; |
|
|
0
|
|
|
|
|
|
|
|
1963
|
0
|
|
|
|
|
|
say { $args->{fh} } "ax$ax.plot(base_y, mean_ys, '$color')"; |
|
|
0
|
|
|
|
|
|
|
|
1964
|
0
|
|
|
|
|
|
say { $args->{fh} } |
|
|
0
|
|
|
|
|
|
|
|
1965
|
|
|
|
|
|
|
"ax$ax.fill_between(base_y, ys_lower, ys_upper, color='$color', alpha=0.3)"; |
|
1966
|
|
|
|
|
|
|
} else { |
|
1967
|
0
|
|
|
|
|
|
die "$current_sub cannot take ref type \"$ref_type\" for \"data\""; |
|
1968
|
|
|
|
|
|
|
} |
|
1969
|
|
|
|
|
|
|
} |
|
1970
|
|
|
|
|
|
|
|
|
1971
|
|
|
|
|
|
|
sub print_type { |
|
1972
|
0
|
|
|
0
|
0
|
|
my $str = shift; |
|
1973
|
0
|
|
|
|
|
|
my $type = 'no quotes'; |
|
1974
|
0
|
0
|
|
|
|
|
if ($str =~ m/^\w+\h*=\h*["']/) { |
|
1975
|
0
|
|
|
|
|
|
return 'no quotes'; |
|
1976
|
|
|
|
|
|
|
} |
|
1977
|
0
|
0
|
0
|
|
|
|
if ($str =~ m/^\w+$/) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1978
|
0
|
|
|
|
|
|
return 'single quotes'; |
|
1979
|
|
|
|
|
|
|
} elsif ($str =~ m/[!@#\$\%^&*\(\)\{\}\[\]\<\>,\/\-\h:;\+=\w]+$/) { |
|
1980
|
0
|
|
|
|
|
|
return 'single quotes'; |
|
1981
|
|
|
|
|
|
|
} elsif (($str =~ m/,/) && ($str !~ m/[\]\[]/)) { |
|
1982
|
0
|
|
|
|
|
|
return 'single quotes'; |
|
1983
|
|
|
|
|
|
|
} |
|
1984
|
0
|
|
|
|
|
|
return $type; |
|
1985
|
|
|
|
|
|
|
} |
|
1986
|
|
|
|
|
|
|
my @all_opt; |
|
1987
|
|
|
|
|
|
|
foreach my $type (keys %opt) { |
|
1988
|
|
|
|
|
|
|
push @all_opt, @{ $opt{$type} }; |
|
1989
|
|
|
|
|
|
|
} |
|
1990
|
|
|
|
|
|
|
sub plt { |
|
1991
|
0
|
|
|
0
|
0
|
|
my ($args) = @_; |
|
1992
|
0
|
|
|
|
|
|
my $current_sub = ( split( /::/, ( caller(0) )[3] ) )[-1] |
|
1993
|
|
|
|
|
|
|
; # https://stackoverflow.com/questions/2559792/how-can-i-get-the-name-of-the-current-subroutine-in-perl |
|
1994
|
0
|
0
|
|
|
|
|
if ( ref $args ne 'HASH' ) { |
|
1995
|
0
|
|
|
|
|
|
die "args must be given as a hash ref, e.g. \"$current_sub({ data => \@blah })\""; |
|
1996
|
|
|
|
|
|
|
} |
|
1997
|
0
|
0
|
|
|
|
|
if ((scalar grep {$args->{$_}} ('output.file', 'show')) == 0) { |
|
|
0
|
|
|
|
|
|
|
|
1998
|
0
|
|
|
|
|
|
p $args; |
|
1999
|
0
|
|
|
|
|
|
die 'either "show" or "output.file" must be defined'; |
|
2000
|
|
|
|
|
|
|
} |
|
2001
|
0
|
|
|
|
|
|
my @reqd_args = ('output.file'); # e.g. "my_image.svg" |
|
2002
|
0
|
|
|
|
|
|
my $single_example = 'plt({ |
|
2003
|
|
|
|
|
|
|
\'output.file\' => \'/tmp/gospel.word.counts.svg\', |
|
2004
|
|
|
|
|
|
|
\'plot.type\' => \'bar\', |
|
2005
|
|
|
|
|
|
|
data => { |
|
2006
|
|
|
|
|
|
|
\'Matthew\' => 18345, |
|
2007
|
|
|
|
|
|
|
\'Mark\' => 11304, |
|
2008
|
|
|
|
|
|
|
\'Luke\' => 19482, |
|
2009
|
|
|
|
|
|
|
\'John\' => 15635, |
|
2010
|
|
|
|
|
|
|
} |
|
2011
|
|
|
|
|
|
|
});'; |
|
2012
|
0
|
|
|
|
|
|
my $multi_example = 'plt({ |
|
2013
|
|
|
|
|
|
|
\'output.file\' => \'svg/pie.svg\', |
|
2014
|
|
|
|
|
|
|
plots => [ |
|
2015
|
|
|
|
|
|
|
{ |
|
2016
|
|
|
|
|
|
|
data => { |
|
2017
|
|
|
|
|
|
|
Russian => 106_000_000, # Primarily European Russia |
|
2018
|
|
|
|
|
|
|
German => 95_000_000, # Germany, Austria, Switzerland, etc. |
|
2019
|
|
|
|
|
|
|
}, |
|
2020
|
|
|
|
|
|
|
\'plot.type\' => \'pie\', |
|
2021
|
|
|
|
|
|
|
title => \'Top Languages in Europe\', |
|
2022
|
|
|
|
|
|
|
suptitle => \'Pie in subplots\', |
|
2023
|
|
|
|
|
|
|
}, |
|
2024
|
|
|
|
|
|
|
{ |
|
2025
|
|
|
|
|
|
|
data => { |
|
2026
|
|
|
|
|
|
|
Russian => 106_000_000, # Primarily European Russia |
|
2027
|
|
|
|
|
|
|
German => 95_000_000, # Germany, Austria, Switzerland, etc. |
|
2028
|
|
|
|
|
|
|
}, |
|
2029
|
|
|
|
|
|
|
\'plot.type\' => \'pie\', |
|
2030
|
|
|
|
|
|
|
title => \'Top Languages in Europe\', |
|
2031
|
|
|
|
|
|
|
}, |
|
2032
|
|
|
|
|
|
|
ncols => 3, |
|
2033
|
|
|
|
|
|
|
});'; |
|
2034
|
0
|
|
|
|
|
|
my @undef_args = grep { !defined $args->{$_} } @reqd_args; |
|
|
0
|
|
|
|
|
|
|
|
2035
|
0
|
0
|
|
|
|
|
if ( scalar @undef_args > 0 ) { |
|
2036
|
0
|
|
|
|
|
|
p @undef_args; |
|
2037
|
0
|
|
|
|
|
|
die 'the above args are necessary, but were not defined.'; |
|
2038
|
|
|
|
|
|
|
} |
|
2039
|
0
|
0
|
0
|
|
|
|
if ( ( not defined $args->{'plot.type'} ) |
|
2040
|
|
|
|
|
|
|
&& ( not defined $args->{plots} ) ) |
|
2041
|
|
|
|
|
|
|
{ |
|
2042
|
0
|
|
|
|
|
|
p $args; |
|
2043
|
0
|
|
|
|
|
|
die 'either "plot.type" or "plots" must be defined, but neither were'; |
|
2044
|
|
|
|
|
|
|
} |
|
2045
|
0
|
0
|
|
|
|
|
if (ref $args->{'output.file'} ne '') { |
|
2046
|
0
|
|
|
|
|
|
p $args; |
|
2047
|
0
|
|
|
|
|
|
die '"output.file" must be a SCALAR or string, but was given a ' . ref $args->{'output.file'}; |
|
2048
|
|
|
|
|
|
|
} |
|
2049
|
0
|
|
|
|
|
|
my @defined_args = (@reqd_args, @ax_methods, @fig_methods, @plt_methods, @arg, @all_opt); |
|
2050
|
|
|
|
|
|
|
my @bad_args = grep { |
|
2051
|
0
|
|
|
|
|
|
my $key = $_; |
|
2052
|
0
|
|
|
|
|
|
not grep { $_ eq $key } @defined_args |
|
|
0
|
|
|
|
|
|
|
|
2053
|
0
|
|
|
|
|
|
} keys %{$args}; |
|
|
0
|
|
|
|
|
|
|
|
2054
|
0
|
0
|
|
|
|
|
if ( scalar @bad_args > 0 ) { |
|
2055
|
0
|
|
|
|
|
|
p @defined_args, array_max => scalar @defined_args; |
|
2056
|
0
|
|
|
|
|
|
p @bad_args, array_max => scalar @bad_args; |
|
2057
|
0
|
|
|
|
|
|
say STDERR 'the 2nd group of arguments are not recognized, while the 1st is the defined list'; |
|
2058
|
0
|
|
|
|
|
|
die "The above args are accepted by \"$current_sub\""; |
|
2059
|
|
|
|
|
|
|
} |
|
2060
|
0
|
|
|
|
|
|
my $single_plot = 0; # false |
|
2061
|
0
|
0
|
0
|
|
|
|
if ( ( defined $args->{'plot.type'} ) && ( defined $args->{data} ) ) { |
|
2062
|
0
|
|
|
|
|
|
$single_plot = 1; # true |
|
2063
|
|
|
|
|
|
|
} |
|
2064
|
0
|
0
|
0
|
|
|
|
if (($single_plot == 1) && (not defined $args->{'plot.type'})) { |
|
2065
|
0
|
|
|
|
|
|
p $args; |
|
2066
|
0
|
|
|
|
|
|
say $single_example; |
|
2067
|
0
|
|
|
|
|
|
die "\"plot.type\" was not defined for a single plot in $current_sub"; |
|
2068
|
|
|
|
|
|
|
} |
|
2069
|
0
|
0
|
0
|
|
|
|
if ( ( $single_plot == 0 ) && (not defined $args->{plots} )) { |
|
2070
|
0
|
|
|
|
|
|
say $multi_example; |
|
2071
|
0
|
|
|
|
|
|
die "$current_sub: single plots need \"data\" and \"plot.type\", see example above"; |
|
2072
|
|
|
|
|
|
|
} |
|
2073
|
0
|
0
|
0
|
|
|
|
if ( ( $single_plot == 0 ) && ( ref $args->{plots} ne 'ARRAY' ) ) { |
|
2074
|
0
|
|
|
|
|
|
p $args; |
|
2075
|
0
|
|
|
|
|
|
die "$current_sub \"plots\" must have an array entered into it"; |
|
2076
|
|
|
|
|
|
|
} |
|
2077
|
0
|
0
|
0
|
|
|
|
if ( ( $single_plot == 0 ) && ( scalar @{ $args->{plots} } == 0 ) ) { |
|
|
0
|
|
|
|
|
|
|
|
2078
|
0
|
|
|
|
|
|
p $args; |
|
2079
|
0
|
|
|
|
|
|
die "$current_sub \"plots\" has 0 plots entered."; |
|
2080
|
|
|
|
|
|
|
} |
|
2081
|
0
|
0
|
|
|
|
|
if ($single_plot == 1) { |
|
2082
|
0
|
0
|
|
|
|
|
foreach my $arg (grep {defined $args->{$_} && $args->{$_} > 1} ('ncols', 'nrows')) { |
|
|
0
|
|
|
|
|
|
|
|
2083
|
0
|
|
|
|
|
|
warn "\"$arg\" is set to >1, but there is only 1 plot: resetting $arg to 1."; |
|
2084
|
0
|
|
|
|
|
|
$args->{$arg} = 1; |
|
2085
|
|
|
|
|
|
|
} |
|
2086
|
|
|
|
|
|
|
} |
|
2087
|
0
|
|
0
|
|
|
|
$args->{nrows} = $args->{nrows} // 1; |
|
2088
|
0
|
|
0
|
|
|
|
$args->{ncols} = $args->{ncols} // 1; |
|
2089
|
0
|
0
|
0
|
|
|
|
if ( ( $single_plot == 0 ) |
|
2090
|
0
|
|
|
|
|
|
&& ( ( $args->{nrows} * $args->{ncols} ) < scalar @{ $args->{plots} } ) |
|
2091
|
|
|
|
|
|
|
) |
|
2092
|
|
|
|
|
|
|
{ |
|
2093
|
0
|
|
|
|
|
|
p $args; |
|
2094
|
0
|
|
|
|
|
|
my $n_plots = scalar @{ $args->{plots} }; |
|
|
0
|
|
|
|
|
|
|
|
2095
|
0
|
|
|
|
|
|
say |
|
2096
|
|
|
|
|
|
|
"ncols = $args->{ncols}; nrows = $args->{nrows}, but there are $n_plots plots.\n"; |
|
2097
|
0
|
|
|
|
|
|
die 'There are not enough subplots for the data'; |
|
2098
|
|
|
|
|
|
|
} |
|
2099
|
0
|
0
|
|
|
|
|
if ($single_plot == 0) { # multiple plots |
|
2100
|
0
|
|
|
|
|
|
my $max_i = scalar @{ $args->{plots} } - 1; |
|
|
0
|
|
|
|
|
|
|
|
2101
|
0
|
|
|
|
|
|
my @hash_ref_i = grep { ref $args->{plots}[$_]{data} eq 'HASH' } 0..$max_i; |
|
|
0
|
|
|
|
|
|
|
|
2102
|
0
|
|
|
|
|
|
@bad_args = grep { scalar keys %{ $args->{plots}[$_]{data} } == 0} @hash_ref_i; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
2103
|
0
|
0
|
|
|
|
|
if (scalar @bad_args > 0) { |
|
2104
|
0
|
|
|
|
|
|
foreach my $i (@bad_args) { |
|
2105
|
0
|
|
|
|
|
|
say STDERR "plot index $i:"; |
|
2106
|
0
|
|
|
|
|
|
p $args->{plots}[$i]; |
|
2107
|
|
|
|
|
|
|
} |
|
2108
|
0
|
|
|
|
|
|
die "the above hash ref indices have empty data hashes for $current_sub"; |
|
2109
|
|
|
|
|
|
|
} |
|
2110
|
0
|
|
|
|
|
|
my @output_file = grep {defined $args->{plots}[$_]{'output.file'}} 0..$max_i; |
|
|
0
|
|
|
|
|
|
|
|
2111
|
0
|
0
|
|
|
|
|
if (scalar @output_file > 0) { |
|
2112
|
0
|
|
|
|
|
|
p $args; |
|
2113
|
0
|
|
|
|
|
|
p @output_file; |
|
2114
|
0
|
|
|
|
|
|
die '"output.file" was defined at subplots indices above, which does not make sense'; |
|
2115
|
|
|
|
|
|
|
} |
|
2116
|
|
|
|
|
|
|
} |
|
2117
|
0
|
0
|
0
|
|
|
|
if (($single_plot == 1) && (ref $args->{data} eq 'HASH') && (scalar keys %{ $args->{data}} == 0 )) { |
|
|
0
|
|
0
|
|
|
|
|
|
2118
|
0
|
|
|
|
|
|
p $args; |
|
2119
|
0
|
|
|
|
|
|
die '"data" is an empty hash'; |
|
2120
|
|
|
|
|
|
|
} |
|
2121
|
0
|
0
|
|
|
|
|
@bad_args = grep {defined $args->{$_} && (not looks_like_number($args->{$_}))} ('cbpad', 'ncols', 'nrows', 'scale', 'scalex', 'scaley'); |
|
|
0
|
|
|
|
|
|
|
|
2122
|
0
|
0
|
|
|
|
|
if (scalar @bad_args > 0) { |
|
2123
|
0
|
|
|
|
|
|
p $args; |
|
2124
|
0
|
|
|
|
|
|
p @bad_args; |
|
2125
|
0
|
|
|
|
|
|
die 'the above args must be numeric'; |
|
2126
|
|
|
|
|
|
|
} |
|
2127
|
0
|
|
|
|
|
|
my @ax = map { "ax$_" } 0 .. $args->{nrows} * $args->{ncols} - 1; |
|
|
0
|
|
|
|
|
|
|
|
2128
|
0
|
|
|
|
|
|
my ( @py, @y, $fh); |
|
2129
|
0
|
|
|
|
|
|
my $i = 0; |
|
2130
|
0
|
|
|
|
|
|
foreach my $ax (@ax) { |
|
2131
|
0
|
|
|
|
|
|
my $a1i = int $i / $args->{ncols}; # 1st index |
|
2132
|
0
|
|
|
|
|
|
my $a2i = $i % $args->{ncols}; # 2nd index |
|
2133
|
0
|
|
|
|
|
|
$y[$a1i][$a2i] = $ax; |
|
2134
|
0
|
|
|
|
|
|
$i++; |
|
2135
|
|
|
|
|
|
|
} |
|
2136
|
0
|
|
|
|
|
|
foreach my $y (@y) { |
|
2137
|
0
|
|
|
|
|
|
push @py, '(' . join( ',', @{$y} ) . ')'; |
|
|
0
|
|
|
|
|
|
|
|
2138
|
|
|
|
|
|
|
} |
|
2139
|
0
|
0
|
0
|
|
|
|
if ((defined $args->{'shared.colorbar'}) && ($single_plot == 1)) { |
|
2140
|
0
|
|
|
|
|
|
warn 'There is only 1 plot/subplot, shared colorbars make no sense... deleting'; |
|
2141
|
0
|
|
|
|
|
|
delete $args->{'shared.colorbar'}; |
|
2142
|
|
|
|
|
|
|
} |
|
2143
|
0
|
0
|
|
|
|
|
if (defined $args->{'shared.colorbar'}) { |
|
2144
|
0
|
|
|
|
|
|
my $ref = ref $args->{'shared.colorbar'}; |
|
2145
|
0
|
0
|
|
|
|
|
if ($ref ne 'ARRAY') { |
|
2146
|
0
|
|
|
|
|
|
p $args; |
|
2147
|
0
|
|
|
|
|
|
die '"shared.colobar" must be an array reference'; |
|
2148
|
|
|
|
|
|
|
} |
|
2149
|
0
|
|
|
|
|
|
my $max_subplot_idx = max(@{ $args->{'shared.colorbar'} }); |
|
|
0
|
|
|
|
|
|
|
|
2150
|
0
|
0
|
|
|
|
|
if ($max_subplot_idx > ($args->{nrows} * $args->{ncols} - 1)) { |
|
2151
|
0
|
|
|
|
|
|
p $args; |
|
2152
|
0
|
|
|
|
|
|
die "the max \"shared.colorbar\" index $max_subplot_idx > than the max index of plots"; |
|
2153
|
|
|
|
|
|
|
} |
|
2154
|
|
|
|
|
|
|
} |
|
2155
|
0
|
0
|
|
|
|
|
if (defined $args->{add}) { |
|
2156
|
0
|
|
|
|
|
|
my $ref = ref $args->{add}; |
|
2157
|
0
|
0
|
|
|
|
|
if ($ref ne 'ARRAY') { |
|
2158
|
0
|
|
|
|
|
|
die "\"add\" must be an array (of anonymous hashes), but you entered a $ref reference"; |
|
2159
|
|
|
|
|
|
|
} |
|
2160
|
|
|
|
|
|
|
} |
|
2161
|
0
|
0
|
|
|
|
|
if ( defined $args->{fh} ) { |
|
2162
|
0
|
|
|
|
|
|
my $ref = ref $args->{fh}; |
|
2163
|
0
|
0
|
|
|
|
|
if ($ref ne 'File::Temp') { |
|
2164
|
0
|
|
|
|
|
|
p $args; |
|
2165
|
0
|
|
|
|
|
|
die "$current_sub received a \"$ref\" for \"fh\", which isn't a \"File::Temp\" object."; |
|
2166
|
|
|
|
|
|
|
} |
|
2167
|
0
|
|
|
|
|
|
$fh = $args->{fh};# open $fh, '>>', $args->{fh}; |
|
2168
|
|
|
|
|
|
|
} else { |
|
2169
|
0
|
|
|
|
|
|
$fh = File::Temp->new(DIR => '/tmp', SUFFIX => '.py', UNLINK => 0); |
|
2170
|
|
|
|
|
|
|
} |
|
2171
|
0
|
|
|
|
|
|
say 'temp file is ' . $fh->filename; |
|
2172
|
0
|
|
|
|
|
|
say $fh 'import matplotlib.pyplot as plt'; |
|
2173
|
0
|
|
|
|
|
|
say $fh 'import json, base64'; |
|
2174
|
0
|
0
|
|
|
|
|
if ( $single_plot == 0 ) { |
|
|
|
0
|
|
|
|
|
|
|
2175
|
0
|
|
0
|
|
|
|
$args->{sharex} = $args->{sharex} // 0; |
|
2176
|
0
|
|
0
|
|
|
|
$args->{sharey} = $args->{sharey} // 0; |
|
2177
|
0
|
|
|
|
|
|
say $fh 'fig, (' |
|
2178
|
|
|
|
|
|
|
. join( ',', @py ) |
|
2179
|
|
|
|
|
|
|
. ") = plt.subplots($args->{nrows}, $args->{ncols}, sharex = $args->{sharex}, sharey = $args->{sharey}, layout = 'constrained') #" . __LINE__; |
|
2180
|
|
|
|
|
|
|
} elsif ( $single_plot == 1 ) { |
|
2181
|
0
|
|
|
|
|
|
say $fh 'fig, ax0 = plt.subplots(1,1, layout = "constrained")'; |
|
2182
|
|
|
|
|
|
|
} else { |
|
2183
|
0
|
|
|
|
|
|
die "\$single_plot = $single_plot breaks pigeonholes"; |
|
2184
|
|
|
|
|
|
|
} |
|
2185
|
0
|
0
|
|
|
|
|
if ( defined $args->{plots} ) { |
|
2186
|
0
|
|
|
|
|
|
my @undef_plot_types; |
|
2187
|
0
|
|
|
|
|
|
my $j = 0; |
|
2188
|
0
|
|
|
|
|
|
foreach my $plot (@{ $args->{plots} }) { |
|
|
0
|
|
|
|
|
|
|
|
2189
|
0
|
0
|
|
|
|
|
next if defined $plot->{'plot.type'}; |
|
2190
|
0
|
|
|
|
|
|
push @undef_plot_types, $j; |
|
2191
|
0
|
|
|
|
|
|
$j++; |
|
2192
|
|
|
|
|
|
|
} |
|
2193
|
0
|
0
|
|
|
|
|
if ( scalar @undef_plot_types > 0 ) { |
|
2194
|
0
|
|
|
|
|
|
p $args; |
|
2195
|
0
|
|
|
|
|
|
p @undef_plot_types; |
|
2196
|
0
|
|
|
|
|
|
die 'The above subplot indices are missing "plot.type"'; |
|
2197
|
|
|
|
|
|
|
} |
|
2198
|
|
|
|
|
|
|
} |
|
2199
|
0
|
|
|
|
|
|
my %dispatch = ( |
|
2200
|
|
|
|
|
|
|
bar => \&barplot_helper, barh => \&barplot_helper, |
|
2201
|
|
|
|
|
|
|
boxplot => \&boxplot_helper, colored_table=> \&colored_table_helper, |
|
2202
|
|
|
|
|
|
|
hexbin => \&hexbin_helper, hist => \&hist_helper, |
|
2203
|
|
|
|
|
|
|
hist2d => \&hist2d_helper, imshow => \&imshow_helper, |
|
2204
|
|
|
|
|
|
|
pie => \&pie_helper, plot => \&plot_helper, |
|
2205
|
|
|
|
|
|
|
scatter => \&scatter_helper, violin => \&violin_helper, |
|
2206
|
|
|
|
|
|
|
violinplot => \&violin_helper, |
|
2207
|
|
|
|
|
|
|
wide => \&wide_helper |
|
2208
|
|
|
|
|
|
|
); |
|
2209
|
0
|
0
|
|
|
|
|
if ($single_plot == 1) { |
|
2210
|
0
|
|
|
|
|
|
foreach my $graph (@{ $args->{add} }) { |
|
|
0
|
|
|
|
|
|
|
|
2211
|
0
|
|
0
|
|
|
|
my $type = $graph->{'plot.type'} // $args->{'plot.type'}; |
|
2212
|
0
|
0
|
|
|
|
|
die 'plot.type not defined' unless defined $graph->{'plot.type'}; |
|
2213
|
0
|
|
|
|
|
|
$dispatch{$type}->({ |
|
2214
|
|
|
|
|
|
|
fh => $fh, |
|
2215
|
|
|
|
|
|
|
ax => 0, |
|
2216
|
|
|
|
|
|
|
plot => $graph |
|
2217
|
|
|
|
|
|
|
}); |
|
2218
|
|
|
|
|
|
|
} |
|
2219
|
0
|
|
|
|
|
|
delete $args->{add}; |
|
2220
|
0
|
|
|
|
|
|
my $type = $args->{'plot.type'}; |
|
2221
|
0
|
0
|
|
|
|
|
unless (defined $dispatch{$type}) { |
|
2222
|
0
|
|
|
|
|
|
p $args; |
|
2223
|
0
|
|
|
|
|
|
die "$type isn't defined"; |
|
2224
|
|
|
|
|
|
|
} |
|
2225
|
0
|
0
|
|
|
|
|
die 'plot.type not defined' unless defined $dispatch{$type}; |
|
2226
|
0
|
|
|
|
|
|
$dispatch{$type}->({ |
|
2227
|
|
|
|
|
|
|
fh => $fh, |
|
2228
|
|
|
|
|
|
|
ax => 0, |
|
2229
|
|
|
|
|
|
|
plot => $args |
|
2230
|
|
|
|
|
|
|
}); |
|
2231
|
0
|
|
|
|
|
|
my %rename = ( |
|
2232
|
|
|
|
|
|
|
xlabel => 'set_xlabel', |
|
2233
|
|
|
|
|
|
|
title => 'set_title', |
|
2234
|
|
|
|
|
|
|
ylabel => 'set_ylabel', |
|
2235
|
|
|
|
|
|
|
legend => 'legend', |
|
2236
|
|
|
|
|
|
|
xlim => 'set_xlim', |
|
2237
|
|
|
|
|
|
|
); |
|
2238
|
0
|
|
|
|
|
|
foreach my $opt ( grep { defined $rename{$_} } keys %{$args} ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
2239
|
0
|
|
|
|
|
|
$args->{ $rename{$opt} } = delete $args->{$opt}; |
|
2240
|
|
|
|
|
|
|
} |
|
2241
|
|
|
|
|
|
|
plot_args({ |
|
2242
|
0
|
|
|
|
|
|
fh => $fh, |
|
2243
|
|
|
|
|
|
|
args => $args, |
|
2244
|
|
|
|
|
|
|
ax => 'ax0' |
|
2245
|
|
|
|
|
|
|
}); |
|
2246
|
|
|
|
|
|
|
} |
|
2247
|
0
|
|
|
|
|
|
my $ax = 0; |
|
2248
|
0
|
|
|
|
|
|
foreach my $plot (@{ $args->{plots} } ) { |
|
|
0
|
|
|
|
|
|
|
|
2249
|
0
|
0
|
0
|
|
|
|
if ( |
|
2250
|
|
|
|
|
|
|
(defined $args->{'shared.colorbar'}) && # shared.colorbar is defined |
|
2251
|
0
|
|
|
|
|
|
(grep {$_ == $ax} @{ $args->{'shared.colorbar'} }) # subplot's colorbar is shared w other plots |
|
|
0
|
|
|
|
|
|
|
|
2252
|
|
|
|
|
|
|
) { |
|
2253
|
0
|
0
|
|
|
|
|
if ($ax == max( @{ $args->{'shared.colorbar'} } )) { # this is the max |
|
|
0
|
|
|
|
|
|
|
|
2254
|
0
|
|
|
|
|
|
$plot->{'colorbar.on'} = 1; # turn on if this is the max plot |
|
2255
|
0
|
|
|
|
|
|
$plot->{'shared.colorbar'} = $args->{'shared.colorbar'}; |
|
2256
|
0
|
|
|
|
|
|
$plot->{cbpad} = $args->{cbpad}; |
|
2257
|
|
|
|
|
|
|
} else { |
|
2258
|
0
|
|
|
|
|
|
$plot->{'colorbar.on'} = 0; # turn off, its colorbar will be shared later |
|
2259
|
|
|
|
|
|
|
} |
|
2260
|
|
|
|
|
|
|
} |
|
2261
|
0
|
0
|
|
|
|
|
if (defined $plot->{add}) { |
|
2262
|
0
|
|
|
|
|
|
my $ref = ref $plot->{add}; |
|
2263
|
0
|
0
|
|
|
|
|
if ($ref ne 'ARRAY') { |
|
2264
|
0
|
|
|
|
|
|
die "\"add\" must be an array (of anonymous hashes), but you entered a $ref reference at ax = $ax"; |
|
2265
|
|
|
|
|
|
|
} |
|
2266
|
|
|
|
|
|
|
} |
|
2267
|
0
|
|
|
|
|
|
foreach my $graph (@{ $plot->{add} }) { |
|
|
0
|
|
|
|
|
|
|
|
2268
|
0
|
|
|
|
|
|
$dispatch{$graph->{'plot.type'}}->({ |
|
2269
|
|
|
|
|
|
|
fh => $fh, |
|
2270
|
|
|
|
|
|
|
ax => $ax, |
|
2271
|
|
|
|
|
|
|
plot => $graph |
|
2272
|
|
|
|
|
|
|
}); |
|
2273
|
|
|
|
|
|
|
} |
|
2274
|
0
|
|
|
|
|
|
delete $plot->{add}; |
|
2275
|
0
|
|
|
|
|
|
my @reqd_keys = ( |
|
2276
|
|
|
|
|
|
|
'data', # data type, of which several are available |
|
2277
|
|
|
|
|
|
|
'plot.type', # "bar", "barh", "hist", etc. |
|
2278
|
|
|
|
|
|
|
); |
|
2279
|
0
|
|
|
|
|
|
my @undef_keys = grep { !defined $plot->{$_} } @reqd_keys; |
|
|
0
|
|
|
|
|
|
|
|
2280
|
0
|
0
|
|
|
|
|
if ( scalar @undef_keys > 0 ) { |
|
2281
|
0
|
|
|
|
|
|
p $plot; |
|
2282
|
0
|
|
|
|
|
|
p @undef_keys; |
|
2283
|
0
|
|
|
|
|
|
die "Above args are necessary, but were not defined for plot $ax."; |
|
2284
|
|
|
|
|
|
|
} |
|
2285
|
0
|
|
|
|
|
|
$dispatch{$plot->{'plot.type'}}->({ |
|
2286
|
|
|
|
|
|
|
fh => $fh, |
|
2287
|
|
|
|
|
|
|
ax => $ax, |
|
2288
|
|
|
|
|
|
|
plot => $plot |
|
2289
|
|
|
|
|
|
|
}); |
|
2290
|
0
|
|
|
|
|
|
my %rename = ( |
|
2291
|
|
|
|
|
|
|
xlabel => 'set_xlabel', title => 'set_title', |
|
2292
|
|
|
|
|
|
|
ylabel => 'set_ylabel', legend => 'legend', |
|
2293
|
|
|
|
|
|
|
# xlim => 'set_xlim', |
|
2294
|
|
|
|
|
|
|
); |
|
2295
|
0
|
|
|
|
|
|
foreach my $opt ( grep { defined $rename{$_} } keys %{$plot} ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
2296
|
0
|
|
|
|
|
|
$plot->{ $rename{$opt} } = delete $plot->{$opt}; |
|
2297
|
|
|
|
|
|
|
} |
|
2298
|
|
|
|
|
|
|
plot_args({ |
|
2299
|
0
|
|
|
|
|
|
fh => $fh, |
|
2300
|
|
|
|
|
|
|
args => $plot, |
|
2301
|
|
|
|
|
|
|
ax => "ax$ax" |
|
2302
|
|
|
|
|
|
|
}); |
|
2303
|
0
|
|
|
|
|
|
$ax++; |
|
2304
|
|
|
|
|
|
|
} |
|
2305
|
0
|
|
|
|
|
|
foreach my $ax (@ax) { |
|
2306
|
0
|
|
|
|
|
|
say $fh "if $ax.has_data() == False:"; # remove empty plots |
|
2307
|
0
|
|
|
|
|
|
say $fh "\t$ax.remove()"; # remove empty plots |
|
2308
|
|
|
|
|
|
|
} |
|
2309
|
0
|
|
|
|
|
|
my %methods = map { $_ => 1 } @plt_methods; |
|
|
0
|
|
|
|
|
|
|
|
2310
|
0
|
|
|
|
|
|
foreach my $plt_method ( grep { defined $methods{$_} } keys %{$args} ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
2311
|
0
|
|
|
|
|
|
my $ref = ref $args->{$plt_method}; |
|
2312
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { |
|
|
|
0
|
|
|
|
|
|
|
2313
|
0
|
|
|
|
|
|
my $type = print_type($args->{$plt_method}); |
|
2314
|
0
|
0
|
|
|
|
|
if ($type eq 'single quotes') { |
|
|
|
0
|
|
|
|
|
|
|
2315
|
0
|
0
|
|
|
|
|
if ($plt_method eq 'show') { |
|
2316
|
0
|
|
|
|
|
|
say $fh "plt.$plt_method()#" . __LINE__; |
|
2317
|
|
|
|
|
|
|
} else { |
|
2318
|
0
|
|
|
|
|
|
say $fh "plt.$plt_method('$args->{$plt_method}')#" . __LINE__; |
|
2319
|
|
|
|
|
|
|
} |
|
2320
|
|
|
|
|
|
|
} elsif ($type eq 'no quotes') { |
|
2321
|
0
|
|
|
|
|
|
say $fh "plt.$plt_method($args->{$plt_method})#" . __LINE__; |
|
2322
|
|
|
|
|
|
|
} |
|
2323
|
|
|
|
|
|
|
} elsif ( $ref eq 'ARRAY' ) { |
|
2324
|
0
|
|
|
|
|
|
foreach my $j ( @{ $args->{$plt_method} } ) { |
|
|
0
|
|
|
|
|
|
|
|
2325
|
0
|
|
|
|
|
|
my $type = print_type($j); |
|
2326
|
0
|
0
|
|
|
|
|
if ($type eq 'single quotes') { |
|
|
|
0
|
|
|
|
|
|
|
2327
|
0
|
|
|
|
|
|
say $fh "plt.$plt_method('$j')#" . __LINE__; |
|
2328
|
|
|
|
|
|
|
} elsif ($type eq 'no quotes') { |
|
2329
|
0
|
|
|
|
|
|
say $fh "plt.$plt_method($j)#" . __LINE__; |
|
2330
|
|
|
|
|
|
|
} |
|
2331
|
|
|
|
|
|
|
} |
|
2332
|
|
|
|
|
|
|
} else { |
|
2333
|
0
|
|
|
|
|
|
p $args; |
|
2334
|
0
|
|
|
|
|
|
die "$plt_method = \"$ref\" only accepts scalar or array types"; |
|
2335
|
|
|
|
|
|
|
} |
|
2336
|
|
|
|
|
|
|
} |
|
2337
|
0
|
|
|
|
|
|
%methods = map { $_ => 1 } @fig_methods; |
|
|
0
|
|
|
|
|
|
|
|
2338
|
0
|
|
|
|
|
|
foreach my $fig_method ( grep { defined $methods{$_} } keys %{$args} ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
2339
|
0
|
|
|
|
|
|
my $ref = ref $args->{$fig_method}; |
|
2340
|
0
|
0
|
|
|
|
|
if ( $ref eq '' ) { |
|
|
|
0
|
|
|
|
|
|
|
2341
|
0
|
|
|
|
|
|
say $fh "fig.$fig_method($args->{$fig_method})#" . __LINE__; |
|
2342
|
|
|
|
|
|
|
} elsif ( $ref eq 'ARRAY' ) { |
|
2343
|
0
|
|
|
|
|
|
foreach my $j ( @{ $args->{$fig_method} } ) { # say $fh "plt.$method($plt)"; |
|
|
0
|
|
|
|
|
|
|
|
2344
|
0
|
|
|
|
|
|
say $fh "fig.$fig_method($j)"; |
|
2345
|
|
|
|
|
|
|
} |
|
2346
|
|
|
|
|
|
|
} else { |
|
2347
|
0
|
|
|
|
|
|
p $args; |
|
2348
|
0
|
|
|
|
|
|
die "$fig_method = \"$ref\" only accepts scalar or array types"; |
|
2349
|
|
|
|
|
|
|
} |
|
2350
|
|
|
|
|
|
|
} |
|
2351
|
0
|
0
|
|
|
|
|
if (defined $args->{scale}) { |
|
2352
|
0
|
|
|
|
|
|
say $fh "fig.set_figheight(plt.rcParams['figure.figsize'][1] * $args->{scale}) #" . __LINE__; |
|
2353
|
0
|
|
|
|
|
|
say $fh "fig.set_figwidth(plt.rcParams['figure.figsize'][0] * $args->{scale}) #" . __LINE__; |
|
2354
|
|
|
|
|
|
|
} |
|
2355
|
0
|
0
|
|
|
|
|
if (defined $args->{scalex}) { |
|
2356
|
0
|
|
|
|
|
|
say $fh "fig.set_figwidth(plt.rcParams['figure.figsize'][0] * $args->{scalex}) #" . __LINE__; |
|
2357
|
|
|
|
|
|
|
} |
|
2358
|
0
|
0
|
|
|
|
|
if (defined $args->{scaley}) { |
|
2359
|
0
|
|
|
|
|
|
say $fh "fig.set_figheight(plt.rcParams['figure.figsize'][1] * $args->{scaley}) #" . __LINE__; |
|
2360
|
|
|
|
|
|
|
} |
|
2361
|
|
|
|
|
|
|
write_data({ |
|
2362
|
0
|
|
|
|
|
|
data => $args->{'output.file'}, |
|
2363
|
|
|
|
|
|
|
fh => $fh, |
|
2364
|
|
|
|
|
|
|
name => 'output_file' |
|
2365
|
|
|
|
|
|
|
}); |
|
2366
|
0
|
|
|
|
|
|
say $fh "plt.savefig(output_file, bbox_inches = 'tight', metadata={'Creator': 'made/written by " |
|
2367
|
|
|
|
|
|
|
. getcwd() |
|
2368
|
|
|
|
|
|
|
. "/$RealScript called using \"$current_sub\" in " . __FILE__ . " version $VERSION'})"; |
|
2369
|
0
|
|
0
|
|
|
|
$args->{execute} = $args->{execute} // 1; |
|
2370
|
0
|
0
|
|
|
|
|
say $fh 'plt.close()' if $args->{execute} == 0; |
|
2371
|
0
|
0
|
|
|
|
|
if ( $args->{execute} ) { |
|
2372
|
|
|
|
|
|
|
my ($stdout, $stderr, $exit) = capture { |
|
2373
|
0
|
|
|
0
|
|
|
system( 'python3 ' . $fh->filename ) |
|
2374
|
0
|
|
|
|
|
|
}; |
|
2375
|
0
|
0
|
|
|
|
|
if ($exit != 0) { |
|
2376
|
0
|
|
|
|
|
|
say STDERR "STDOUT = $stdout"; |
|
2377
|
0
|
|
|
|
|
|
say STDERR "STDERR = $stderr"; |
|
2378
|
0
|
|
|
|
|
|
die 'python3 ' . $fh->filename . ' failed'; |
|
2379
|
|
|
|
|
|
|
} |
|
2380
|
0
|
|
|
|
|
|
say 'wrote ' |
|
2381
|
|
|
|
|
|
|
. colored( ['cyan on_bright_yellow'], "$args->{'output.file'}" ); |
|
2382
|
|
|
|
|
|
|
} else { # not running yet |
|
2383
|
0
|
|
|
|
|
|
say 'will write ' |
|
2384
|
|
|
|
|
|
|
. colored( ['cyan on_bright_yellow'], "$args->{'output.file'}" ); |
|
2385
|
|
|
|
|
|
|
} |
|
2386
|
0
|
|
|
|
|
|
return $fh->filename; |
|
2387
|
|
|
|
|
|
|
} |
|
2388
|
|
|
|
|
|
|
# Generate wrappers dynamically |
|
2389
|
|
|
|
|
|
|
my @wrappers = qw(bar barh boxplot colored_table hexbin hist hist2d imshow pie plot scatter violin wide); |
|
2390
|
|
|
|
|
|
|
|
|
2391
|
|
|
|
|
|
|
foreach my $sub_name (@wrappers) { |
|
2392
|
2
|
|
|
2
|
|
24
|
no strict 'refs'; # Gemini helped |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
996
|
|
|
2393
|
|
|
|
|
|
|
*$sub_name = sub { |
|
2394
|
0
|
|
|
0
|
|
|
my ($args) = @_; |
|
2395
|
|
|
|
|
|
|
# Check for conflicts |
|
2396
|
0
|
0
|
0
|
|
|
|
if ((defined $args->{'plot.type'}) && ($args->{'plot.type'} ne $sub_name)) { |
|
2397
|
0
|
|
|
|
|
|
warn "$args->{'plot.type'} will be ignored for $sub_name"; |
|
2398
|
|
|
|
|
|
|
} |
|
2399
|
0
|
0
|
|
|
|
|
if (defined $args->{plots}) { |
|
2400
|
0
|
|
|
|
|
|
die "\"plots\" is meant for the subroutine \"plt\"; $sub_name is single-only"; |
|
2401
|
|
|
|
|
|
|
} |
|
2402
|
|
|
|
|
|
|
# Call plt |
|
2403
|
0
|
|
|
|
|
|
plt({ %{ $args }, 'plot.type' => $sub_name }); |
|
|
0
|
|
|
|
|
|
|
|
2404
|
|
|
|
|
|
|
}; |
|
2405
|
|
|
|
|
|
|
} |
|
2406
|
|
|
|
|
|
|
1; |
|
2407
|
|
|
|
|
|
|
# from md2pod.pl πατερ ημων ο εν τοις ουρανοις, ἁγιασθήτω τὸ ὄνομά σου |
|
2408
|
|
|
|
|
|
|
=encoding utf8 |
|
2409
|
|
|
|
|
|
|
|
|
2410
|
|
|
|
|
|
|
=head1 Synopsis |
|
2411
|
|
|
|
|
|
|
|
|
2412
|
|
|
|
|
|
|
Take a data structure in Perl, and automatically write a Python3 script using matplotlib to generate an image. The Python3 script is saved in C, to be edited at the user's discretion. |
|
2413
|
|
|
|
|
|
|
Requires python3 and matplotlib installations. |
|
2414
|
|
|
|
|
|
|
|
|
2415
|
|
|
|
|
|
|
My aim is to simplify the most common tasks as much as possible. In my opinion, using this module is much easier than matplotlib itself. |
|
2416
|
|
|
|
|
|
|
|
|
2417
|
|
|
|
|
|
|
=head1 Single Plots |
|
2418
|
|
|
|
|
|
|
|
|
2419
|
|
|
|
|
|
|
Simplest use case: |
|
2420
|
|
|
|
|
|
|
|
|
2421
|
|
|
|
|
|
|
use Matplotlib::Simple; |
|
2422
|
|
|
|
|
|
|
bar({ |
|
2423
|
|
|
|
|
|
|
'output.file' => '/tmp/gospel.word.counts.png', |
|
2424
|
|
|
|
|
|
|
data => { |
|
2425
|
|
|
|
|
|
|
Matthew => 18345, |
|
2426
|
|
|
|
|
|
|
Mark => 11304, |
|
2427
|
|
|
|
|
|
|
Luke => 19482, |
|
2428
|
|
|
|
|
|
|
John => 15635, |
|
2429
|
|
|
|
|
|
|
} |
|
2430
|
|
|
|
|
|
|
}); |
|
2431
|
|
|
|
|
|
|
|
|
2432
|
|
|
|
|
|
|
A more complete (and slightly faster execution): |
|
2433
|
|
|
|
|
|
|
|
|
2434
|
|
|
|
|
|
|
use Matplotlib::Simple; |
|
2435
|
|
|
|
|
|
|
plt({ |
|
2436
|
|
|
|
|
|
|
'output.file' => '/tmp/gospel.word.counts.png', |
|
2437
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2438
|
|
|
|
|
|
|
data => { |
|
2439
|
|
|
|
|
|
|
Matthew => 18345, |
|
2440
|
|
|
|
|
|
|
Mark => 11304, |
|
2441
|
|
|
|
|
|
|
Luke => 19482, |
|
2442
|
|
|
|
|
|
|
John => 15635, |
|
2443
|
|
|
|
|
|
|
} |
|
2444
|
|
|
|
|
|
|
}); |
|
2445
|
|
|
|
|
|
|
|
|
2446
|
|
|
|
|
|
|
|
|
2447
|
|
|
|
|
|
|
=for html |
|
2448
|
|
|
|
|
|
|
|
|
2449
|
|
|
|
|
|
|
|
|
2450
|
|
|
|
|
|
|
|
|
2451
|
|
|
|
|
|
|
|
|
2452
|
|
|
|
|
|
|
|
|
2453
|
|
|
|
|
|
|
=head1 Multiple Plots |
|
2454
|
|
|
|
|
|
|
|
|
2455
|
|
|
|
|
|
|
Having a C argument as an array lets the module know to create subplots: |
|
2456
|
|
|
|
|
|
|
|
|
2457
|
|
|
|
|
|
|
use Matplotlib::Simple 'plt'; |
|
2458
|
|
|
|
|
|
|
plt({ |
|
2459
|
|
|
|
|
|
|
'output.file' => 'svg/pies.png', |
|
2460
|
|
|
|
|
|
|
plots => [ |
|
2461
|
|
|
|
|
|
|
{ |
|
2462
|
|
|
|
|
|
|
data => { |
|
2463
|
|
|
|
|
|
|
Russian => 106_000_000, # Primarily European Russia |
|
2464
|
|
|
|
|
|
|
German => 95_000_000, # Germany, Austria, Switzerland, etc. |
|
2465
|
|
|
|
|
|
|
}, |
|
2466
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
2467
|
|
|
|
|
|
|
title => 'Top Languages in Europe', |
|
2468
|
|
|
|
|
|
|
suptitle => 'Pie in subplots', |
|
2469
|
|
|
|
|
|
|
}, |
|
2470
|
|
|
|
|
|
|
{ |
|
2471
|
|
|
|
|
|
|
data => { |
|
2472
|
|
|
|
|
|
|
Russian => 106_000_000, # Primarily European Russia |
|
2473
|
|
|
|
|
|
|
German => 95_000_000, # Germany, Austria, Switzerland, etc. |
|
2474
|
|
|
|
|
|
|
}, |
|
2475
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
2476
|
|
|
|
|
|
|
title => 'Top Languages in Europe', |
|
2477
|
|
|
|
|
|
|
}, |
|
2478
|
|
|
|
|
|
|
], |
|
2479
|
|
|
|
|
|
|
ncols => 2, |
|
2480
|
|
|
|
|
|
|
}); |
|
2481
|
|
|
|
|
|
|
|
|
2482
|
|
|
|
|
|
|
which produces the following subplots image: |
|
2483
|
|
|
|
|
|
|
|
|
2484
|
|
|
|
|
|
|
|
|
2485
|
|
|
|
|
|
|
=for html |
|
2486
|
|
|
|
|
|
|
|
|
2487
|
|
|
|
|
|
|
|
|
2488
|
|
|
|
|
|
|
|
|
2489
|
|
|
|
|
|
|
|
|
2490
|
|
|
|
|
|
|
|
|
2491
|
|
|
|
|
|
|
C, C, C, C, C, C, C, C, C, C, and C all match the methods in matplotlib itself. |
|
2492
|
|
|
|
|
|
|
|
|
2493
|
|
|
|
|
|
|
=head2 Options |
|
2494
|
|
|
|
|
|
|
|
|
2495
|
|
|
|
|
|
|
C and C are both implemented at the plot, rather than subplot, level. See Matplotlib's documentation for more clarity. |
|
2496
|
|
|
|
|
|
|
|
|
2497
|
|
|
|
|
|
|
=head1 Color Bars (colorbars) |
|
2498
|
|
|
|
|
|
|
|
|
2499
|
|
|
|
|
|
|
Colarbar args attempt to match matplotlib closely |
|
2500
|
|
|
|
|
|
|
|
|
2501
|
|
|
|
|
|
|
=for html |
|
2502
|
|
|
|
|
|
|
|
2503
|
|
|
|
|
|
|
|
|
2504
|
|
|
|
|
|
|
| | Option | Description | Example |
|
2505
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
2506
|
|
|
|
|
|
|
| cbdrawedges | Whether to draw lines at color boundaries | cbdrawedges => 1 |
|
2507
|
|
|
|
|
|
|
| cblabel | The label on the colorbar's long axis | cblabel => 1 |
|
2508
|
|
|
|
|
|
|
| cblocation | of the colorbar None or {'left', 'right', 'top', 'bottom'} | |
|
2509
|
|
|
|
|
|
|
| cborientation | # None or {vertical, horizontal} | |
|
2510
|
|
|
|
|
|
|
| cbpad | pad : float, default: 0.05 if vertical, 0.15 if horizontal; Fraction of original Axes between colorbar and new image Axes | |
|
2511
|
|
|
|
|
|
|
| cb_logscale | Perl true (anything but 0) or false (0) | |
|
2512
|
|
|
|
|
|
|
| shared.colorbar | share colorbar between different plots: specify plot indices | 'shared.colorbar' => [0,1] |
|
2513
|
|
|
|
|
|
|
|
|
2514
|
|
|
|
|
|
|
| |
|
2515
|
|
|
|
|
|
|
|
|
2516
|
|
|
|
|
|
|
=head1 Size/Dimensions of output file |
|
2517
|
|
|
|
|
|
|
|
|
2518
|
|
|
|
|
|
|
=for html |
|
2519
|
|
|
|
|
|
|
|
2520
|
|
|
|
|
|
|
|
|
2521
|
|
|
|
|
|
|
| | Option | Description | Example |
|
2522
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
2523
|
|
|
|
|
|
|
| scale | scale/multiply the size of the output figure | scale => 2.4 |
|
2524
|
|
|
|
|
|
|
| scalex | scale/multiply the x-axis only | scalex => 2.4 |
|
2525
|
|
|
|
|
|
|
| scaley | scale/multiply the y-axis only | scalex => 1.4 |
|
2526
|
|
|
|
|
|
|
|
|
2527
|
|
|
|
|
|
|
| |
|
2528
|
|
|
|
|
|
|
|
|
2529
|
|
|
|
|
|
|
=head1 Examples/Plot Types |
|
2530
|
|
|
|
|
|
|
|
|
2531
|
|
|
|
|
|
|
Consider the following helper subroutines to generate data to plot: |
|
2532
|
|
|
|
|
|
|
|
|
2533
|
|
|
|
|
|
|
sub linspace { # mostly written by Grok |
|
2534
|
|
|
|
|
|
|
my ($start, $stop, $num, $endpoint) = @_; # endpoint means include $stop |
|
2535
|
|
|
|
|
|
|
$num = defined $num ? int($num) : 50; # Default to 50 points |
|
2536
|
|
|
|
|
|
|
$endpoint = defined $endpoint ? $endpoint : 1; # Default to include endpoint |
|
2537
|
|
|
|
|
|
|
return () if $num < 0; # Return empty array for invalid num |
|
2538
|
|
|
|
|
|
|
return ($start) if $num == 1; # Return single value if num is 1 |
|
2539
|
|
|
|
|
|
|
my (@result, $step); |
|
2540
|
|
|
|
|
|
|
|
|
2541
|
|
|
|
|
|
|
if ($endpoint) { |
|
2542
|
|
|
|
|
|
|
$step = ($stop - $start) / ($num - 1) if $num > 1; |
|
2543
|
|
|
|
|
|
|
for my $i (0 .. $num - 1) { |
|
2544
|
|
|
|
|
|
|
$result[$i] = $start + $i * $step; |
|
2545
|
|
|
|
|
|
|
} |
|
2546
|
|
|
|
|
|
|
} else { |
|
2547
|
|
|
|
|
|
|
$step = ($stop - $start) / $num; |
|
2548
|
|
|
|
|
|
|
for my $i (0 .. $num - 1) { |
|
2549
|
|
|
|
|
|
|
$result[$i] = $start + $i * $step; |
|
2550
|
|
|
|
|
|
|
} |
|
2551
|
|
|
|
|
|
|
} |
|
2552
|
|
|
|
|
|
|
return @result; |
|
2553
|
|
|
|
|
|
|
} |
|
2554
|
|
|
|
|
|
|
|
|
2555
|
|
|
|
|
|
|
sub generate_normal_dist { |
|
2556
|
|
|
|
|
|
|
my ($mean, $std_dev, $size) = @_; |
|
2557
|
|
|
|
|
|
|
$size = defined $size ? int $size : 100; # default to 100 points |
|
2558
|
|
|
|
|
|
|
my @numbers; |
|
2559
|
|
|
|
|
|
|
for (1 .. int($size / 2) + 1) {# Box-Muller transform |
|
2560
|
|
|
|
|
|
|
my $u1 = rand(); |
|
2561
|
|
|
|
|
|
|
my $u2 = rand(); |
|
2562
|
|
|
|
|
|
|
my $z0 = sqrt(-2.0 * log($u1)) * cos(2.0 * 3.141592653589793 * $u2); |
|
2563
|
|
|
|
|
|
|
my $z1 = sqrt(-2.0 * log($u1)) * sin(2.0 * 3.141592653589793 * $u2); # Scale and shift to match mean and std_dev |
|
2564
|
|
|
|
|
|
|
push @numbers, ($z0 * $std_dev + $mean, $z1 * $std_dev + $mean); |
|
2565
|
|
|
|
|
|
|
} # Trim to exact size if needed |
|
2566
|
|
|
|
|
|
|
@numbers = @numbers[0 .. $size - 1] if @numbers > $size; |
|
2567
|
|
|
|
|
|
|
@numbers = map {sprintf '%.1f', $_} @numbers; |
|
2568
|
|
|
|
|
|
|
return \@numbers; |
|
2569
|
|
|
|
|
|
|
} |
|
2570
|
|
|
|
|
|
|
sub rand_between { |
|
2571
|
|
|
|
|
|
|
my ($min, $max) = @_; |
|
2572
|
|
|
|
|
|
|
return $min + rand($max - $min) |
|
2573
|
|
|
|
|
|
|
} |
|
2574
|
|
|
|
|
|
|
|
|
2575
|
|
|
|
|
|
|
=head2 Barplot/bar/barh |
|
2576
|
|
|
|
|
|
|
|
|
2577
|
|
|
|
|
|
|
Plot a hash or a hash of arrays as a boxplot |
|
2578
|
|
|
|
|
|
|
|
|
2579
|
|
|
|
|
|
|
=head3 Options |
|
2580
|
|
|
|
|
|
|
|
|
2581
|
|
|
|
|
|
|
=for html |
|
2582
|
|
|
|
|
|
|
|
2583
|
|
|
|
|
|
|
|
|
2584
|
|
|
|
|
|
|
| | Option | Description | Example |
|
2585
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
2586
|
|
|
|
|
|
|
| | color | :mpltype:color or list of :mpltype:color, optional; The colors of the bar faces. This is an alias for *facecolor*. If both are given, *facecolor* takes precedence # if entering multiple colors, quoting isn't needed; as of version 0.23, colors can be given as a hash | color => ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'fuchsia'], or a single color for all bars color => 'red', or as of version 0.23 color => {A => 'red', B => 'green'} |
|
2587
|
|
|
|
|
|
|
| | edgecolor | :mpltype:color or list of :mpltype:color, optional; The colors of the bar edges | edgecolor => 'black' |
|
2588
|
|
|
|
|
|
|
| | key.order | define the keys in an order (an array reference) | 'key.order' => ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], |
|
2589
|
|
|
|
|
|
|
| | linewidth | float or array, optional; Width of the bar edge(s). If 0, don't draw edges. Only does anything with defined edgecolor | linewidth => 2, |
|
2590
|
|
|
|
|
|
|
| | log | bool, default: False; If *True*, set the y-axis to be log scale. | log = 'True', |
|
2591
|
|
|
|
|
|
|
| | stacked | stack the groups on top of one another; default 0 = off | stacked => 1, |
|
2592
|
|
|
|
|
|
|
| | width | float only, default: 0.8; The width(s) of the bars. width will be deactivated with grouped, non-stacked bar plots | width => 0.4, |
|
2593
|
|
|
|
|
|
|
| | xerr | float or array-like of shape(N,) or shape(2, N), optional. If not *None*, add horizontal / vertical errorbars to the bar tips. The values are +/- sizes relative to the data: - scalar: symmetric +/- values for all bars # - shape(N,): symmetric +/- values for each bar # - shape(2, N): Separate - and + values for each bar. First row # contains the lower errors, the second row contains the upper # errors. # - *None*: No errorbar. (Default) | yerr => {'USA' => [15,29], 'Russia' => [199,1000],} |
|
2594
|
|
|
|
|
|
|
| | yerr | same as xerr, but better with bar | |
|
2595
|
|
|
|
|
|
|
|
|
2596
|
|
|
|
|
|
|
| |
|
2597
|
|
|
|
|
|
|
|
|
2598
|
|
|
|
|
|
|
an example of multiple plots, showing many options: |
|
2599
|
|
|
|
|
|
|
|
|
2600
|
|
|
|
|
|
|
=head3 single, simple plot |
|
2601
|
|
|
|
|
|
|
|
|
2602
|
|
|
|
|
|
|
use Matplotlib::Simple 'plt'; |
|
2603
|
|
|
|
|
|
|
plt({ |
|
2604
|
|
|
|
|
|
|
'output.file' => 'output.images/single.barplot.png', |
|
2605
|
|
|
|
|
|
|
data => { # simple hash |
|
2606
|
|
|
|
|
|
|
Fri => 76, Mon => 73, Sat => 26, Sun => 11, Thu => 94, Tue => 93, Wed => 77 |
|
2607
|
|
|
|
|
|
|
}, |
|
2608
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2609
|
|
|
|
|
|
|
xlabel => '# of Days', |
|
2610
|
|
|
|
|
|
|
ylabel => 'Count', |
|
2611
|
|
|
|
|
|
|
title => 'Customer Calls by Days' |
|
2612
|
|
|
|
|
|
|
}); |
|
2613
|
|
|
|
|
|
|
|
|
2614
|
|
|
|
|
|
|
where C, C, C, etc. are axis methods in matplotlib itself. C, C, C are all specific to C. |
|
2615
|
|
|
|
|
|
|
|
|
2616
|
|
|
|
|
|
|
=for html |
|
2617
|
|
|
|
|
|
|
|
|
2618
|
|
|
|
|
|
|
|
|
2619
|
|
|
|
|
|
|
|
|
2620
|
|
|
|
|
|
|
|
|
2621
|
|
|
|
|
|
|
|
|
2622
|
|
|
|
|
|
|
=head3 multiple plots |
|
2623
|
|
|
|
|
|
|
|
|
2624
|
|
|
|
|
|
|
plt({ |
|
2625
|
|
|
|
|
|
|
fh => $fh, |
|
2626
|
|
|
|
|
|
|
execute => 0, |
|
2627
|
|
|
|
|
|
|
'output.file' => 'output.images/barplots.png', |
|
2628
|
|
|
|
|
|
|
plots => [ |
|
2629
|
|
|
|
|
|
|
{ # simple plot |
|
2630
|
|
|
|
|
|
|
data => { # simple hash |
|
2631
|
|
|
|
|
|
|
Fri => 76, Mon => 73, Sat => 26, Sun => 11, Thu => 94, Tue => 93, Wed => 77 |
|
2632
|
|
|
|
|
|
|
}, |
|
2633
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2634
|
|
|
|
|
|
|
'key.order' => ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], |
|
2635
|
|
|
|
|
|
|
suptitle => 'Types of Plots', # applies to all |
|
2636
|
|
|
|
|
|
|
color => ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'fuchsia'], |
|
2637
|
|
|
|
|
|
|
edgecolor => 'black', |
|
2638
|
|
|
|
|
|
|
set_figwidth => 40/1.5, # applies to all plots |
|
2639
|
|
|
|
|
|
|
set_figheight => 30/2, # applies to all plots |
|
2640
|
|
|
|
|
|
|
title => 'bar: Rejections During Job Search', |
|
2641
|
|
|
|
|
|
|
xlabel => 'Day of the Week', |
|
2642
|
|
|
|
|
|
|
ylabel => 'No. of Rejections' |
|
2643
|
|
|
|
|
|
|
}, |
|
2644
|
|
|
|
|
|
|
{ # grouped bar plot |
|
2645
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2646
|
|
|
|
|
|
|
data => { |
|
2647
|
|
|
|
|
|
|
1941 => { |
|
2648
|
|
|
|
|
|
|
UK => 6.6, |
|
2649
|
|
|
|
|
|
|
US => 6.2, |
|
2650
|
|
|
|
|
|
|
USSR => 17.8, |
|
2651
|
|
|
|
|
|
|
Germany => 26.6 |
|
2652
|
|
|
|
|
|
|
}, |
|
2653
|
|
|
|
|
|
|
1942 => { |
|
2654
|
|
|
|
|
|
|
UK => 7.6, |
|
2655
|
|
|
|
|
|
|
US => 26.4, |
|
2656
|
|
|
|
|
|
|
USSR => 19.2, |
|
2657
|
|
|
|
|
|
|
Germany => 29.7 |
|
2658
|
|
|
|
|
|
|
}, |
|
2659
|
|
|
|
|
|
|
1943 => { |
|
2660
|
|
|
|
|
|
|
UK => 7.9, |
|
2661
|
|
|
|
|
|
|
US => 61.4, |
|
2662
|
|
|
|
|
|
|
USSR => 22.5, |
|
2663
|
|
|
|
|
|
|
Germany => 34.9 |
|
2664
|
|
|
|
|
|
|
}, |
|
2665
|
|
|
|
|
|
|
1944 => { |
|
2666
|
|
|
|
|
|
|
UK => 7.4, |
|
2667
|
|
|
|
|
|
|
US => 80.5, |
|
2668
|
|
|
|
|
|
|
USSR => 27.0, |
|
2669
|
|
|
|
|
|
|
Germany => 31.4 |
|
2670
|
|
|
|
|
|
|
}, |
|
2671
|
|
|
|
|
|
|
1945 => { |
|
2672
|
|
|
|
|
|
|
UK => 5.4, |
|
2673
|
|
|
|
|
|
|
US => 83.1, |
|
2674
|
|
|
|
|
|
|
USSR => 25.5, |
|
2675
|
|
|
|
|
|
|
Germany => 11.2 #Rapid decrease due to war's end |
|
2676
|
|
|
|
|
|
|
}, |
|
2677
|
|
|
|
|
|
|
}, |
|
2678
|
|
|
|
|
|
|
stacked => 0, |
|
2679
|
|
|
|
|
|
|
title => 'Hash of Hash Grouped Unstacked Barplot', |
|
2680
|
|
|
|
|
|
|
width => 0.23, |
|
2681
|
|
|
|
|
|
|
xlabel => 'r"$\it{anno}$ $\it{domini}$"', # italic |
|
2682
|
|
|
|
|
|
|
ylabel => 'Military Expenditure (Billions of $)' |
|
2683
|
|
|
|
|
|
|
}, |
|
2684
|
|
|
|
|
|
|
{ # grouped bar plot |
|
2685
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2686
|
|
|
|
|
|
|
data => { |
|
2687
|
|
|
|
|
|
|
1941 => { |
|
2688
|
|
|
|
|
|
|
UK => 6.6, |
|
2689
|
|
|
|
|
|
|
US => 6.2, |
|
2690
|
|
|
|
|
|
|
USSR => 17.8, |
|
2691
|
|
|
|
|
|
|
Germany => 26.6 |
|
2692
|
|
|
|
|
|
|
}, |
|
2693
|
|
|
|
|
|
|
1942 => { |
|
2694
|
|
|
|
|
|
|
UK => 7.6, |
|
2695
|
|
|
|
|
|
|
US => 26.4, |
|
2696
|
|
|
|
|
|
|
USSR => 19.2, |
|
2697
|
|
|
|
|
|
|
Germany => 29.7 |
|
2698
|
|
|
|
|
|
|
}, |
|
2699
|
|
|
|
|
|
|
1943 => { |
|
2700
|
|
|
|
|
|
|
UK => 7.9, |
|
2701
|
|
|
|
|
|
|
US => 61.4, |
|
2702
|
|
|
|
|
|
|
USSR => 22.5, |
|
2703
|
|
|
|
|
|
|
Germany => 34.9 |
|
2704
|
|
|
|
|
|
|
}, |
|
2705
|
|
|
|
|
|
|
1944 => { |
|
2706
|
|
|
|
|
|
|
UK => 7.4, |
|
2707
|
|
|
|
|
|
|
US => 80.5, |
|
2708
|
|
|
|
|
|
|
USSR => 27.0, |
|
2709
|
|
|
|
|
|
|
Germany => 31.4 |
|
2710
|
|
|
|
|
|
|
}, |
|
2711
|
|
|
|
|
|
|
1945 => { |
|
2712
|
|
|
|
|
|
|
UK => 5.4, |
|
2713
|
|
|
|
|
|
|
US => 83.1, |
|
2714
|
|
|
|
|
|
|
USSR => 25.5, |
|
2715
|
|
|
|
|
|
|
Germany => 11.2 #Rapid decrease due to war's end |
|
2716
|
|
|
|
|
|
|
}, |
|
2717
|
|
|
|
|
|
|
}, |
|
2718
|
|
|
|
|
|
|
stacked => 1, |
|
2719
|
|
|
|
|
|
|
title => 'Hash of Hash Grouped Stacked Barplot', |
|
2720
|
|
|
|
|
|
|
xlabel => 'r"$\it{anno}$ $\it{domini}$"', # italic |
|
2721
|
|
|
|
|
|
|
ylabel => 'Military Expenditure (Billions of $)' |
|
2722
|
|
|
|
|
|
|
}, |
|
2723
|
|
|
|
|
|
|
{# grouped barplot: arrays indicate Union, Confederate which must be specified in options hash |
|
2724
|
|
|
|
|
|
|
data => { # 4th plot: arrays indicate Union, Confederate which must be specified in options hash |
|
2725
|
|
|
|
|
|
|
'Antietam' => [ 12400, 10300 ], |
|
2726
|
|
|
|
|
|
|
'Gettysburg' => [ 23000, 28000 ], |
|
2727
|
|
|
|
|
|
|
'Chickamauga' => [ 16000, 18000 ], |
|
2728
|
|
|
|
|
|
|
'Chancellorsville' => [ 17000, 13000 ], |
|
2729
|
|
|
|
|
|
|
'Wilderness' => [ 17500, 11000 ], |
|
2730
|
|
|
|
|
|
|
'Spotsylvania' => [ 18000, 12000 ], |
|
2731
|
|
|
|
|
|
|
'Cold Harbor' => [ 12000, 5000 ], |
|
2732
|
|
|
|
|
|
|
'Shiloh' => [ 13000, 10700 ], |
|
2733
|
|
|
|
|
|
|
'Second Bull Run' => [ 10000, 8000 ], |
|
2734
|
|
|
|
|
|
|
'Fredericksburg' => [ 12600, 5300 ], |
|
2735
|
|
|
|
|
|
|
}, |
|
2736
|
|
|
|
|
|
|
'plot.type' => 'barh', |
|
2737
|
|
|
|
|
|
|
color => ['blue', 'gray'], # colors match indices of data arrays |
|
2738
|
|
|
|
|
|
|
label => ['North', 'South'], # colors match indices of data arrays |
|
2739
|
|
|
|
|
|
|
xlabel => 'Casualties', |
|
2740
|
|
|
|
|
|
|
ylabel => 'Battle', |
|
2741
|
|
|
|
|
|
|
title => 'barh: hash of array' |
|
2742
|
|
|
|
|
|
|
}, |
|
2743
|
|
|
|
|
|
|
{ # 5th plot: barplot with groups |
|
2744
|
|
|
|
|
|
|
data => { |
|
2745
|
|
|
|
|
|
|
1942 => [ 109867, 310000, 7700000 ], # US, Japan, USSR |
|
2746
|
|
|
|
|
|
|
1943 => [ 221111, 440000, 9000000 ], |
|
2747
|
|
|
|
|
|
|
1944 => [ 318584, 610000, 7000000 ], |
|
2748
|
|
|
|
|
|
|
1945 => [ 318929, 1060000, 3000000 ], |
|
2749
|
|
|
|
|
|
|
}, |
|
2750
|
|
|
|
|
|
|
color => ['blue', 'pink', 'red'], # colors match indices of data arrays |
|
2751
|
|
|
|
|
|
|
label => ['USA', 'Japan', 'USSR'], # colors match indices of data arrays |
|
2752
|
|
|
|
|
|
|
'log' => 1, |
|
2753
|
|
|
|
|
|
|
title => 'grouped bar: Casualties in WWII', |
|
2754
|
|
|
|
|
|
|
ylabel => 'Casualties', |
|
2755
|
|
|
|
|
|
|
'plot.type' => 'bar' |
|
2756
|
|
|
|
|
|
|
}, |
|
2757
|
|
|
|
|
|
|
{ # nuclear weapons barplot |
|
2758
|
|
|
|
|
|
|
'plot.type' => 'bar', |
|
2759
|
|
|
|
|
|
|
data => { |
|
2760
|
|
|
|
|
|
|
'USA' => 5277, # FAS Estimate |
|
2761
|
|
|
|
|
|
|
'Russia' => 5449, # FAS Estimate |
|
2762
|
|
|
|
|
|
|
'UK' => 225, # Consistent estimate |
|
2763
|
|
|
|
|
|
|
'France' => 290, # Consistent estimate |
|
2764
|
|
|
|
|
|
|
'China' => 600, # FAS Estimate |
|
2765
|
|
|
|
|
|
|
'India' => 180, # FAS Estimate |
|
2766
|
|
|
|
|
|
|
'Pakistan' => 130, # FAS Estimate |
|
2767
|
|
|
|
|
|
|
'Israel' => 90, # FAS Estimate |
|
2768
|
|
|
|
|
|
|
'North Korea' => 50, # FAS Estimate |
|
2769
|
|
|
|
|
|
|
}, |
|
2770
|
|
|
|
|
|
|
title => 'Simple hash for barchart with yerr', |
|
2771
|
|
|
|
|
|
|
xlabel => 'Country', |
|
2772
|
|
|
|
|
|
|
yerr => { |
|
2773
|
|
|
|
|
|
|
'USA' => [15,29], |
|
2774
|
|
|
|
|
|
|
'Russia' => [199,1000], |
|
2775
|
|
|
|
|
|
|
'UK' => [15,19], |
|
2776
|
|
|
|
|
|
|
'France' => [19,29], |
|
2777
|
|
|
|
|
|
|
'China' => [200,159], |
|
2778
|
|
|
|
|
|
|
'India' => [15,25], |
|
2779
|
|
|
|
|
|
|
'Pakistan' => [15,49], |
|
2780
|
|
|
|
|
|
|
'Israel' => [90,50], |
|
2781
|
|
|
|
|
|
|
'North Korea' => [10,20], |
|
2782
|
|
|
|
|
|
|
}, |
|
2783
|
|
|
|
|
|
|
ylabel => '# of Nuclear Warheads', |
|
2784
|
|
|
|
|
|
|
'log' => 'True', # linewidth => 1, |
|
2785
|
|
|
|
|
|
|
} |
|
2786
|
|
|
|
|
|
|
], |
|
2787
|
|
|
|
|
|
|
ncols => 3, |
|
2788
|
|
|
|
|
|
|
nrows => 4 |
|
2789
|
|
|
|
|
|
|
}); |
|
2790
|
|
|
|
|
|
|
|
|
2791
|
|
|
|
|
|
|
which produces the plot: |
|
2792
|
|
|
|
|
|
|
|
|
2793
|
|
|
|
|
|
|
|
|
2794
|
|
|
|
|
|
|
=for html |
|
2795
|
|
|
|
|
|
|
|
|
2796
|
|
|
|
|
|
|
|
|
2797
|
|
|
|
|
|
|
|
|
2798
|
|
|
|
|
|
|
|
|
2799
|
|
|
|
|
|
|
|
|
2800
|
|
|
|
|
|
|
=head3 colors for each hash key defined by hash |
|
2801
|
|
|
|
|
|
|
|
|
2802
|
|
|
|
|
|
|
plt({ |
|
2803
|
|
|
|
|
|
|
plots => [ |
|
2804
|
|
|
|
|
|
|
{ |
|
2805
|
|
|
|
|
|
|
color => { |
|
2806
|
|
|
|
|
|
|
A => 'red', B => 'green', C => 'blue' |
|
2807
|
|
|
|
|
|
|
}, |
|
2808
|
|
|
|
|
|
|
data => { |
|
2809
|
|
|
|
|
|
|
A => 1, B => 2, C => 3 |
|
2810
|
|
|
|
|
|
|
}, |
|
2811
|
|
|
|
|
|
|
'plot.type' => 'bar' |
|
2812
|
|
|
|
|
|
|
}, |
|
2813
|
|
|
|
|
|
|
{ |
|
2814
|
|
|
|
|
|
|
color => { |
|
2815
|
|
|
|
|
|
|
A => 'red', B => 'green', C => 'blue' |
|
2816
|
|
|
|
|
|
|
}, |
|
2817
|
|
|
|
|
|
|
data => { |
|
2818
|
|
|
|
|
|
|
A => 1, B => 2, C => 3 |
|
2819
|
|
|
|
|
|
|
}, |
|
2820
|
|
|
|
|
|
|
'plot.type' => 'barh' |
|
2821
|
|
|
|
|
|
|
}, |
|
2822
|
|
|
|
|
|
|
], |
|
2823
|
|
|
|
|
|
|
ncols => 2, |
|
2824
|
|
|
|
|
|
|
'output.file' => '/tmp/key.colors.bar.svg', |
|
2825
|
|
|
|
|
|
|
}); |
|
2826
|
|
|
|
|
|
|
|
|
2827
|
|
|
|
|
|
|
which produces the plot |
|
2828
|
|
|
|
|
|
|
|
|
2829
|
|
|
|
|
|
|
|
|
2830
|
|
|
|
|
|
|
=for html |
|
2831
|
|
|
|
|
|
|
|
|
2832
|
|
|
|
|
|
|
|
|
2833
|
|
|
|
|
|
|
|
|
2834
|
|
|
|
|
|
|
|
|
2835
|
|
|
|
|
|
|
|
|
2836
|
|
|
|
|
|
|
=head2 boxplot |
|
2837
|
|
|
|
|
|
|
|
|
2838
|
|
|
|
|
|
|
Plot a hash of arrays as a series of boxplots |
|
2839
|
|
|
|
|
|
|
|
|
2840
|
|
|
|
|
|
|
=head3 options |
|
2841
|
|
|
|
|
|
|
|
|
2842
|
|
|
|
|
|
|
=for html |
|
2843
|
|
|
|
|
|
|
|
2844
|
|
|
|
|
|
|
|
|
2845
|
|
|
|
|
|
|
| | Option | Description | Example |
|
2846
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
2847
|
|
|
|
|
|
|
| color | a single color for all plots | color => 'pink' |
|
2848
|
|
|
|
|
|
|
| colors | a hash, where each data point and color is a hash pair | colors => { A => 'orange', E => 'yellow', B => 'purple' }, |
|
2849
|
|
|
|
|
|
|
| key.order | order that the keys in the entry hash will be plotted | key.order = ['A', 'E', 'B'] |
|
2850
|
|
|
|
|
|
|
| orientation | orientation of the plot, by default vertical | orientation = 'horizontal' |
|
2851
|
|
|
|
|
|
|
| showcaps | Show the caps on the ends of whiskers; default True | showcaps => 'False', |
|
2852
|
|
|
|
|
|
|
| showfliers | Show the outliers beyond the caps; default True | showfliers => 'False' |
|
2853
|
|
|
|
|
|
|
| showmeans | show means; default = True | showmeans => 'False' |
|
2854
|
|
|
|
|
|
|
| whiskers | show whiskers, default = 1 | whiskers => 0, |
|
2855
|
|
|
|
|
|
|
|
|
2856
|
|
|
|
|
|
|
| |
|
2857
|
|
|
|
|
|
|
|
|
2858
|
|
|
|
|
|
|
=head3 single, simple plot |
|
2859
|
|
|
|
|
|
|
|
|
2860
|
|
|
|
|
|
|
my $x = generate_normal_dist( 100, 15, 3 * 10 ); |
|
2861
|
|
|
|
|
|
|
my $y = generate_normal_dist( 85, 15, 3 * 10 ); |
|
2862
|
|
|
|
|
|
|
my $z = generate_normal_dist( 106, 15, 3 * 10 ); |
|
2863
|
|
|
|
|
|
|
|
|
2864
|
|
|
|
|
|
|
single plots are simple |
|
2865
|
|
|
|
|
|
|
|
|
2866
|
|
|
|
|
|
|
use Matplotlib::Simple 'barplot'; |
|
2867
|
|
|
|
|
|
|
boxplot({ |
|
2868
|
|
|
|
|
|
|
'output.file' => 'output.images/single.boxplot.png', |
|
2869
|
|
|
|
|
|
|
data => { # simple hash |
|
2870
|
|
|
|
|
|
|
E => [ 55, @{$x}, 160 ], |
|
2871
|
|
|
|
|
|
|
B => [ @{$y}, 140 ], |
|
2872
|
|
|
|
|
|
|
|
|
2873
|
|
|
|
|
|
|
# A => @a |
|
2874
|
|
|
|
|
|
|
}, |
|
2875
|
|
|
|
|
|
|
title => 'Single Box Plot: Specified Colors', |
|
2876
|
|
|
|
|
|
|
colors => { E => 'yellow', B => 'purple' }, |
|
2877
|
|
|
|
|
|
|
fh => $fh, |
|
2878
|
|
|
|
|
|
|
execute => 0, |
|
2879
|
|
|
|
|
|
|
}); |
|
2880
|
|
|
|
|
|
|
|
|
2881
|
|
|
|
|
|
|
which makes the following image: |
|
2882
|
|
|
|
|
|
|
|
|
2883
|
|
|
|
|
|
|
|
|
2884
|
|
|
|
|
|
|
=for html |
|
2885
|
|
|
|
|
|
|
|
|
2886
|
|
|
|
|
|
|
|
|
2887
|
|
|
|
|
|
|
|
|
2888
|
|
|
|
|
|
|
|
|
2889
|
|
|
|
|
|
|
|
|
2890
|
|
|
|
|
|
|
=head3 multiple plots |
|
2891
|
|
|
|
|
|
|
|
|
2892
|
|
|
|
|
|
|
plt({ |
|
2893
|
|
|
|
|
|
|
'output.file' => 'output.images/boxplot.png', |
|
2894
|
|
|
|
|
|
|
execute => 0, |
|
2895
|
|
|
|
|
|
|
fh => $fh, |
|
2896
|
|
|
|
|
|
|
plots => [ |
|
2897
|
|
|
|
|
|
|
{ |
|
2898
|
|
|
|
|
|
|
data => { |
|
2899
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2900
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2901
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2902
|
|
|
|
|
|
|
}, |
|
2903
|
|
|
|
|
|
|
title => 'Simple Boxplot', |
|
2904
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2905
|
|
|
|
|
|
|
xlabel => 'label', |
|
2906
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
2907
|
|
|
|
|
|
|
suptitle => 'Boxplot examples' |
|
2908
|
|
|
|
|
|
|
}, |
|
2909
|
|
|
|
|
|
|
{ |
|
2910
|
|
|
|
|
|
|
color => 'pink', |
|
2911
|
|
|
|
|
|
|
data => { |
|
2912
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2913
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2914
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2915
|
|
|
|
|
|
|
}, |
|
2916
|
|
|
|
|
|
|
title => 'Specify single color', |
|
2917
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2918
|
|
|
|
|
|
|
xlabel => 'label', |
|
2919
|
|
|
|
|
|
|
'plot.type' => 'boxplot' |
|
2920
|
|
|
|
|
|
|
}, |
|
2921
|
|
|
|
|
|
|
{ |
|
2922
|
|
|
|
|
|
|
colors => { |
|
2923
|
|
|
|
|
|
|
A => 'orange', |
|
2924
|
|
|
|
|
|
|
E => 'yellow', |
|
2925
|
|
|
|
|
|
|
B => 'purple' |
|
2926
|
|
|
|
|
|
|
}, |
|
2927
|
|
|
|
|
|
|
data => { |
|
2928
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2929
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2930
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2931
|
|
|
|
|
|
|
}, |
|
2932
|
|
|
|
|
|
|
title => 'Specify set-specific color; showfliers = False', |
|
2933
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2934
|
|
|
|
|
|
|
xlabel => 'label', |
|
2935
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
2936
|
|
|
|
|
|
|
showmeans => 'True', |
|
2937
|
|
|
|
|
|
|
showfliers => 'False', |
|
2938
|
|
|
|
|
|
|
set_figwidth => 12 |
|
2939
|
|
|
|
|
|
|
}, |
|
2940
|
|
|
|
|
|
|
{ |
|
2941
|
|
|
|
|
|
|
colors => { |
|
2942
|
|
|
|
|
|
|
A => 'orange', |
|
2943
|
|
|
|
|
|
|
E => 'yellow', |
|
2944
|
|
|
|
|
|
|
B => 'purple' |
|
2945
|
|
|
|
|
|
|
}, |
|
2946
|
|
|
|
|
|
|
data => { |
|
2947
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2948
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2949
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2950
|
|
|
|
|
|
|
}, |
|
2951
|
|
|
|
|
|
|
title => 'Specify set-specific color; showmeans = False', |
|
2952
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2953
|
|
|
|
|
|
|
xlabel => 'label', |
|
2954
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
2955
|
|
|
|
|
|
|
showmeans => 'False', |
|
2956
|
|
|
|
|
|
|
}, |
|
2957
|
|
|
|
|
|
|
{ |
|
2958
|
|
|
|
|
|
|
colors => { |
|
2959
|
|
|
|
|
|
|
A => 'orange', |
|
2960
|
|
|
|
|
|
|
E => 'yellow', |
|
2961
|
|
|
|
|
|
|
B => 'purple' |
|
2962
|
|
|
|
|
|
|
}, |
|
2963
|
|
|
|
|
|
|
data => { |
|
2964
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2965
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2966
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2967
|
|
|
|
|
|
|
}, |
|
2968
|
|
|
|
|
|
|
title => 'Set-specific color; orientation = horizontal', |
|
2969
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2970
|
|
|
|
|
|
|
xlabel => 'label', |
|
2971
|
|
|
|
|
|
|
orientation => 'horizontal', |
|
2972
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
2973
|
|
|
|
|
|
|
}, |
|
2974
|
|
|
|
|
|
|
{ |
|
2975
|
|
|
|
|
|
|
colors => { |
|
2976
|
|
|
|
|
|
|
A => 'orange', |
|
2977
|
|
|
|
|
|
|
E => 'yellow', |
|
2978
|
|
|
|
|
|
|
B => 'purple' |
|
2979
|
|
|
|
|
|
|
}, |
|
2980
|
|
|
|
|
|
|
data => { |
|
2981
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2982
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
2983
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
2984
|
|
|
|
|
|
|
}, |
|
2985
|
|
|
|
|
|
|
title => 'Notch = True', |
|
2986
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
2987
|
|
|
|
|
|
|
xlabel => 'label', |
|
2988
|
|
|
|
|
|
|
notch => 'True', |
|
2989
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
2990
|
|
|
|
|
|
|
}, |
|
2991
|
|
|
|
|
|
|
{ |
|
2992
|
|
|
|
|
|
|
colors => { |
|
2993
|
|
|
|
|
|
|
A => 'orange', |
|
2994
|
|
|
|
|
|
|
E => 'yellow', |
|
2995
|
|
|
|
|
|
|
B => 'purple' |
|
2996
|
|
|
|
|
|
|
}, |
|
2997
|
|
|
|
|
|
|
data => { |
|
2998
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
2999
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
3000
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
3001
|
|
|
|
|
|
|
}, |
|
3002
|
|
|
|
|
|
|
title => 'showcaps = False', |
|
3003
|
|
|
|
|
|
|
ylabel => 'ylabel', |
|
3004
|
|
|
|
|
|
|
xlabel => 'label', |
|
3005
|
|
|
|
|
|
|
showcaps => 'False', |
|
3006
|
|
|
|
|
|
|
'plot.type' => 'boxplot', |
|
3007
|
|
|
|
|
|
|
set_figheight => 12, |
|
3008
|
|
|
|
|
|
|
}, |
|
3009
|
|
|
|
|
|
|
], |
|
3010
|
|
|
|
|
|
|
ncols => 3, |
|
3011
|
|
|
|
|
|
|
nrows => 3, |
|
3012
|
|
|
|
|
|
|
}); |
|
3013
|
|
|
|
|
|
|
|
|
3014
|
|
|
|
|
|
|
which makes the following plot: |
|
3015
|
|
|
|
|
|
|
|
|
3016
|
|
|
|
|
|
|
|
|
3017
|
|
|
|
|
|
|
=for html |
|
3018
|
|
|
|
|
|
|
|
|
3019
|
|
|
|
|
|
|
|
|
3020
|
|
|
|
|
|
|
|
|
3021
|
|
|
|
|
|
|
|
|
3022
|
|
|
|
|
|
|
|
|
3023
|
|
|
|
|
|
|
=head2 Colored Table |
|
3024
|
|
|
|
|
|
|
|
|
3025
|
|
|
|
|
|
|
=head3 options |
|
3026
|
|
|
|
|
|
|
|
|
3027
|
|
|
|
|
|
|
=head3 Single, simple plot |
|
3028
|
|
|
|
|
|
|
|
|
3029
|
|
|
|
|
|
|
the bond dissociation energy table can be plotted: |
|
3030
|
|
|
|
|
|
|
|
|
3031
|
|
|
|
|
|
|
# https://labs.chem.ucsb.edu/zakarian/armen/11---bonddissociationenergy.pdf and https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Supplemental_Modules_(Physical_and_Theoretical_Chemistry)/Chemical_Bonding/Fundamentals_of_Chemical_Bonding/Bond_Energies |
|
3032
|
|
|
|
|
|
|
my %bond_dissociation = ( |
|
3033
|
|
|
|
|
|
|
Br => { |
|
3034
|
|
|
|
|
|
|
Br => 193 |
|
3035
|
|
|
|
|
|
|
}, |
|
3036
|
|
|
|
|
|
|
C => { |
|
3037
|
|
|
|
|
|
|
Br => 276, C => 347, Cl => 339, F => 485, H => 413, I => 240, |
|
3038
|
|
|
|
|
|
|
N => 305, O => 358, S => 259 |
|
3039
|
|
|
|
|
|
|
}, |
|
3040
|
|
|
|
|
|
|
Cl => { |
|
3041
|
|
|
|
|
|
|
Br => 218, Cl => 239 |
|
3042
|
|
|
|
|
|
|
}, |
|
3043
|
|
|
|
|
|
|
F => { |
|
3044
|
|
|
|
|
|
|
I => 280, Br => 237, Cl => 253, F => 154 |
|
3045
|
|
|
|
|
|
|
}, |
|
3046
|
|
|
|
|
|
|
H => { |
|
3047
|
|
|
|
|
|
|
Br => 363, Cl => 427, F => 565, H => 432, I => 295 |
|
3048
|
|
|
|
|
|
|
}, |
|
3049
|
|
|
|
|
|
|
I => { |
|
3050
|
|
|
|
|
|
|
Br => 175, Cl => 208, I => 149 |
|
3051
|
|
|
|
|
|
|
}, |
|
3052
|
|
|
|
|
|
|
N => { |
|
3053
|
|
|
|
|
|
|
Br => 243, Cl => 200, F => 272, H => 391, N => 160, O => 201 |
|
3054
|
|
|
|
|
|
|
}, |
|
3055
|
|
|
|
|
|
|
O => { |
|
3056
|
|
|
|
|
|
|
Cl => 203, F => 190, H => 467, I => 234, O => 146 |
|
3057
|
|
|
|
|
|
|
}, |
|
3058
|
|
|
|
|
|
|
S => { |
|
3059
|
|
|
|
|
|
|
Br => 218, Cl => 253, F => 327, H => 347, S => 266 |
|
3060
|
|
|
|
|
|
|
}, |
|
3061
|
|
|
|
|
|
|
Si => { |
|
3062
|
|
|
|
|
|
|
C => 360, H => 393, O => 452, Si => 340 |
|
3063
|
|
|
|
|
|
|
} |
|
3064
|
|
|
|
|
|
|
); |
|
3065
|
|
|
|
|
|
|
|
|
3066
|
|
|
|
|
|
|
and the plot itself: |
|
3067
|
|
|
|
|
|
|
|
|
3068
|
|
|
|
|
|
|
colored_table({ |
|
3069
|
|
|
|
|
|
|
'cblabel' => 'kJ/mol', |
|
3070
|
|
|
|
|
|
|
'col.labels' => ['H', 'F', 'Cl', 'Br', 'I'], |
|
3071
|
|
|
|
|
|
|
data => \%bond_dissociation, |
|
3072
|
|
|
|
|
|
|
execute => 0, |
|
3073
|
|
|
|
|
|
|
fh => $fh, |
|
3074
|
|
|
|
|
|
|
mirror => 1, |
|
3075
|
|
|
|
|
|
|
'output.file' => 'output.images/single.tab.png', |
|
3076
|
|
|
|
|
|
|
'row.labels' => ['H', 'F', 'Cl', 'Br', 'I'], |
|
3077
|
|
|
|
|
|
|
'show.numbers'=> 1, |
|
3078
|
|
|
|
|
|
|
set_title => 'Bond Dissociation Energy' |
|
3079
|
|
|
|
|
|
|
}); |
|
3080
|
|
|
|
|
|
|
|
|
3081
|
|
|
|
|
|
|
which makes the following image: |
|
3082
|
|
|
|
|
|
|
|
|
3083
|
|
|
|
|
|
|
|
|
3084
|
|
|
|
|
|
|
=for html |
|
3085
|
|
|
|
|
|
|
|
|
3086
|
|
|
|
|
|
|
|
|
3087
|
|
|
|
|
|
|
|
|
3088
|
|
|
|
|
|
|
|
|
3089
|
|
|
|
|
|
|
|
|
3090
|
|
|
|
|
|
|
=head3 Multiple Plots |
|
3091
|
|
|
|
|
|
|
|
|
3092
|
|
|
|
|
|
|
plt({ |
|
3093
|
|
|
|
|
|
|
'output.file' => 'output.images/tab.multiple.png', |
|
3094
|
|
|
|
|
|
|
execute => 0, |
|
3095
|
|
|
|
|
|
|
fh => $fh, |
|
3096
|
|
|
|
|
|
|
plots => [ |
|
3097
|
|
|
|
|
|
|
{ |
|
3098
|
|
|
|
|
|
|
data => \%bond_dissociation, |
|
3099
|
|
|
|
|
|
|
'output.file' => '/tmp/single.bonds.svg', |
|
3100
|
|
|
|
|
|
|
'plot.type' => 'colored_table', |
|
3101
|
|
|
|
|
|
|
set_title => 'No other options' |
|
3102
|
|
|
|
|
|
|
}, |
|
3103
|
|
|
|
|
|
|
{ |
|
3104
|
|
|
|
|
|
|
data => \%bond_dissociation, |
|
3105
|
|
|
|
|
|
|
cblabel => 'Average Dissociation Energy (kJ/mol)', |
|
3106
|
|
|
|
|
|
|
'col.labels' => ['H', 'C', 'N', 'O', 'F', 'Si', 'S', 'Cl', 'Br', 'I'], |
|
3107
|
|
|
|
|
|
|
mirror => 1, |
|
3108
|
|
|
|
|
|
|
'output.file' => '/tmp/single.bonds.svg', |
|
3109
|
|
|
|
|
|
|
'plot.type' => 'colored_table', |
|
3110
|
|
|
|
|
|
|
'row.labels' => ['H', 'C', 'N', 'O', 'F', 'Si', 'S', 'Cl', 'Br', 'I'], |
|
3111
|
|
|
|
|
|
|
'show.numbers'=> 1, |
|
3112
|
|
|
|
|
|
|
set_title => 'Showing numbers and mirror with defined order' |
|
3113
|
|
|
|
|
|
|
}, |
|
3114
|
|
|
|
|
|
|
{ |
|
3115
|
|
|
|
|
|
|
data => \%bond_dissociation, |
|
3116
|
|
|
|
|
|
|
cblabel => 'Average Dissociation Energy (kJ/mol)', |
|
3117
|
|
|
|
|
|
|
'col.labels' => ['H', 'C', 'N', 'O', 'F', 'Si', 'S', 'Cl', 'Br', 'I'], |
|
3118
|
|
|
|
|
|
|
mirror => 1, |
|
3119
|
|
|
|
|
|
|
'output.file' => '/tmp/single.bonds.svg', |
|
3120
|
|
|
|
|
|
|
'plot.type' => 'colored_table', |
|
3121
|
|
|
|
|
|
|
'row.labels' => ['H', 'C', 'N', 'O', 'F', 'Si', 'S', 'Cl', 'Br', 'I'], |
|
3122
|
|
|
|
|
|
|
'show.numbers'=> 1, |
|
3123
|
|
|
|
|
|
|
set_title => 'Set undefined color to white', |
|
3124
|
|
|
|
|
|
|
'undef.color' => 'white' |
|
3125
|
|
|
|
|
|
|
} |
|
3126
|
|
|
|
|
|
|
], |
|
3127
|
|
|
|
|
|
|
ncols => 3, |
|
3128
|
|
|
|
|
|
|
set_figwidth => 14, |
|
3129
|
|
|
|
|
|
|
suptitle => 'Colored Table options' |
|
3130
|
|
|
|
|
|
|
}); |
|
3131
|
|
|
|
|
|
|
|
|
3132
|
|
|
|
|
|
|
which makes the following plot: |
|
3133
|
|
|
|
|
|
|
|
|
3134
|
|
|
|
|
|
|
|
|
3135
|
|
|
|
|
|
|
=for html |
|
3136
|
|
|
|
|
|
|
|
|
3137
|
|
|
|
|
|
|
|
|
3138
|
|
|
|
|
|
|
|
|
3139
|
|
|
|
|
|
|
|
|
3140
|
|
|
|
|
|
|
|
|
3141
|
|
|
|
|
|
|
=head2 hexbin |
|
3142
|
|
|
|
|
|
|
|
|
3143
|
|
|
|
|
|
|
Plot a hash of arrays as a hexbin |
|
3144
|
|
|
|
|
|
|
see https://matplotlib.org/stable/api/Igen/matplotlib.pyplot.hexbin.html |
|
3145
|
|
|
|
|
|
|
|
|
3146
|
|
|
|
|
|
|
=head3 options |
|
3147
|
|
|
|
|
|
|
|
|
3148
|
|
|
|
|
|
|
=for html |
|
3149
|
|
|
|
|
|
|
|
3150
|
|
|
|
|
|
|
|
|
3151
|
|
|
|
|
|
|
| | Option | Description | Example |
|
3152
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
3153
|
|
|
|
|
|
|
| | cb_logscale | colorbar log scale from matplotlib.colors import LogNorm | default 0, any value > 0 enables |
|
3154
|
|
|
|
|
|
|
| | cmap | The Colormap instance or registered colormap name used to map scalar data to colors | default gist_rainbow |
|
3155
|
|
|
|
|
|
|
| | key.order | define the keys in an order (an array reference) | 'key.order' => ['X-rays', 'Yak Butter'], |
|
3156
|
|
|
|
|
|
|
| | marginals | integer, by default off = 0 | marginals => 1 |
|
3157
|
|
|
|
|
|
|
| | mincnt | int >= 0, default: None; If not None, only display cells with at least mincnt number of points in the cell. | mincnt => 2 |
|
3158
|
|
|
|
|
|
|
| | vmax | The normalization method used to scale scalar data to the [0, 1] range before mapping to colors using cmap | 'asinh', 'function', 'functionlog', 'linear', 'log', 'logit', 'symlog' default linear |
|
3159
|
|
|
|
|
|
|
| | vmin | The normalization method used to scale scalar data to the [0, 1] range before mapping to colors using cmap | 'asinh', 'function', 'functionlog', 'linear', 'log', 'logit', 'symlog' default linear |
|
3160
|
|
|
|
|
|
|
| | xbins | integer that accesses horizontal gridsize | default is 15 |
|
3161
|
|
|
|
|
|
|
| | xscale.hexbin | 'linear', 'log'}, default: 'linear': Use a linear or log10 scale on the horizontal axis | 'xscale.hexbin' => 'log' |
|
3162
|
|
|
|
|
|
|
| | ybins | integer that accesses vertical gridsize | default is 15 |
|
3163
|
|
|
|
|
|
|
| | yscale.hexbin | 'linear', 'log'}, default: 'linear': Use a linear or log10 scale on the vertical axis | 'yscale.hexbin' => 'log' |
|
3164
|
|
|
|
|
|
|
|
|
3165
|
|
|
|
|
|
|
| |
|
3166
|
|
|
|
|
|
|
|
|
3167
|
|
|
|
|
|
|
=head3 single, simple plot |
|
3168
|
|
|
|
|
|
|
|
|
3169
|
|
|
|
|
|
|
plt({ |
|
3170
|
|
|
|
|
|
|
data => { |
|
3171
|
|
|
|
|
|
|
E => generate_normal_dist(100, 15, 3*210), |
|
3172
|
|
|
|
|
|
|
B => generate_normal_dist(85, 15, 3*210) |
|
3173
|
|
|
|
|
|
|
}, |
|
3174
|
|
|
|
|
|
|
'output.file' => 'output.images/single.hexbin.png', |
|
3175
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3176
|
|
|
|
|
|
|
set_figwidth => 12, |
|
3177
|
|
|
|
|
|
|
title => 'Simple Hexbin', |
|
3178
|
|
|
|
|
|
|
}); |
|
3179
|
|
|
|
|
|
|
|
|
3180
|
|
|
|
|
|
|
which makes the following plot: |
|
3181
|
|
|
|
|
|
|
|
|
3182
|
|
|
|
|
|
|
=for html |
|
3183
|
|
|
|
|
|
|
|
|
3184
|
|
|
|
|
|
|
|
|
3185
|
|
|
|
|
|
|
|
|
3186
|
|
|
|
|
|
|
|
|
3187
|
|
|
|
|
|
|
=head3 multiple plots |
|
3188
|
|
|
|
|
|
|
|
|
3189
|
|
|
|
|
|
|
plt({ |
|
3190
|
|
|
|
|
|
|
fh => $fh, |
|
3191
|
|
|
|
|
|
|
execute => 0, |
|
3192
|
|
|
|
|
|
|
'output.file' => 'output.images/hexbin.png', |
|
3193
|
|
|
|
|
|
|
plots => [ |
|
3194
|
|
|
|
|
|
|
{ |
|
3195
|
|
|
|
|
|
|
data => { |
|
3196
|
|
|
|
|
|
|
E => @e, |
|
3197
|
|
|
|
|
|
|
B => @b |
|
3198
|
|
|
|
|
|
|
}, |
|
3199
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3200
|
|
|
|
|
|
|
title => 'Simple Hexbin', |
|
3201
|
|
|
|
|
|
|
}, |
|
3202
|
|
|
|
|
|
|
{ |
|
3203
|
|
|
|
|
|
|
data => { |
|
3204
|
|
|
|
|
|
|
E => @e, |
|
3205
|
|
|
|
|
|
|
B => @b |
|
3206
|
|
|
|
|
|
|
}, |
|
3207
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3208
|
|
|
|
|
|
|
title => 'colorbar logscale', |
|
3209
|
|
|
|
|
|
|
cb_logscale => 1 |
|
3210
|
|
|
|
|
|
|
}, |
|
3211
|
|
|
|
|
|
|
{ |
|
3212
|
|
|
|
|
|
|
cmap => 'jet', |
|
3213
|
|
|
|
|
|
|
data => { |
|
3214
|
|
|
|
|
|
|
E => @e, |
|
3215
|
|
|
|
|
|
|
B => @b |
|
3216
|
|
|
|
|
|
|
}, |
|
3217
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3218
|
|
|
|
|
|
|
title => 'cmap is jet', |
|
3219
|
|
|
|
|
|
|
xlabel => 'xlabel', |
|
3220
|
|
|
|
|
|
|
}, |
|
3221
|
|
|
|
|
|
|
{ |
|
3222
|
|
|
|
|
|
|
data => { |
|
3223
|
|
|
|
|
|
|
E => @e, |
|
3224
|
|
|
|
|
|
|
B => @b |
|
3225
|
|
|
|
|
|
|
}, |
|
3226
|
|
|
|
|
|
|
'key.order' => ['E', 'B'], |
|
3227
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3228
|
|
|
|
|
|
|
title => 'Switch axes with key.order', |
|
3229
|
|
|
|
|
|
|
}, |
|
3230
|
|
|
|
|
|
|
{ |
|
3231
|
|
|
|
|
|
|
data => { |
|
3232
|
|
|
|
|
|
|
E => @e, |
|
3233
|
|
|
|
|
|
|
B => @b |
|
3234
|
|
|
|
|
|
|
}, |
|
3235
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3236
|
|
|
|
|
|
|
title => 'vmax set to 25', |
|
3237
|
|
|
|
|
|
|
vmax => 25 |
|
3238
|
|
|
|
|
|
|
}, |
|
3239
|
|
|
|
|
|
|
{ |
|
3240
|
|
|
|
|
|
|
data => { |
|
3241
|
|
|
|
|
|
|
E => @e, |
|
3242
|
|
|
|
|
|
|
B => @b |
|
3243
|
|
|
|
|
|
|
}, |
|
3244
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3245
|
|
|
|
|
|
|
title => 'vmin set to -4', |
|
3246
|
|
|
|
|
|
|
vmin => -4 |
|
3247
|
|
|
|
|
|
|
}, |
|
3248
|
|
|
|
|
|
|
{ |
|
3249
|
|
|
|
|
|
|
data => { |
|
3250
|
|
|
|
|
|
|
E => @e, |
|
3251
|
|
|
|
|
|
|
B => @b |
|
3252
|
|
|
|
|
|
|
}, |
|
3253
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3254
|
|
|
|
|
|
|
title => 'mincnt set to 7', |
|
3255
|
|
|
|
|
|
|
mincnt => 7 |
|
3256
|
|
|
|
|
|
|
}, |
|
3257
|
|
|
|
|
|
|
{ |
|
3258
|
|
|
|
|
|
|
data => { |
|
3259
|
|
|
|
|
|
|
E => @e, |
|
3260
|
|
|
|
|
|
|
B => @b |
|
3261
|
|
|
|
|
|
|
}, |
|
3262
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3263
|
|
|
|
|
|
|
title => 'xbins set to 9', |
|
3264
|
|
|
|
|
|
|
xbins => 9 |
|
3265
|
|
|
|
|
|
|
}, |
|
3266
|
|
|
|
|
|
|
{ |
|
3267
|
|
|
|
|
|
|
data => { |
|
3268
|
|
|
|
|
|
|
E => @e, |
|
3269
|
|
|
|
|
|
|
B => @b |
|
3270
|
|
|
|
|
|
|
}, |
|
3271
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3272
|
|
|
|
|
|
|
title => 'ybins set to 9', |
|
3273
|
|
|
|
|
|
|
ybins => 9 |
|
3274
|
|
|
|
|
|
|
}, |
|
3275
|
|
|
|
|
|
|
{ |
|
3276
|
|
|
|
|
|
|
data => { |
|
3277
|
|
|
|
|
|
|
E => @e, |
|
3278
|
|
|
|
|
|
|
B => @b |
|
3279
|
|
|
|
|
|
|
}, |
|
3280
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3281
|
|
|
|
|
|
|
title => 'marginals = 1', |
|
3282
|
|
|
|
|
|
|
marginals => 1 |
|
3283
|
|
|
|
|
|
|
}, |
|
3284
|
|
|
|
|
|
|
{ |
|
3285
|
|
|
|
|
|
|
data => { |
|
3286
|
|
|
|
|
|
|
E => @e, |
|
3287
|
|
|
|
|
|
|
B => @b |
|
3288
|
|
|
|
|
|
|
}, |
|
3289
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3290
|
|
|
|
|
|
|
title => 'xscale.hexbin = 1', |
|
3291
|
|
|
|
|
|
|
'xscale.hexbin' => 'log' |
|
3292
|
|
|
|
|
|
|
}, |
|
3293
|
|
|
|
|
|
|
{ |
|
3294
|
|
|
|
|
|
|
data => { |
|
3295
|
|
|
|
|
|
|
E => @e, |
|
3296
|
|
|
|
|
|
|
B => @b |
|
3297
|
|
|
|
|
|
|
}, |
|
3298
|
|
|
|
|
|
|
'plot.type' => 'hexbin', |
|
3299
|
|
|
|
|
|
|
title => 'yscale.hexbin = 1', |
|
3300
|
|
|
|
|
|
|
'yscale.hexbin' => 'log' |
|
3301
|
|
|
|
|
|
|
}, |
|
3302
|
|
|
|
|
|
|
], |
|
3303
|
|
|
|
|
|
|
ncols => 4, |
|
3304
|
|
|
|
|
|
|
nrows => 3, |
|
3305
|
|
|
|
|
|
|
scale => 5, |
|
3306
|
|
|
|
|
|
|
suptitle => 'Various Changes to Standard Hexbin: All data is the same' |
|
3307
|
|
|
|
|
|
|
}); |
|
3308
|
|
|
|
|
|
|
|
|
3309
|
|
|
|
|
|
|
which produces the following image: |
|
3310
|
|
|
|
|
|
|
|
|
3311
|
|
|
|
|
|
|
=for html |
|
3312
|
|
|
|
|
|
|
|
|
3313
|
|
|
|
|
|
|
|
|
3314
|
|
|
|
|
|
|
|
|
3315
|
|
|
|
|
|
|
|
|
3316
|
|
|
|
|
|
|
|
|
3317
|
|
|
|
|
|
|
=head2 hist |
|
3318
|
|
|
|
|
|
|
|
|
3319
|
|
|
|
|
|
|
Plot a hash of arrays as a series of histograms |
|
3320
|
|
|
|
|
|
|
|
|
3321
|
|
|
|
|
|
|
=head3 options |
|
3322
|
|
|
|
|
|
|
|
|
3323
|
|
|
|
|
|
|
=for html |
|
3324
|
|
|
|
|
|
|
|
3325
|
|
|
|
|
|
|
|
|
3326
|
|
|
|
|
|
|
| | Option | Description | Example |
|
3327
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
3328
|
|
|
|
|
|
|
| alpha | default 0.5; same for all sets | |
|
3329
|
|
|
|
|
|
|
| bins | # nt or sequence or str, default: :rc:hist.binsIf *bins* is an integer, it defines the number of equal-width bins in the range. If *bins* is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, bins may be unequally spaced. All but the last (righthand-most) bin is half-open | |
|
3330
|
|
|
|
|
|
|
| color | a hash, where keys are the keys in data, and values are colors | X => 'blue' |
|
3331
|
|
|
|
|
|
|
| log | if set to > 1, the y-axis will be logarithmic | |
|
3332
|
|
|
|
|
|
|
| orientation | {'vertical', 'horizontal'}, default: 'vertical' | |
|
3333
|
|
|
|
|
|
|
|
|
3334
|
|
|
|
|
|
|
| |
|
3335
|
|
|
|
|
|
|
|
|
3336
|
|
|
|
|
|
|
=head3 single, simple plot |
|
3337
|
|
|
|
|
|
|
|
|
3338
|
|
|
|
|
|
|
use Matplotlib::Simple 'hist'; |
|
3339
|
|
|
|
|
|
|
|
|
3340
|
|
|
|
|
|
|
my @e = generate_normal_dist( 100, 15, 3 * 200 ); |
|
3341
|
|
|
|
|
|
|
my @b = generate_normal_dist( 85, 15, 3 * 200 ); |
|
3342
|
|
|
|
|
|
|
my @a = generate_normal_dist( 105, 15, 3 * 200 ); |
|
3343
|
|
|
|
|
|
|
|
|
3344
|
|
|
|
|
|
|
hist({ |
|
3345
|
|
|
|
|
|
|
fh => $fh, |
|
3346
|
|
|
|
|
|
|
execute => 0, |
|
3347
|
|
|
|
|
|
|
'output.file' => 'output.images/single.hist.png', |
|
3348
|
|
|
|
|
|
|
data => { |
|
3349
|
|
|
|
|
|
|
E => @e, |
|
3350
|
|
|
|
|
|
|
B => @b, |
|
3351
|
|
|
|
|
|
|
A => @a, |
|
3352
|
|
|
|
|
|
|
} |
|
3353
|
|
|
|
|
|
|
}); |
|
3354
|
|
|
|
|
|
|
|
|
3355
|
|
|
|
|
|
|
which makes the following simple plot: |
|
3356
|
|
|
|
|
|
|
|
|
3357
|
|
|
|
|
|
|
|
|
3358
|
|
|
|
|
|
|
=for html |
|
3359
|
|
|
|
|
|
|
|
|
3360
|
|
|
|
|
|
|
|
|
3361
|
|
|
|
|
|
|
|
|
3362
|
|
|
|
|
|
|
|
|
3363
|
|
|
|
|
|
|
|
|
3364
|
|
|
|
|
|
|
=head3 multiple plots |
|
3365
|
|
|
|
|
|
|
|
|
3366
|
|
|
|
|
|
|
plt({ |
|
3367
|
|
|
|
|
|
|
fh => $fh, |
|
3368
|
|
|
|
|
|
|
execute => 0, |
|
3369
|
|
|
|
|
|
|
'output.file' => 'output.images/histogram.png', |
|
3370
|
|
|
|
|
|
|
set_figwidth => 15, |
|
3371
|
|
|
|
|
|
|
suptitle => 'hist Examples', |
|
3372
|
|
|
|
|
|
|
plots => [ |
|
3373
|
|
|
|
|
|
|
{ # 1st subplot |
|
3374
|
|
|
|
|
|
|
data => { |
|
3375
|
|
|
|
|
|
|
E => @e, |
|
3376
|
|
|
|
|
|
|
B => @b, |
|
3377
|
|
|
|
|
|
|
A => @a, |
|
3378
|
|
|
|
|
|
|
}, |
|
3379
|
|
|
|
|
|
|
'plot.type' => 'hist', |
|
3380
|
|
|
|
|
|
|
alpha => 0.25, |
|
3381
|
|
|
|
|
|
|
bins => 50, |
|
3382
|
|
|
|
|
|
|
title => 'alpha = 0.25', |
|
3383
|
|
|
|
|
|
|
color => { |
|
3384
|
|
|
|
|
|
|
B => 'Black', |
|
3385
|
|
|
|
|
|
|
E => 'Orange', |
|
3386
|
|
|
|
|
|
|
A => 'Yellow', |
|
3387
|
|
|
|
|
|
|
}, |
|
3388
|
|
|
|
|
|
|
scatter => '[' |
|
3389
|
|
|
|
|
|
|
. join( ',', 22 .. 44 ) . '],[' # x coords |
|
3390
|
|
|
|
|
|
|
. join( ',', 22 .. 44 ) # y coords |
|
3391
|
|
|
|
|
|
|
. '], label = "scatter"', |
|
3392
|
|
|
|
|
|
|
xlabel => 'Value', |
|
3393
|
|
|
|
|
|
|
ylabel => 'Frequency', |
|
3394
|
|
|
|
|
|
|
}, |
|
3395
|
|
|
|
|
|
|
{ # 2nd subplot |
|
3396
|
|
|
|
|
|
|
data => { |
|
3397
|
|
|
|
|
|
|
E => @e, |
|
3398
|
|
|
|
|
|
|
B => @b, |
|
3399
|
|
|
|
|
|
|
A => @a, |
|
3400
|
|
|
|
|
|
|
}, |
|
3401
|
|
|
|
|
|
|
'plot.type' => 'hist', |
|
3402
|
|
|
|
|
|
|
alpha => 0.75, |
|
3403
|
|
|
|
|
|
|
bins => 50, |
|
3404
|
|
|
|
|
|
|
title => 'alpha = 0.75', |
|
3405
|
|
|
|
|
|
|
color => { |
|
3406
|
|
|
|
|
|
|
B => 'Black', |
|
3407
|
|
|
|
|
|
|
E => 'Orange', |
|
3408
|
|
|
|
|
|
|
A => 'Yellow', |
|
3409
|
|
|
|
|
|
|
}, |
|
3410
|
|
|
|
|
|
|
xlabel => 'Value', |
|
3411
|
|
|
|
|
|
|
ylabel => 'Frequency', |
|
3412
|
|
|
|
|
|
|
}, |
|
3413
|
|
|
|
|
|
|
{ # 3rd subplot |
|
3414
|
|
|
|
|
|
|
add => [ # add secondary plots/graphs/methods |
|
3415
|
|
|
|
|
|
|
{ # 1st additional plot/graph |
|
3416
|
|
|
|
|
|
|
data => { |
|
3417
|
|
|
|
|
|
|
'Gaussian' => [ |
|
3418
|
|
|
|
|
|
|
[40..150], |
|
3419
|
|
|
|
|
|
|
[map {150 * exp(-0.5*($_-100)**2)} 40..150] |
|
3420
|
|
|
|
|
|
|
] |
|
3421
|
|
|
|
|
|
|
}, |
|
3422
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
3423
|
|
|
|
|
|
|
'set.options' => { |
|
3424
|
|
|
|
|
|
|
'Gaussian' => 'color = "red", linestyle = "dashed"' |
|
3425
|
|
|
|
|
|
|
} |
|
3426
|
|
|
|
|
|
|
} |
|
3427
|
|
|
|
|
|
|
], |
|
3428
|
|
|
|
|
|
|
data => { |
|
3429
|
|
|
|
|
|
|
E => @e, |
|
3430
|
|
|
|
|
|
|
B => @b, |
|
3431
|
|
|
|
|
|
|
A => @a, |
|
3432
|
|
|
|
|
|
|
}, |
|
3433
|
|
|
|
|
|
|
'plot.type' => 'hist', |
|
3434
|
|
|
|
|
|
|
alpha => 0.75, |
|
3435
|
|
|
|
|
|
|
bins => { |
|
3436
|
|
|
|
|
|
|
A => 10, |
|
3437
|
|
|
|
|
|
|
B => 25, |
|
3438
|
|
|
|
|
|
|
E => 50 |
|
3439
|
|
|
|
|
|
|
}, |
|
3440
|
|
|
|
|
|
|
title => 'Varying # of bins', |
|
3441
|
|
|
|
|
|
|
color => { |
|
3442
|
|
|
|
|
|
|
B => 'Black', |
|
3443
|
|
|
|
|
|
|
E => 'Orange', |
|
3444
|
|
|
|
|
|
|
A => 'Yellow', |
|
3445
|
|
|
|
|
|
|
}, |
|
3446
|
|
|
|
|
|
|
xlabel => 'Value', |
|
3447
|
|
|
|
|
|
|
ylabel => 'Frequency', |
|
3448
|
|
|
|
|
|
|
}, |
|
3449
|
|
|
|
|
|
|
{# 4th subplot |
|
3450
|
|
|
|
|
|
|
data => { |
|
3451
|
|
|
|
|
|
|
E => @e, |
|
3452
|
|
|
|
|
|
|
B => @b, |
|
3453
|
|
|
|
|
|
|
A => @a, |
|
3454
|
|
|
|
|
|
|
}, |
|
3455
|
|
|
|
|
|
|
'plot.type' => 'hist', |
|
3456
|
|
|
|
|
|
|
alpha => 0.75, |
|
3457
|
|
|
|
|
|
|
color => { |
|
3458
|
|
|
|
|
|
|
B => 'Black', |
|
3459
|
|
|
|
|
|
|
E => 'Orange', |
|
3460
|
|
|
|
|
|
|
A => 'Yellow', |
|
3461
|
|
|
|
|
|
|
}, |
|
3462
|
|
|
|
|
|
|
orientation => 'horizontal', # assign x and y labels smartly |
|
3463
|
|
|
|
|
|
|
title => 'Horizontal orientation', |
|
3464
|
|
|
|
|
|
|
ylabel => 'Value', |
|
3465
|
|
|
|
|
|
|
xlabel => 'Frequency', # 'log' => 1, |
|
3466
|
|
|
|
|
|
|
}, |
|
3467
|
|
|
|
|
|
|
], |
|
3468
|
|
|
|
|
|
|
ncols => 3, |
|
3469
|
|
|
|
|
|
|
nrows => 2, |
|
3470
|
|
|
|
|
|
|
}); |
|
3471
|
|
|
|
|
|
|
|
|
3472
|
|
|
|
|
|
|
|
|
3473
|
|
|
|
|
|
|
=for html |
|
3474
|
|
|
|
|
|
|
|
|
3475
|
|
|
|
|
|
|
|
|
3476
|
|
|
|
|
|
|
|
|
3477
|
|
|
|
|
|
|
|
|
3478
|
|
|
|
|
|
|
|
|
3479
|
|
|
|
|
|
|
Make a 2-D histogram from a hash of arrays |
|
3480
|
|
|
|
|
|
|
|
|
3481
|
|
|
|
|
|
|
=head2 hist2d |
|
3482
|
|
|
|
|
|
|
|
|
3483
|
|
|
|
|
|
|
=head3 single, simple plot |
|
3484
|
|
|
|
|
|
|
|
|
3485
|
|
|
|
|
|
|
plt({ |
|
3486
|
|
|
|
|
|
|
'output.file' => 'output.images/single.hist2d.png', |
|
3487
|
|
|
|
|
|
|
data => { |
|
3488
|
|
|
|
|
|
|
E => @e, |
|
3489
|
|
|
|
|
|
|
B => @b |
|
3490
|
|
|
|
|
|
|
}, |
|
3491
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3492
|
|
|
|
|
|
|
title => 'title', |
|
3493
|
|
|
|
|
|
|
execute => 0, |
|
3494
|
|
|
|
|
|
|
fh => $fh, |
|
3495
|
|
|
|
|
|
|
}); |
|
3496
|
|
|
|
|
|
|
|
|
3497
|
|
|
|
|
|
|
makes the following image: |
|
3498
|
|
|
|
|
|
|
|
|
3499
|
|
|
|
|
|
|
|
|
3500
|
|
|
|
|
|
|
=for html |
|
3501
|
|
|
|
|
|
|
|
|
3502
|
|
|
|
|
|
|
|
|
3503
|
|
|
|
|
|
|
|
|
3504
|
|
|
|
|
|
|
|
|
3505
|
|
|
|
|
|
|
|
|
3506
|
|
|
|
|
|
|
the range for the density min and max is reported to stdout |
|
3507
|
|
|
|
|
|
|
|
|
3508
|
|
|
|
|
|
|
=head3 options |
|
3509
|
|
|
|
|
|
|
|
|
3510
|
|
|
|
|
|
|
=for html |
|
3511
|
|
|
|
|
|
|
|
3512
|
|
|
|
|
|
|
|
|
3513
|
|
|
|
|
|
|
| | Option | Description | Example |
|
3514
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
3515
|
|
|
|
|
|
|
| cb_logscale | make the colorbar log-scale | cb_logscale => 1 |
|
3516
|
|
|
|
|
|
|
| cmap | color map for coloring # "gist_rainbow" by default | |
|
3517
|
|
|
|
|
|
|
| 'cmax', cmin | All bins that has count < *cmin* or > *cmax* will not be displayed | |
|
3518
|
|
|
|
|
|
|
| | 'density' | density : bool, default: False | |
|
3519
|
|
|
|
|
|
|
| | 'key.order' | define the keys in an order (an array reference) | |
|
3520
|
|
|
|
|
|
|
| | 'logscale' | # logscale, an array of axes that will get log scale | |
|
3521
|
|
|
|
|
|
|
| | 'show.colorbar' | self-evident, 0 or 1 | show.colorbar => 1 |
|
3522
|
|
|
|
|
|
|
| | 'vmax' | When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover | |
|
3523
|
|
|
|
|
|
|
| | 'vmin' | # When using scalar data and no explicit *norm*, *vmin* and *vmax* define the data range that the colormap cover | |
|
3524
|
|
|
|
|
|
|
| | 'xbins' | # default 15 | |
|
3525
|
|
|
|
|
|
|
| | 'xmin', 'xmax', | | |
|
3526
|
|
|
|
|
|
|
| | 'ymin', 'ymax', | | |
|
3527
|
|
|
|
|
|
|
| | 'ybins' | default 15 | |
|
3528
|
|
|
|
|
|
|
|
|
3529
|
|
|
|
|
|
|
| |
|
3530
|
|
|
|
|
|
|
|
|
3531
|
|
|
|
|
|
|
=head3 multiple plots |
|
3532
|
|
|
|
|
|
|
|
|
3533
|
|
|
|
|
|
|
plt({ |
|
3534
|
|
|
|
|
|
|
fh => $fh, |
|
3535
|
|
|
|
|
|
|
execute => 1, |
|
3536
|
|
|
|
|
|
|
ncols => 3, |
|
3537
|
|
|
|
|
|
|
nrows => 3, |
|
3538
|
|
|
|
|
|
|
suptitle => 'Types of hist2d plots: all of the data is identical', |
|
3539
|
|
|
|
|
|
|
plots => [ |
|
3540
|
|
|
|
|
|
|
{ |
|
3541
|
|
|
|
|
|
|
data => { |
|
3542
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3543
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3544
|
|
|
|
|
|
|
}, |
|
3545
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3546
|
|
|
|
|
|
|
title => 'Simple hist2d', |
|
3547
|
|
|
|
|
|
|
}, |
|
3548
|
|
|
|
|
|
|
{ |
|
3549
|
|
|
|
|
|
|
data => { |
|
3550
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3551
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3552
|
|
|
|
|
|
|
}, |
|
3553
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3554
|
|
|
|
|
|
|
title => 'cmap = terrain', |
|
3555
|
|
|
|
|
|
|
cmap => 'terrain' |
|
3556
|
|
|
|
|
|
|
}, |
|
3557
|
|
|
|
|
|
|
{ |
|
3558
|
|
|
|
|
|
|
cmap => 'ocean', |
|
3559
|
|
|
|
|
|
|
data => { |
|
3560
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3561
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3562
|
|
|
|
|
|
|
}, |
|
3563
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3564
|
|
|
|
|
|
|
title => 'cmap = ocean and set colorbar range with vmin/vmax', |
|
3565
|
|
|
|
|
|
|
set_figwidth => 15, |
|
3566
|
|
|
|
|
|
|
vmin => -2, |
|
3567
|
|
|
|
|
|
|
vmax => 14 |
|
3568
|
|
|
|
|
|
|
}, |
|
3569
|
|
|
|
|
|
|
{ |
|
3570
|
|
|
|
|
|
|
data => { |
|
3571
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3572
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3573
|
|
|
|
|
|
|
}, |
|
3574
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3575
|
|
|
|
|
|
|
title => 'density = True', |
|
3576
|
|
|
|
|
|
|
cmap => 'terrain', |
|
3577
|
|
|
|
|
|
|
density => 'True' |
|
3578
|
|
|
|
|
|
|
}, |
|
3579
|
|
|
|
|
|
|
{ |
|
3580
|
|
|
|
|
|
|
data => { |
|
3581
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3582
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3583
|
|
|
|
|
|
|
}, |
|
3584
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3585
|
|
|
|
|
|
|
title => 'key.order flips axes', |
|
3586
|
|
|
|
|
|
|
cmap => 'terrain', |
|
3587
|
|
|
|
|
|
|
'key.order' => [ 'Y', 'X' ] |
|
3588
|
|
|
|
|
|
|
}, |
|
3589
|
|
|
|
|
|
|
{ |
|
3590
|
|
|
|
|
|
|
cb_logscale => 1, |
|
3591
|
|
|
|
|
|
|
data => { |
|
3592
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3593
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3594
|
|
|
|
|
|
|
}, |
|
3595
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3596
|
|
|
|
|
|
|
title => 'cb_logscale = 1', |
|
3597
|
|
|
|
|
|
|
}, |
|
3598
|
|
|
|
|
|
|
{ |
|
3599
|
|
|
|
|
|
|
cb_logscale => 1, |
|
3600
|
|
|
|
|
|
|
data => { |
|
3601
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3602
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3603
|
|
|
|
|
|
|
}, |
|
3604
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3605
|
|
|
|
|
|
|
title => 'cb_logscale = 1 with vmax set', |
|
3606
|
|
|
|
|
|
|
vmax => 2.1, |
|
3607
|
|
|
|
|
|
|
vmin => 1 |
|
3608
|
|
|
|
|
|
|
}, |
|
3609
|
|
|
|
|
|
|
{ |
|
3610
|
|
|
|
|
|
|
data => { |
|
3611
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3612
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3613
|
|
|
|
|
|
|
}, |
|
3614
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3615
|
|
|
|
|
|
|
'show.colorbar' => 0, |
|
3616
|
|
|
|
|
|
|
title => 'no colorbar', |
|
3617
|
|
|
|
|
|
|
}, |
|
3618
|
|
|
|
|
|
|
{ |
|
3619
|
|
|
|
|
|
|
data => { |
|
3620
|
|
|
|
|
|
|
X => $x, # x-axis |
|
3621
|
|
|
|
|
|
|
Y => $y, # y-axis |
|
3622
|
|
|
|
|
|
|
}, |
|
3623
|
|
|
|
|
|
|
'plot.type' => 'hist2d', |
|
3624
|
|
|
|
|
|
|
title => 'xbins = 9', |
|
3625
|
|
|
|
|
|
|
xbins => 9 |
|
3626
|
|
|
|
|
|
|
}, |
|
3627
|
|
|
|
|
|
|
], |
|
3628
|
|
|
|
|
|
|
'output.file' => 'output.images/hist2d.png', |
|
3629
|
|
|
|
|
|
|
}); |
|
3630
|
|
|
|
|
|
|
|
|
3631
|
|
|
|
|
|
|
makes the following image: |
|
3632
|
|
|
|
|
|
|
|
|
3633
|
|
|
|
|
|
|
|
|
3634
|
|
|
|
|
|
|
=for html |
|
3635
|
|
|
|
|
|
|
|
|
3636
|
|
|
|
|
|
|
|
|
3637
|
|
|
|
|
|
|
|
|
3638
|
|
|
|
|
|
|
|
|
3639
|
|
|
|
|
|
|
|
|
3640
|
|
|
|
|
|
|
=head2 imshow |
|
3641
|
|
|
|
|
|
|
|
|
3642
|
|
|
|
|
|
|
Plot 2D array of numbers as an image |
|
3643
|
|
|
|
|
|
|
|
|
3644
|
|
|
|
|
|
|
=head3 options |
|
3645
|
|
|
|
|
|
|
|
|
3646
|
|
|
|
|
|
|
=for html |
|
3647
|
|
|
|
|
|
|
|
3648
|
|
|
|
|
|
|
|
|
3649
|
|
|
|
|
|
|
| | Option | Description | Example |
|
3650
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
3651
|
|
|
|
|
|
|
| cblabel | colorbar label | cblabel => 'sin(x) * cos(x)', |
|
3652
|
|
|
|
|
|
|
| cbdrawedges | draw edges for colorbar | |
|
3653
|
|
|
|
|
|
|
| cblocation | 'left', 'right', 'top', 'bottom' | cblocation => 'left', |
|
3654
|
|
|
|
|
|
|
| cborientation | None, or 'vertical', 'horizontal' | |
|
3655
|
|
|
|
|
|
|
| cmap | # The Colormap instance or registered colormap name used to map scalar data to colors. | |
|
3656
|
|
|
|
|
|
|
| vmax | float | |
|
3657
|
|
|
|
|
|
|
| vmin | float | |
|
3658
|
|
|
|
|
|
|
|
|
3659
|
|
|
|
|
|
|
| |
|
3660
|
|
|
|
|
|
|
|
|
3661
|
|
|
|
|
|
|
=head3 single, simple plot |
|
3662
|
|
|
|
|
|
|
|
|
3663
|
|
|
|
|
|
|
my @imshow_data; |
|
3664
|
|
|
|
|
|
|
foreach my $i (0..360) { |
|
3665
|
|
|
|
|
|
|
foreach my $j (0..360) { |
|
3666
|
|
|
|
|
|
|
push @{ $imshow_data[$i] }, sin($i * $pi/180)*cos($j * $pi/180); |
|
3667
|
|
|
|
|
|
|
} |
|
3668
|
|
|
|
|
|
|
} |
|
3669
|
|
|
|
|
|
|
plt({ |
|
3670
|
|
|
|
|
|
|
data => \@imshow_data, |
|
3671
|
|
|
|
|
|
|
execute => 0, |
|
3672
|
|
|
|
|
|
|
fh => $fh, |
|
3673
|
|
|
|
|
|
|
'output.file' => 'output.images/imshow.single.png', |
|
3674
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3675
|
|
|
|
|
|
|
set_xlim => '0, ' . scalar @imshow_data, |
|
3676
|
|
|
|
|
|
|
set_ylim => '0, ' . scalar @imshow_data, |
|
3677
|
|
|
|
|
|
|
}); |
|
3678
|
|
|
|
|
|
|
|
|
3679
|
|
|
|
|
|
|
which makes the following image: |
|
3680
|
|
|
|
|
|
|
|
|
3681
|
|
|
|
|
|
|
|
|
3682
|
|
|
|
|
|
|
=for html |
|
3683
|
|
|
|
|
|
|
|
|
3684
|
|
|
|
|
|
|
|
|
3685
|
|
|
|
|
|
|
|
|
3686
|
|
|
|
|
|
|
|
|
3687
|
|
|
|
|
|
|
|
|
3688
|
|
|
|
|
|
|
=head3 multiple plots |
|
3689
|
|
|
|
|
|
|
|
|
3690
|
|
|
|
|
|
|
plt({ |
|
3691
|
|
|
|
|
|
|
plots => [ |
|
3692
|
|
|
|
|
|
|
{ |
|
3693
|
|
|
|
|
|
|
data => \@imshow_data, |
|
3694
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3695
|
|
|
|
|
|
|
set_xlim => '0, ' . scalar @imshow_data, |
|
3696
|
|
|
|
|
|
|
set_ylim => '0, ' . scalar @imshow_data, |
|
3697
|
|
|
|
|
|
|
title => 'basic', |
|
3698
|
|
|
|
|
|
|
}, |
|
3699
|
|
|
|
|
|
|
{ |
|
3700
|
|
|
|
|
|
|
cblabel => 'sin(x) * cos(x)', |
|
3701
|
|
|
|
|
|
|
data => \@imshow_data, |
|
3702
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3703
|
|
|
|
|
|
|
set_xlim => '0, ' . scalar @imshow_data, |
|
3704
|
|
|
|
|
|
|
set_ylim => '0, ' . scalar @imshow_data, |
|
3705
|
|
|
|
|
|
|
title => 'cblabel', |
|
3706
|
|
|
|
|
|
|
}, |
|
3707
|
|
|
|
|
|
|
{ |
|
3708
|
|
|
|
|
|
|
cblabel => 'sin(x) * cos(x)', |
|
3709
|
|
|
|
|
|
|
cblocation => 'left', |
|
3710
|
|
|
|
|
|
|
data => \@imshow_data, |
|
3711
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3712
|
|
|
|
|
|
|
set_xlim => '0, ' . scalar @imshow_data, |
|
3713
|
|
|
|
|
|
|
set_ylim => '0, ' . scalar @imshow_data, |
|
3714
|
|
|
|
|
|
|
title => 'cblocation = left', |
|
3715
|
|
|
|
|
|
|
}, |
|
3716
|
|
|
|
|
|
|
{ |
|
3717
|
|
|
|
|
|
|
cblabel => 'sin(x) * cos(x)', |
|
3718
|
|
|
|
|
|
|
data => \@imshow_data, |
|
3719
|
|
|
|
|
|
|
add => [ # add secondary plots |
|
3720
|
|
|
|
|
|
|
{ # 1st additional plot |
|
3721
|
|
|
|
|
|
|
data => { |
|
3722
|
|
|
|
|
|
|
'sin(x)' => [ |
|
3723
|
|
|
|
|
|
|
[0..360], |
|
3724
|
|
|
|
|
|
|
[map {180 + 180*sin($_ * $pi/180)} 0..360] |
|
3725
|
|
|
|
|
|
|
], |
|
3726
|
|
|
|
|
|
|
'cos(x)' => [ |
|
3727
|
|
|
|
|
|
|
[0..360], |
|
3728
|
|
|
|
|
|
|
[map {180 + 180*cos($_ * $pi/180)} 0..360] |
|
3729
|
|
|
|
|
|
|
], |
|
3730
|
|
|
|
|
|
|
}, |
|
3731
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
3732
|
|
|
|
|
|
|
'set.options' => { |
|
3733
|
|
|
|
|
|
|
'sin(x)' => 'color = "red", linestyle = "dashed"', |
|
3734
|
|
|
|
|
|
|
'cos(x)' => 'color = "blue", linestyle = "dashed"', |
|
3735
|
|
|
|
|
|
|
} |
|
3736
|
|
|
|
|
|
|
} |
|
3737
|
|
|
|
|
|
|
], |
|
3738
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3739
|
|
|
|
|
|
|
set_xlim => '0, ' . scalar @imshow_data, |
|
3740
|
|
|
|
|
|
|
set_ylim => '0, ' . scalar @imshow_data, |
|
3741
|
|
|
|
|
|
|
title => 'auxiliary plots', |
|
3742
|
|
|
|
|
|
|
}, |
|
3743
|
|
|
|
|
|
|
], |
|
3744
|
|
|
|
|
|
|
execute => 0, |
|
3745
|
|
|
|
|
|
|
fh => $fh, |
|
3746
|
|
|
|
|
|
|
'output.file' => 'output.images/imshow.multiple.png', |
|
3747
|
|
|
|
|
|
|
ncols => 2, |
|
3748
|
|
|
|
|
|
|
nrows => 2, |
|
3749
|
|
|
|
|
|
|
set_figheight => 6*3,# 4.8 |
|
3750
|
|
|
|
|
|
|
set_figwidth => 6*4 # 6.4 |
|
3751
|
|
|
|
|
|
|
}); |
|
3752
|
|
|
|
|
|
|
|
|
3753
|
|
|
|
|
|
|
which makes the following image: |
|
3754
|
|
|
|
|
|
|
|
|
3755
|
|
|
|
|
|
|
|
|
3756
|
|
|
|
|
|
|
=for html |
|
3757
|
|
|
|
|
|
|
|
|
3758
|
|
|
|
|
|
|
|
|
3759
|
|
|
|
|
|
|
|
|
3760
|
|
|
|
|
|
|
|
|
3761
|
|
|
|
|
|
|
|
|
3762
|
|
|
|
|
|
|
=head3 Secondary Structure Prediction (DSSP) |
|
3763
|
|
|
|
|
|
|
|
|
3764
|
|
|
|
|
|
|
Sometimes strings instead of numbers can be entered into a 2-D array, one example is protein secondary structure. |
|
3765
|
|
|
|
|
|
|
Protein secondary structure can be plotted thus, with a key in C to show which strings become which integers in a minimal working example: |
|
3766
|
|
|
|
|
|
|
|
|
3767
|
|
|
|
|
|
|
plt({ |
|
3768
|
|
|
|
|
|
|
cbpad => 0.01, # default 0.05 is too big |
|
3769
|
|
|
|
|
|
|
data => [ # imshow gets a 2D array |
|
3770
|
|
|
|
|
|
|
[' ', ' ', ' ', ' ', 'G'], # bottom |
|
3771
|
|
|
|
|
|
|
['S', 'I', 'T', 'E', 'H'], # top |
|
3772
|
|
|
|
|
|
|
], |
|
3773
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3774
|
|
|
|
|
|
|
stringmap => { |
|
3775
|
|
|
|
|
|
|
'H' => 'Alpha helix', |
|
3776
|
|
|
|
|
|
|
'B' => 'Residue in isolated β-bridge', |
|
3777
|
|
|
|
|
|
|
'E' => 'Extended strand, participates in β ladder', |
|
3778
|
|
|
|
|
|
|
'G' => '3-helix (3/10 helix)', |
|
3779
|
|
|
|
|
|
|
'I' => '5 helix (pi helix)', |
|
3780
|
|
|
|
|
|
|
'T' => 'hydrogen bonded turn', |
|
3781
|
|
|
|
|
|
|
'S' => 'bend', |
|
3782
|
|
|
|
|
|
|
' ' => 'Loops and irregular elements' |
|
3783
|
|
|
|
|
|
|
}, |
|
3784
|
|
|
|
|
|
|
'output.file' => 'output.images/dssp.single.png', |
|
3785
|
|
|
|
|
|
|
scalex => 2.4, |
|
3786
|
|
|
|
|
|
|
set_ylim => '0, 1', |
|
3787
|
|
|
|
|
|
|
title => 'Dictionary of Secondary Structure in Proteins (DSSP)', |
|
3788
|
|
|
|
|
|
|
xlabel => 'xlabel', |
|
3789
|
|
|
|
|
|
|
ylabel => 'ylabel' |
|
3790
|
|
|
|
|
|
|
}); |
|
3791
|
|
|
|
|
|
|
|
|
3792
|
|
|
|
|
|
|
|
|
3793
|
|
|
|
|
|
|
=for html |
|
3794
|
|
|
|
|
|
|
|
|
3795
|
|
|
|
|
|
|
|
|
3796
|
|
|
|
|
|
|
|
|
3797
|
|
|
|
|
|
|
|
|
3798
|
|
|
|
|
|
|
|
|
3799
|
|
|
|
|
|
|
or for multiple plots, where the colorbar can be spread across multiple plots now: |
|
3800
|
|
|
|
|
|
|
|
|
3801
|
|
|
|
|
|
|
plt({ |
|
3802
|
|
|
|
|
|
|
cbpad => 0.01, # default 0.05 is too big |
|
3803
|
|
|
|
|
|
|
plots => [ |
|
3804
|
|
|
|
|
|
|
{ # 1st plot |
|
3805
|
|
|
|
|
|
|
data => [ |
|
3806
|
|
|
|
|
|
|
[' ', ' ', ' ', ' ', 'G'], # bottom |
|
3807
|
|
|
|
|
|
|
['S', 'I', 'T', 'E', 'H'], # top |
|
3808
|
|
|
|
|
|
|
], |
|
3809
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3810
|
|
|
|
|
|
|
set_xticklabels=> '[]', # remove x-axis labels |
|
3811
|
|
|
|
|
|
|
set_ylim => '0, 1', |
|
3812
|
|
|
|
|
|
|
stringmap => { |
|
3813
|
|
|
|
|
|
|
'H' => 'Alpha helix', |
|
3814
|
|
|
|
|
|
|
'B' => 'Residue in isolated β-bridge', |
|
3815
|
|
|
|
|
|
|
'E' => 'Extended strand, participates in β ladder', |
|
3816
|
|
|
|
|
|
|
'G' => '3-helix (3/10 helix)', |
|
3817
|
|
|
|
|
|
|
'I' => '5 helix (pi helix)', |
|
3818
|
|
|
|
|
|
|
'T' => 'hydrogen bonded turn', |
|
3819
|
|
|
|
|
|
|
'S' => 'bend', |
|
3820
|
|
|
|
|
|
|
' ' => 'Loops and irregular elements' |
|
3821
|
|
|
|
|
|
|
}, |
|
3822
|
|
|
|
|
|
|
title => 'top plot', |
|
3823
|
|
|
|
|
|
|
ylabel => 'ylabel' |
|
3824
|
|
|
|
|
|
|
}, |
|
3825
|
|
|
|
|
|
|
{ # 2nd plot |
|
3826
|
|
|
|
|
|
|
data => [ |
|
3827
|
|
|
|
|
|
|
[' ', ' ', ' ', ' ', 'G'], # bottom |
|
3828
|
|
|
|
|
|
|
['S', 'I', 'T', 'E', 'H'], # top |
|
3829
|
|
|
|
|
|
|
], |
|
3830
|
|
|
|
|
|
|
'plot.type' => 'imshow', |
|
3831
|
|
|
|
|
|
|
set_ylim => '0, 1', |
|
3832
|
|
|
|
|
|
|
stringmap => { |
|
3833
|
|
|
|
|
|
|
'H' => 'Alpha helix', |
|
3834
|
|
|
|
|
|
|
'B' => 'Residue in isolated β-bridge', |
|
3835
|
|
|
|
|
|
|
'E' => 'Extended strand, participates in β ladder', |
|
3836
|
|
|
|
|
|
|
'G' => '3-helix (3/10 helix)', |
|
3837
|
|
|
|
|
|
|
'I' => '5 helix (pi helix)', |
|
3838
|
|
|
|
|
|
|
'T' => 'hydrogen bonded turn', |
|
3839
|
|
|
|
|
|
|
'S' => 'bend', |
|
3840
|
|
|
|
|
|
|
' ' => 'Loops and irregular elements' |
|
3841
|
|
|
|
|
|
|
}, |
|
3842
|
|
|
|
|
|
|
title => 'bottom plot', |
|
3843
|
|
|
|
|
|
|
xlabel => 'xlabel', |
|
3844
|
|
|
|
|
|
|
ylabel => 'ylabel' |
|
3845
|
|
|
|
|
|
|
} |
|
3846
|
|
|
|
|
|
|
], |
|
3847
|
|
|
|
|
|
|
nrows => 2, |
|
3848
|
|
|
|
|
|
|
'output.file' => 'output.images/dssp.multiple.png', |
|
3849
|
|
|
|
|
|
|
scalex => 2.4, |
|
3850
|
|
|
|
|
|
|
'shared.colorbar' => [0,1], # plots 0 and 1 share a colorbar |
|
3851
|
|
|
|
|
|
|
suptitle => 'Dictionary of Secondary Structure in Proteins (DSSP)', |
|
3852
|
|
|
|
|
|
|
}); |
|
3853
|
|
|
|
|
|
|
|
|
3854
|
|
|
|
|
|
|
which makes the following plot: |
|
3855
|
|
|
|
|
|
|
|
|
3856
|
|
|
|
|
|
|
|
|
3857
|
|
|
|
|
|
|
=for html |
|
3858
|
|
|
|
|
|
|
|
|
3859
|
|
|
|
|
|
|
|
|
3860
|
|
|
|
|
|
|
|
|
3861
|
|
|
|
|
|
|
|
|
3862
|
|
|
|
|
|
|
|
|
3863
|
|
|
|
|
|
|
=head2 pie |
|
3864
|
|
|
|
|
|
|
|
|
3865
|
|
|
|
|
|
|
=head3 options |
|
3866
|
|
|
|
|
|
|
|
|
3867
|
|
|
|
|
|
|
=head3 single, simple plot |
|
3868
|
|
|
|
|
|
|
|
|
3869
|
|
|
|
|
|
|
plt({ |
|
3870
|
|
|
|
|
|
|
'output.file' => 'output.images/single.pie.png', |
|
3871
|
|
|
|
|
|
|
data => { # simple hash |
|
3872
|
|
|
|
|
|
|
Fri => 76, |
|
3873
|
|
|
|
|
|
|
Mon => 73, |
|
3874
|
|
|
|
|
|
|
Sat => 26, |
|
3875
|
|
|
|
|
|
|
Sun => 11, |
|
3876
|
|
|
|
|
|
|
Thu => 94, |
|
3877
|
|
|
|
|
|
|
Tue => 93, |
|
3878
|
|
|
|
|
|
|
Wed => 77 |
|
3879
|
|
|
|
|
|
|
}, |
|
3880
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
3881
|
|
|
|
|
|
|
title => 'Single Simple Pie', |
|
3882
|
|
|
|
|
|
|
fh => $fh, |
|
3883
|
|
|
|
|
|
|
execute => 0, |
|
3884
|
|
|
|
|
|
|
}); |
|
3885
|
|
|
|
|
|
|
|
|
3886
|
|
|
|
|
|
|
which makes the image: |
|
3887
|
|
|
|
|
|
|
|
|
3888
|
|
|
|
|
|
|
|
|
3889
|
|
|
|
|
|
|
=for html |
|
3890
|
|
|
|
|
|
|
|
|
3891
|
|
|
|
|
|
|
|
|
3892
|
|
|
|
|
|
|
|
|
3893
|
|
|
|
|
|
|
|
|
3894
|
|
|
|
|
|
|
|
|
3895
|
|
|
|
|
|
|
=head3 multiple plots |
|
3896
|
|
|
|
|
|
|
|
|
3897
|
|
|
|
|
|
|
plt({ |
|
3898
|
|
|
|
|
|
|
'output.file' => 'output.images/pie.png', |
|
3899
|
|
|
|
|
|
|
plots => [ |
|
3900
|
|
|
|
|
|
|
{ |
|
3901
|
|
|
|
|
|
|
data => { |
|
3902
|
|
|
|
|
|
|
'Russian' => 106_000_000, # Primarily European Russia |
|
3903
|
|
|
|
|
|
|
'German' => |
|
3904
|
|
|
|
|
|
|
95_000_000, # Germany, Austria, Switzerland, etc. |
|
3905
|
|
|
|
|
|
|
'English' => 70_000_000, # UK, Ireland, etc. |
|
3906
|
|
|
|
|
|
|
'French' => 66_000_000, # France, Belgium, Switzerland, etc. |
|
3907
|
|
|
|
|
|
|
'Italian' => 59_000_000, # Italy, Switzerland, etc. |
|
3908
|
|
|
|
|
|
|
'Spanish' => 45_000_000, # Spain |
|
3909
|
|
|
|
|
|
|
'Polish' => 38_000_000, # Poland |
|
3910
|
|
|
|
|
|
|
'Ukrainian' => 32_000_000, # Ukraine |
|
3911
|
|
|
|
|
|
|
'Romanian' => 24_000_000, # Romania, Moldova |
|
3912
|
|
|
|
|
|
|
'Dutch' => 22_000_000 # Netherlands, Belgium |
|
3913
|
|
|
|
|
|
|
}, |
|
3914
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
3915
|
|
|
|
|
|
|
title => 'Top Languages in Europe', |
|
3916
|
|
|
|
|
|
|
suptitle => 'Pie in subplots', |
|
3917
|
|
|
|
|
|
|
}, |
|
3918
|
|
|
|
|
|
|
{ |
|
3919
|
|
|
|
|
|
|
data => { |
|
3920
|
|
|
|
|
|
|
'Russian' => 106_000_000, # Primarily European Russia |
|
3921
|
|
|
|
|
|
|
'German' => |
|
3922
|
|
|
|
|
|
|
95_000_000, # Germany, Austria, Switzerland, etc. |
|
3923
|
|
|
|
|
|
|
'English' => 70_000_000, # UK, Ireland, etc. |
|
3924
|
|
|
|
|
|
|
'French' => 66_000_000, # France, Belgium, Switzerland, etc. |
|
3925
|
|
|
|
|
|
|
'Italian' => 59_000_000, # Italy, Switzerland, etc. |
|
3926
|
|
|
|
|
|
|
'Spanish' => 45_000_000, # Spain |
|
3927
|
|
|
|
|
|
|
'Polish' => 38_000_000, # Poland |
|
3928
|
|
|
|
|
|
|
'Ukrainian' => 32_000_000, # Ukraine |
|
3929
|
|
|
|
|
|
|
'Romanian' => 24_000_000, # Romania, Moldova |
|
3930
|
|
|
|
|
|
|
'Dutch' => 22_000_000 # Netherlands, Belgium |
|
3931
|
|
|
|
|
|
|
}, |
|
3932
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
3933
|
|
|
|
|
|
|
title => 'Top Languages in Europe', |
|
3934
|
|
|
|
|
|
|
autopct => '%1.1f%%', |
|
3935
|
|
|
|
|
|
|
}, |
|
3936
|
|
|
|
|
|
|
{ |
|
3937
|
|
|
|
|
|
|
data => { |
|
3938
|
|
|
|
|
|
|
'United States' => 86, |
|
3939
|
|
|
|
|
|
|
'United Kingdom' => 33, |
|
3940
|
|
|
|
|
|
|
'Germany' => 29, |
|
3941
|
|
|
|
|
|
|
'France' => 10, |
|
3942
|
|
|
|
|
|
|
'Japan' => 7, |
|
3943
|
|
|
|
|
|
|
'Israel' => 6, |
|
3944
|
|
|
|
|
|
|
}, |
|
3945
|
|
|
|
|
|
|
title => 'Chem. Nobels: swap text positions', |
|
3946
|
|
|
|
|
|
|
'plot.type' => 'pie', |
|
3947
|
|
|
|
|
|
|
autopct => '%1.1f%%', |
|
3948
|
|
|
|
|
|
|
pctdistance => 1.25, |
|
3949
|
|
|
|
|
|
|
labeldistance => 0.6, |
|
3950
|
|
|
|
|
|
|
} |
|
3951
|
|
|
|
|
|
|
], |
|
3952
|
|
|
|
|
|
|
fh => $fh, |
|
3953
|
|
|
|
|
|
|
execute => 0, |
|
3954
|
|
|
|
|
|
|
set_figwidth => 12, |
|
3955
|
|
|
|
|
|
|
ncols => 3, |
|
3956
|
|
|
|
|
|
|
}); |
|
3957
|
|
|
|
|
|
|
|
|
3958
|
|
|
|
|
|
|
|
|
3959
|
|
|
|
|
|
|
=for html |
|
3960
|
|
|
|
|
|
|
|
|
3961
|
|
|
|
|
|
|
|
|
3962
|
|
|
|
|
|
|
|
|
3963
|
|
|
|
|
|
|
|
|
3964
|
|
|
|
|
|
|
|
|
3965
|
|
|
|
|
|
|
=head2 plot |
|
3966
|
|
|
|
|
|
|
|
|
3967
|
|
|
|
|
|
|
plot either a hash of arrays or an array of arrays |
|
3968
|
|
|
|
|
|
|
|
|
3969
|
|
|
|
|
|
|
=head3 single, simple |
|
3970
|
|
|
|
|
|
|
|
|
3971
|
|
|
|
|
|
|
data can be given as a hash, where the hash key is the label: |
|
3972
|
|
|
|
|
|
|
|
|
3973
|
|
|
|
|
|
|
plt({ |
|
3974
|
|
|
|
|
|
|
fh => $fh, |
|
3975
|
|
|
|
|
|
|
execute => 0, |
|
3976
|
|
|
|
|
|
|
'output.file' => 'output.images/plot.single.png', |
|
3977
|
|
|
|
|
|
|
data => { |
|
3978
|
|
|
|
|
|
|
'sin(x)' => [ |
|
3979
|
|
|
|
|
|
|
[@x], # x |
|
3980
|
|
|
|
|
|
|
[ map { sin($_) } @x ] # y |
|
3981
|
|
|
|
|
|
|
], |
|
3982
|
|
|
|
|
|
|
'cos(x)' => [ |
|
3983
|
|
|
|
|
|
|
[@x], # x |
|
3984
|
|
|
|
|
|
|
[ map { cos($_) } @x ] # y |
|
3985
|
|
|
|
|
|
|
], |
|
3986
|
|
|
|
|
|
|
}, |
|
3987
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
3988
|
|
|
|
|
|
|
title => 'simple plot', |
|
3989
|
|
|
|
|
|
|
set_xticks => |
|
3990
|
|
|
|
|
|
|
"[-2 * $pi, -3 * $pi / 2, -$pi, -$pi / 2, 0, $pi / 2, $pi, 3 * $pi / 2, 2 * $pi" |
|
3991
|
|
|
|
|
|
|
. '], [r\'$-2\pi$\', r\'$-3\pi/2$\', r\'$-\pi$\', r\'$-\pi/2$\', r\'$0$\', r\'$\pi/2$\', r\'$\pi$\', r\'$3\pi/2$\', r\'$2\pi$\']', |
|
3992
|
|
|
|
|
|
|
'set.options' => { # set options overrides global settings |
|
3993
|
|
|
|
|
|
|
'sin(x)' => 'color="blue", linewidth=2', |
|
3994
|
|
|
|
|
|
|
'cos(x)' => 'color="red", linewidth=2' |
|
3995
|
|
|
|
|
|
|
} |
|
3996
|
|
|
|
|
|
|
}); |
|
3997
|
|
|
|
|
|
|
|
|
3998
|
|
|
|
|
|
|
or as an array of arrays: |
|
3999
|
|
|
|
|
|
|
|
|
4000
|
|
|
|
|
|
|
plt({ |
|
4001
|
|
|
|
|
|
|
fh => $fh, |
|
4002
|
|
|
|
|
|
|
execute => 0, |
|
4003
|
|
|
|
|
|
|
'output.file' => 'output.images/plot.single.arr.png', |
|
4004
|
|
|
|
|
|
|
data => [ |
|
4005
|
|
|
|
|
|
|
[ |
|
4006
|
|
|
|
|
|
|
[@x], # x |
|
4007
|
|
|
|
|
|
|
[ map { sin($_) } @x ] # y |
|
4008
|
|
|
|
|
|
|
], |
|
4009
|
|
|
|
|
|
|
[ |
|
4010
|
|
|
|
|
|
|
[@x], # x |
|
4011
|
|
|
|
|
|
|
[ map { cos($_) } @x ] # y |
|
4012
|
|
|
|
|
|
|
], |
|
4013
|
|
|
|
|
|
|
], |
|
4014
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4015
|
|
|
|
|
|
|
title => 'simple plot', |
|
4016
|
|
|
|
|
|
|
set_xticks => |
|
4017
|
|
|
|
|
|
|
"[-2 * $pi, -3 * $pi / 2, -$pi, -$pi / 2, 0, $pi / 2, $pi, 3 * $pi / 2, 2 * $pi" |
|
4018
|
|
|
|
|
|
|
. '], [r\'$-2\pi$\', r\'$-3\pi/2$\', r\'$-\pi$\', r\'$-\pi/2$\', r\'$0$\', r\'$\pi/2$\', r\'$\pi$\', r\'$3\pi/2$\', r\'$2\pi$\']', |
|
4019
|
|
|
|
|
|
|
'set.options' => [ # set options overrides global settings; indices match data array |
|
4020
|
|
|
|
|
|
|
'color="blue", linewidth=2, label = "sin(x)"', # labels aren't added automatically when using array here |
|
4021
|
|
|
|
|
|
|
'color="red", linewidth=2, label = "cos(x)"' |
|
4022
|
|
|
|
|
|
|
], |
|
4023
|
|
|
|
|
|
|
}); |
|
4024
|
|
|
|
|
|
|
|
|
4025
|
|
|
|
|
|
|
both of which make the following "plot" plot: |
|
4026
|
|
|
|
|
|
|
|
|
4027
|
|
|
|
|
|
|
|
|
4028
|
|
|
|
|
|
|
=for html |
|
4029
|
|
|
|
|
|
|
|
|
4030
|
|
|
|
|
|
|
|
|
4031
|
|
|
|
|
|
|
|
|
4032
|
|
|
|
|
|
|
|
|
4033
|
|
|
|
|
|
|
|
|
4034
|
|
|
|
|
|
|
=head3 multiple sub-plots |
|
4035
|
|
|
|
|
|
|
|
|
4036
|
|
|
|
|
|
|
which makes |
|
4037
|
|
|
|
|
|
|
|
|
4038
|
|
|
|
|
|
|
my $epsilon = 10**-7; |
|
4039
|
|
|
|
|
|
|
my (%set_opt, %d); |
|
4040
|
|
|
|
|
|
|
my $i = 0; |
|
4041
|
|
|
|
|
|
|
foreach my $interval ( |
|
4042
|
|
|
|
|
|
|
[-2*$pi, -$pi], |
|
4043
|
|
|
|
|
|
|
[-$pi, 0], |
|
4044
|
|
|
|
|
|
|
[0, $pi], |
|
4045
|
|
|
|
|
|
|
[$pi, 2*$pi] |
|
4046
|
|
|
|
|
|
|
) { |
|
4047
|
|
|
|
|
|
|
my @th = linspace($interval->[0] + $epsilon, $interval->[1] - $epsilon, 99, 0); |
|
4048
|
|
|
|
|
|
|
@{ $d{csc}{$i}[0] } = @th; |
|
4049
|
|
|
|
|
|
|
@{ $d{csc}{$i}[1] } = map { 1/sin($_) } @th; |
|
4050
|
|
|
|
|
|
|
@{ $d{cot}{$i}[0] } = @th; |
|
4051
|
|
|
|
|
|
|
@{ $d{cot}{$i}[1] } = map { cos($_)/sin($_) } @th; |
|
4052
|
|
|
|
|
|
|
if ($i == 0) { |
|
4053
|
|
|
|
|
|
|
$set_opt{csc}{$i} = 'color = "red", label = "csc(θ)"'; |
|
4054
|
|
|
|
|
|
|
$set_opt{cot}{$i} = 'color = "violet", label = "cot(θ)"'; |
|
4055
|
|
|
|
|
|
|
} else { |
|
4056
|
|
|
|
|
|
|
$set_opt{csc}{$i} = 'color = "red"'; |
|
4057
|
|
|
|
|
|
|
$set_opt{cot}{$i} = 'color = "violet"'; |
|
4058
|
|
|
|
|
|
|
} |
|
4059
|
|
|
|
|
|
|
$i++; |
|
4060
|
|
|
|
|
|
|
} |
|
4061
|
|
|
|
|
|
|
$i = 0; |
|
4062
|
|
|
|
|
|
|
foreach my $interval ( |
|
4063
|
|
|
|
|
|
|
[-2 * $pi, -1.5 * $pi], |
|
4064
|
|
|
|
|
|
|
[-1.5*$pi, -0.5*$pi], |
|
4065
|
|
|
|
|
|
|
[-0.5*$pi, 0.5 * $pi], |
|
4066
|
|
|
|
|
|
|
[0.5 * $pi, 1.5 * $pi], |
|
4067
|
|
|
|
|
|
|
[1.5 * $pi, 2 * $pi] |
|
4068
|
|
|
|
|
|
|
) { |
|
4069
|
|
|
|
|
|
|
my @th = linspace($interval->[0] + $epsilon, $interval->[1] - $epsilon, 99, 0); |
|
4070
|
|
|
|
|
|
|
@{ $d{sec}{$i}[0] } = @th; |
|
4071
|
|
|
|
|
|
|
@{ $d{sec}{$i}[1] } = map { 1/cos($_) } @th; |
|
4072
|
|
|
|
|
|
|
if ($i == 0) { |
|
4073
|
|
|
|
|
|
|
$set_opt{sec}{$i} = 'color = "blue", label = "sec(θ)"'; |
|
4074
|
|
|
|
|
|
|
$set_opt{tan}{$i} = 'color = "green", label = "tan(θ)"'; |
|
4075
|
|
|
|
|
|
|
} else { |
|
4076
|
|
|
|
|
|
|
$set_opt{sec}{$i} = 'color = "blue"'; |
|
4077
|
|
|
|
|
|
|
$set_opt{tan}{$i} = 'color = "green"'; |
|
4078
|
|
|
|
|
|
|
} |
|
4079
|
|
|
|
|
|
|
@{ $d{tan}{$i}[0] } = @th; |
|
4080
|
|
|
|
|
|
|
@{ $d{tan}{$i}[1] } = map { sin($_)/cos($_) } @th; |
|
4081
|
|
|
|
|
|
|
$i++; |
|
4082
|
|
|
|
|
|
|
} |
|
4083
|
|
|
|
|
|
|
mkdir 'svg' unless -d 'svg'; |
|
4084
|
|
|
|
|
|
|
my $xticks = "[-2 * $pi, -3 * $pi / 2, -$pi, -$pi / 2, 0, $pi / 2, $pi, 3 * $pi / 2, 2 * $pi" |
|
4085
|
|
|
|
|
|
|
. '], [r\'$-2\pi$\', r\'$-3\pi/2$\', r\'$-\pi$\', r\'$-\pi/2$\', r\'$0$\', r\'$\pi/2$\', r\'$\pi$\', r\'$3\pi/2$\', r\'$2\pi$\']'; |
|
4086
|
|
|
|
|
|
|
my ($min, $max) = (-9,9); |
|
4087
|
|
|
|
|
|
|
plt({ |
|
4088
|
|
|
|
|
|
|
fh => $fh, |
|
4089
|
|
|
|
|
|
|
execute => 0, |
|
4090
|
|
|
|
|
|
|
'output.file' => 'output.images/plots.png', |
|
4091
|
|
|
|
|
|
|
plots => [ |
|
4092
|
|
|
|
|
|
|
{ # sin |
|
4093
|
|
|
|
|
|
|
data => { |
|
4094
|
|
|
|
|
|
|
'sin(θ)' => [ |
|
4095
|
|
|
|
|
|
|
[@x], |
|
4096
|
|
|
|
|
|
|
[map {sin($_)} @x] |
|
4097
|
|
|
|
|
|
|
] |
|
4098
|
|
|
|
|
|
|
}, |
|
4099
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4100
|
|
|
|
|
|
|
'set.options' => { |
|
4101
|
|
|
|
|
|
|
'sin(θ)' => 'color = "orange"' |
|
4102
|
|
|
|
|
|
|
}, |
|
4103
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4104
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4105
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4106
|
|
|
|
|
|
|
ylabel => 'sin(θ)', |
|
4107
|
|
|
|
|
|
|
}, |
|
4108
|
|
|
|
|
|
|
{ # sin |
|
4109
|
|
|
|
|
|
|
data => { |
|
4110
|
|
|
|
|
|
|
'cos(θ)' => [ |
|
4111
|
|
|
|
|
|
|
[@x], |
|
4112
|
|
|
|
|
|
|
[map {cos($_)} @x] |
|
4113
|
|
|
|
|
|
|
] |
|
4114
|
|
|
|
|
|
|
}, |
|
4115
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4116
|
|
|
|
|
|
|
'set.options' => { |
|
4117
|
|
|
|
|
|
|
'cos(θ)' => 'color = "black"' |
|
4118
|
|
|
|
|
|
|
}, |
|
4119
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4120
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4121
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4122
|
|
|
|
|
|
|
ylabel => 'cos(θ)', |
|
4123
|
|
|
|
|
|
|
}, |
|
4124
|
|
|
|
|
|
|
{ # csc |
|
4125
|
|
|
|
|
|
|
data => $d{csc}, |
|
4126
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4127
|
|
|
|
|
|
|
'set.options' => $set_opt{csc}, |
|
4128
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4129
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4130
|
|
|
|
|
|
|
set_ylim => "$min,$max", |
|
4131
|
|
|
|
|
|
|
'show.legend' => 0, |
|
4132
|
|
|
|
|
|
|
vlines => [ # asymptotes |
|
4133
|
|
|
|
|
|
|
"-2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4134
|
|
|
|
|
|
|
"-$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4135
|
|
|
|
|
|
|
"0, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4136
|
|
|
|
|
|
|
"$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4137
|
|
|
|
|
|
|
"2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4138
|
|
|
|
|
|
|
], |
|
4139
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4140
|
|
|
|
|
|
|
ylabel => 'csc(θ)', |
|
4141
|
|
|
|
|
|
|
}, |
|
4142
|
|
|
|
|
|
|
{ # sec |
|
4143
|
|
|
|
|
|
|
data => $d{sec}, |
|
4144
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4145
|
|
|
|
|
|
|
'set.options' => $set_opt{sec}, |
|
4146
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4147
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4148
|
|
|
|
|
|
|
set_ylim => "$min,$max", |
|
4149
|
|
|
|
|
|
|
'show.legend' => 0, |
|
4150
|
|
|
|
|
|
|
vlines => [ # asymptotes |
|
4151
|
|
|
|
|
|
|
"-1.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4152
|
|
|
|
|
|
|
"-.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4153
|
|
|
|
|
|
|
".5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4154
|
|
|
|
|
|
|
"1.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4155
|
|
|
|
|
|
|
# "2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4156
|
|
|
|
|
|
|
], |
|
4157
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4158
|
|
|
|
|
|
|
ylabel => 'sec(θ)', |
|
4159
|
|
|
|
|
|
|
}, |
|
4160
|
|
|
|
|
|
|
{ # csc |
|
4161
|
|
|
|
|
|
|
data => $d{cot}, |
|
4162
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4163
|
|
|
|
|
|
|
'set.options' => $set_opt{cot}, |
|
4164
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4165
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4166
|
|
|
|
|
|
|
set_ylim => "$min,$max", |
|
4167
|
|
|
|
|
|
|
'show.legend' => 0, |
|
4168
|
|
|
|
|
|
|
vlines => [ # asymptotes |
|
4169
|
|
|
|
|
|
|
"-2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4170
|
|
|
|
|
|
|
"-$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4171
|
|
|
|
|
|
|
"0, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4172
|
|
|
|
|
|
|
"$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4173
|
|
|
|
|
|
|
"2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4174
|
|
|
|
|
|
|
], |
|
4175
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4176
|
|
|
|
|
|
|
ylabel => 'cot(θ)', |
|
4177
|
|
|
|
|
|
|
}, |
|
4178
|
|
|
|
|
|
|
{ # sec |
|
4179
|
|
|
|
|
|
|
data => $d{tan}, |
|
4180
|
|
|
|
|
|
|
'plot.type' => 'plot', |
|
4181
|
|
|
|
|
|
|
'set.options' => $set_opt{tan}, |
|
4182
|
|
|
|
|
|
|
set_xticks => $xticks, |
|
4183
|
|
|
|
|
|
|
set_xlim => "-2*$pi, 2*$pi", |
|
4184
|
|
|
|
|
|
|
set_ylim => "$min,$max", |
|
4185
|
|
|
|
|
|
|
'show.legend' => 0, |
|
4186
|
|
|
|
|
|
|
vlines => [ # asymptotes |
|
4187
|
|
|
|
|
|
|
"-1.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4188
|
|
|
|
|
|
|
"-.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4189
|
|
|
|
|
|
|
".5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4190
|
|
|
|
|
|
|
"1.5*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4191
|
|
|
|
|
|
|
# "2*$pi, $min, $max, color = 'gray', linestyle = 'dashed'", |
|
4192
|
|
|
|
|
|
|
], |
|
4193
|
|
|
|
|
|
|
xlabel => 'θ', |
|
4194
|
|
|
|
|
|
|
ylabel => 'tan(θ)', |
|
4195
|
|
|
|
|
|
|
}, |
|
4196
|
|
|
|
|
|
|
], # end |
|
4197
|
|
|
|
|
|
|
ncols => 2, |
|
4198
|
|
|
|
|
|
|
nrows => 3, |
|
4199
|
|
|
|
|
|
|
set_figwidth => 8, |
|
4200
|
|
|
|
|
|
|
suptitle => 'Basic Trigonometric Functions' |
|
4201
|
|
|
|
|
|
|
}); |
|
4202
|
|
|
|
|
|
|
|
|
4203
|
|
|
|
|
|
|
|
|
4204
|
|
|
|
|
|
|
=for html |
|
4205
|
|
|
|
|
|
|
|
|
4206
|
|
|
|
|
|
|
|
|
4207
|
|
|
|
|
|
|
|
|
4208
|
|
|
|
|
|
|
|
|
4209
|
|
|
|
|
|
|
|
|
4210
|
|
|
|
|
|
|
=head2 scatter |
|
4211
|
|
|
|
|
|
|
|
|
4212
|
|
|
|
|
|
|
=head3 single, simple plot |
|
4213
|
|
|
|
|
|
|
|
|
4214
|
|
|
|
|
|
|
scatter({ |
|
4215
|
|
|
|
|
|
|
fh => $fh, |
|
4216
|
|
|
|
|
|
|
data => { |
|
4217
|
|
|
|
|
|
|
X => [@x], |
|
4218
|
|
|
|
|
|
|
Y => [map {sin($_)} @x] |
|
4219
|
|
|
|
|
|
|
}, |
|
4220
|
|
|
|
|
|
|
execute => 0, |
|
4221
|
|
|
|
|
|
|
'output.file' => 'output.images/single.scatter.png', |
|
4222
|
|
|
|
|
|
|
}); |
|
4223
|
|
|
|
|
|
|
|
|
4224
|
|
|
|
|
|
|
makes the following image: |
|
4225
|
|
|
|
|
|
|
|
|
4226
|
|
|
|
|
|
|
|
|
4227
|
|
|
|
|
|
|
=for html |
|
4228
|
|
|
|
|
|
|
|
|
4229
|
|
|
|
|
|
|
|
|
4230
|
|
|
|
|
|
|
|
|
4231
|
|
|
|
|
|
|
|
|
4232
|
|
|
|
|
|
|
|
|
4233
|
|
|
|
|
|
|
=head3 options |
|
4234
|
|
|
|
|
|
|
|
|
4235
|
|
|
|
|
|
|
=head3 multiple plots |
|
4236
|
|
|
|
|
|
|
|
|
4237
|
|
|
|
|
|
|
plt({ |
|
4238
|
|
|
|
|
|
|
fh => $fh, |
|
4239
|
|
|
|
|
|
|
'output.file' => 'output.images/scatterplots.png', |
|
4240
|
|
|
|
|
|
|
execute => 0, |
|
4241
|
|
|
|
|
|
|
nrows => 2, |
|
4242
|
|
|
|
|
|
|
ncols => 3, |
|
4243
|
|
|
|
|
|
|
set_figheight => 8, |
|
4244
|
|
|
|
|
|
|
set_figwidth => 16, |
|
4245
|
|
|
|
|
|
|
suptitle => 'Scatterplot Examples', # applies to all |
|
4246
|
|
|
|
|
|
|
plots => [ |
|
4247
|
|
|
|
|
|
|
{ # single-set scatter; no label |
|
4248
|
|
|
|
|
|
|
data => { |
|
4249
|
|
|
|
|
|
|
X => @e, # x-axis |
|
4250
|
|
|
|
|
|
|
Y => @b, # y-axis |
|
4251
|
|
|
|
|
|
|
Z => @a # color |
|
4252
|
|
|
|
|
|
|
}, |
|
4253
|
|
|
|
|
|
|
title => '"Single Set Scatterplot: Random Distributions"', |
|
4254
|
|
|
|
|
|
|
color_key => 'Z', |
|
4255
|
|
|
|
|
|
|
'set.options' => 'marker = "v"' |
|
4256
|
|
|
|
|
|
|
, # arguments to ax.scatter: there's only 1 set, so "set.options" is a scalar |
|
4257
|
|
|
|
|
|
|
text => [ '100, 100, "text1"', '100, 100, "text2"', ], |
|
4258
|
|
|
|
|
|
|
'plot.type' => 'scatter', |
|
4259
|
|
|
|
|
|
|
}, |
|
4260
|
|
|
|
|
|
|
{ # multiple-set scatter, labels are "X" and "Y" |
|
4261
|
|
|
|
|
|
|
data => { |
|
4262
|
|
|
|
|
|
|
X => { # 1st data set; label is "X" |
|
4263
|
|
|
|
|
|
|
A => @a, # x-axis |
|
4264
|
|
|
|
|
|
|
B => @b, # y-axis |
|
4265
|
|
|
|
|
|
|
}, |
|
4266
|
|
|
|
|
|
|
W => { # 2nd data set; label is "Y" |
|
4267
|
|
|
|
|
|
|
A => generate_normal_dist( 100, 15, 210 ), # x-axis |
|
4268
|
|
|
|
|
|
|
B => generate_normal_dist( 100, 15, 210 ), # y-axis |
|
4269
|
|
|
|
|
|
|
} |
|
4270
|
|
|
|
|
|
|
}, |
|
4271
|
|
|
|
|
|
|
'plot.type' => 'scatter', |
|
4272
|
|
|
|
|
|
|
title => 'Multiple Set Scatterplot', |
|
4273
|
|
|
|
|
|
|
'set.options' => |
|
4274
|
|
|
|
|
|
|
{ # arguments to ax.scatter, for each set in data |
|
4275
|
|
|
|
|
|
|
X => 'marker = ".", color = "red"', |
|
4276
|
|
|
|
|
|
|
W => 'marker = "d", color = "green"' |
|
4277
|
|
|
|
|
|
|
}, |
|
4278
|
|
|
|
|
|
|
}, |
|
4279
|
|
|
|
|
|
|
{ # multiple-set scatter, labels are "X" and "Y" |
|
4280
|
|
|
|
|
|
|
data => { # 8th plot, |
|
4281
|
|
|
|
|
|
|
X => { # 1st data set; label is "X" |
|
4282
|
|
|
|
|
|
|
A => @e, # x-axis |
|
4283
|
|
|
|
|
|
|
B => @b, # y-axis |
|
4284
|
|
|
|
|
|
|
C => @a, # color |
|
4285
|
|
|
|
|
|
|
}, |
|
4286
|
|
|
|
|
|
|
Y => { # 2nd data set; label is "Y" |
|
4287
|
|
|
|
|
|
|
A => generate_normal_dist( 100, 15, 210 ), # x-axis |
|
4288
|
|
|
|
|
|
|
B => generate_normal_dist( 100, 15, 210 ), # y-axis |
|
4289
|
|
|
|
|
|
|
C => generate_normal_dist( 100, 15, 210 ), # color |
|
4290
|
|
|
|
|
|
|
}, |
|
4291
|
|
|
|
|
|
|
}, |
|
4292
|
|
|
|
|
|
|
'plot.type' => 'scatter', |
|
4293
|
|
|
|
|
|
|
title => 'Multiple Set Scatter w/ colorbar', |
|
4294
|
|
|
|
|
|
|
'set.options' => { # arguments to ax.scatter, for each set in data |
|
4295
|
|
|
|
|
|
|
X => 'marker = "."', # diamond |
|
4296
|
|
|
|
|
|
|
Y => 'marker = "d"' # diamond |
|
4297
|
|
|
|
|
|
|
}, |
|
4298
|
|
|
|
|
|
|
color_key => 'Z', |
|
4299
|
|
|
|
|
|
|
} |
|
4300
|
|
|
|
|
|
|
] |
|
4301
|
|
|
|
|
|
|
}); |
|
4302
|
|
|
|
|
|
|
|
|
4303
|
|
|
|
|
|
|
which makes the following figure: |
|
4304
|
|
|
|
|
|
|
|
|
4305
|
|
|
|
|
|
|
|
|
4306
|
|
|
|
|
|
|
=for html |
|
4307
|
|
|
|
|
|
|
|
|
4308
|
|
|
|
|
|
|
|
|
4309
|
|
|
|
|
|
|
|
|
4310
|
|
|
|
|
|
|
|
|
4311
|
|
|
|
|
|
|
|
|
4312
|
|
|
|
|
|
|
=head2 violin |
|
4313
|
|
|
|
|
|
|
|
|
4314
|
|
|
|
|
|
|
plot a hash of array refs as violins |
|
4315
|
|
|
|
|
|
|
|
|
4316
|
|
|
|
|
|
|
=head3 options |
|
4317
|
|
|
|
|
|
|
|
|
4318
|
|
|
|
|
|
|
=for html |
|
4319
|
|
|
|
|
|
|
|
4320
|
|
|
|
|
|
|
|
|
4321
|
|
|
|
|
|
|
| | Option | Description | Example |
|
4322
|
|
|
|
|
|
|
| | -------- | ------- | ------- |
|
4323
|
|
|
|
|
|
|
| color | # a hash, where keys are the keys in data, and values are colors, e.g. X => 'blue' | |
|
4324
|
|
|
|
|
|
|
| colors | match sets | colors => { E => 'yellow', B => 'purple', A => 'green' } |
|
4325
|
|
|
|
|
|
|
| key.order | determine key order display on x-axis | |
|
4326
|
|
|
|
|
|
|
| log | # if set to > 1, the y-axis will be logarithmic | |
|
4327
|
|
|
|
|
|
|
| orientation | 'vertical', 'horizontal'}, default: 'vertical' | |
|
4328
|
|
|
|
|
|
|
|
|
4329
|
|
|
|
|
|
|
| |
|
4330
|
|
|
|
|
|
|
|
|
4331
|
|
|
|
|
|
|
=head3 single, simple plot |
|
4332
|
|
|
|
|
|
|
|
|
4333
|
|
|
|
|
|
|
plt({ |
|
4334
|
|
|
|
|
|
|
'output.file' => 'output.images/single.violinplot.png', |
|
4335
|
|
|
|
|
|
|
data => { # simple hash |
|
4336
|
|
|
|
|
|
|
A => [ 55, @{$z} ], |
|
4337
|
|
|
|
|
|
|
E => [ @{$y} ], |
|
4338
|
|
|
|
|
|
|
B => [ 122, @{$z} ], |
|
4339
|
|
|
|
|
|
|
}, |
|
4340
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4341
|
|
|
|
|
|
|
title => 'Single Violin Plot: Specified Colors', |
|
4342
|
|
|
|
|
|
|
colors => { E => 'yellow', B => 'purple', A => 'green' }, |
|
4343
|
|
|
|
|
|
|
fh => $fh, |
|
4344
|
|
|
|
|
|
|
execute => 0, |
|
4345
|
|
|
|
|
|
|
}); |
|
4346
|
|
|
|
|
|
|
|
|
4347
|
|
|
|
|
|
|
which makes: |
|
4348
|
|
|
|
|
|
|
|
|
4349
|
|
|
|
|
|
|
|
|
4350
|
|
|
|
|
|
|
=for html |
|
4351
|
|
|
|
|
|
|
|
|
4352
|
|
|
|
|
|
|
|
|
4353
|
|
|
|
|
|
|
|
|
4354
|
|
|
|
|
|
|
|
|
4355
|
|
|
|
|
|
|
|
|
4356
|
|
|
|
|
|
|
=head3 multiple plots |
|
4357
|
|
|
|
|
|
|
|
|
4358
|
|
|
|
|
|
|
plt({ |
|
4359
|
|
|
|
|
|
|
fh => $fh, |
|
4360
|
|
|
|
|
|
|
execute => 0, |
|
4361
|
|
|
|
|
|
|
'output.file' => 'output.images/violin.png', |
|
4362
|
|
|
|
|
|
|
plots => [ |
|
4363
|
|
|
|
|
|
|
{ |
|
4364
|
|
|
|
|
|
|
data => { |
|
4365
|
|
|
|
|
|
|
E => @e, |
|
4366
|
|
|
|
|
|
|
B => @b |
|
4367
|
|
|
|
|
|
|
}, |
|
4368
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4369
|
|
|
|
|
|
|
title => 'Basic', |
|
4370
|
|
|
|
|
|
|
xlabel => 'xlabel', |
|
4371
|
|
|
|
|
|
|
set_figwidth => 12, |
|
4372
|
|
|
|
|
|
|
suptitle => 'Violinplot' |
|
4373
|
|
|
|
|
|
|
}, |
|
4374
|
|
|
|
|
|
|
{ |
|
4375
|
|
|
|
|
|
|
data => { |
|
4376
|
|
|
|
|
|
|
E => @e, |
|
4377
|
|
|
|
|
|
|
B => @b |
|
4378
|
|
|
|
|
|
|
}, |
|
4379
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4380
|
|
|
|
|
|
|
color => 'red', |
|
4381
|
|
|
|
|
|
|
title => 'Set Same Color for All', |
|
4382
|
|
|
|
|
|
|
}, |
|
4383
|
|
|
|
|
|
|
{ |
|
4384
|
|
|
|
|
|
|
data => { |
|
4385
|
|
|
|
|
|
|
E => @e, |
|
4386
|
|
|
|
|
|
|
B => @b |
|
4387
|
|
|
|
|
|
|
}, |
|
4388
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4389
|
|
|
|
|
|
|
colors => { |
|
4390
|
|
|
|
|
|
|
E => 'yellow', |
|
4391
|
|
|
|
|
|
|
B => 'black' |
|
4392
|
|
|
|
|
|
|
}, |
|
4393
|
|
|
|
|
|
|
title => 'Color by Key', |
|
4394
|
|
|
|
|
|
|
}, |
|
4395
|
|
|
|
|
|
|
{ |
|
4396
|
|
|
|
|
|
|
data => { |
|
4397
|
|
|
|
|
|
|
E => @e, |
|
4398
|
|
|
|
|
|
|
B => @b |
|
4399
|
|
|
|
|
|
|
}, |
|
4400
|
|
|
|
|
|
|
orientation => 'horizontal', |
|
4401
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4402
|
|
|
|
|
|
|
colors => { |
|
4403
|
|
|
|
|
|
|
E => 'yellow', |
|
4404
|
|
|
|
|
|
|
B => 'black' |
|
4405
|
|
|
|
|
|
|
}, |
|
4406
|
|
|
|
|
|
|
title => 'Horizontal orientation', |
|
4407
|
|
|
|
|
|
|
}, |
|
4408
|
|
|
|
|
|
|
{ |
|
4409
|
|
|
|
|
|
|
data => { |
|
4410
|
|
|
|
|
|
|
E => @e, |
|
4411
|
|
|
|
|
|
|
B => @b |
|
4412
|
|
|
|
|
|
|
}, |
|
4413
|
|
|
|
|
|
|
whiskers => 0, |
|
4414
|
|
|
|
|
|
|
'plot.type' => 'violinplot', |
|
4415
|
|
|
|
|
|
|
colors => { |
|
4416
|
|
|
|
|
|
|
E => 'yellow', |
|
4417
|
|
|
|
|
|
|
B => 'black' |
|
4418
|
|
|
|
|
|
|
}, |
|
4419
|
|
|
|
|
|
|
title => 'Whiskers off', |
|
4420
|
|
|
|
|
|
|
}, |
|
4421
|
|
|
|
|
|
|
], |
|
4422
|
|
|
|
|
|
|
ncols => 3, |
|
4423
|
|
|
|
|
|
|
nrows => 2, |
|
4424
|
|
|
|
|
|
|
}); |
|
4425
|
|
|
|
|
|
|
|
|
4426
|
|
|
|
|
|
|
|
|
4427
|
|
|
|
|
|
|
=for html |
|
4428
|
|
|
|
|
|
|
|
|
4429
|
|
|
|
|
|
|
|
|
4430
|
|
|
|
|
|
|
|
|
4431
|
|
|
|
|
|
|
|
|
4432
|
|
|
|
|
|
|
|
|
4433
|
|
|
|
|
|
|
=head2 wide |
|
4434
|
|
|
|
|
|
|
|
|
4435
|
|
|
|
|
|
|
=head3 options |
|
4436
|
|
|
|
|
|
|
|
|
4437
|
|
|
|
|
|
|
=head3 single, simple plot |
|
4438
|
|
|
|
|
|
|
|
|
4439
|
|
|
|
|
|
|
=head3 multiple plots |
|
4440
|
|
|
|
|
|
|
|
|
4441
|
|
|
|
|
|
|
=head1 Advanced |
|
4442
|
|
|
|
|
|
|
|
|
4443
|
|
|
|
|
|
|
=head2 Notes in Files |
|
4444
|
|
|
|
|
|
|
|
|
4445
|
|
|
|
|
|
|
all files that can have notes with them, give notes about how the file was written. For example, SVG files have the following: |
|
4446
|
|
|
|
|
|
|
|
|
4447
|
|
|
|
|
|
|
made/written by /mnt/ceph/dcondon/ui/gromacs/tut/dup.2puy/1.plot.gromacs.pl called using "plot" in /mnt/ceph/dcondon/perl5/perlbrew/perls/perl-5.42.0/lib/site_perl/5.42.0/x86_64-linux/Matplotlib/Simple.pm` |
|
4448
|
|
|
|
|
|
|
|
|
4449
|
|
|
|
|
|
|
=head2 Speed |
|
4450
|
|
|
|
|
|
|
|
|
4451
|
|
|
|
|
|
|
To improve speed, all data can be written into a single temp python3 file thus: |
|
4452
|
|
|
|
|
|
|
|
|
4453
|
|
|
|
|
|
|
use File::Temp; |
|
4454
|
|
|
|
|
|
|
my $fh = File::Temp->new( DIR => '/tmp', SUFFIX => '.py', UNLINK => 0 ); |
|
4455
|
|
|
|
|
|
|
|
|
4456
|
|
|
|
|
|
|
all files will be written to C<< $fh-Efilename >>; be sure to put C<< execute =E 0 >> unless you want the file to be run, which is the last step. |
|
4457
|
|
|
|
|
|
|
|
|
4458
|
|
|
|
|
|
|
plt({ |
|
4459
|
|
|
|
|
|
|
data => { |
|
4460
|
|
|
|
|
|
|
Clinical => [ |
|
4461
|
|
|
|
|
|
|
[ |
|
4462
|
|
|
|
|
|
|
[@xw], # x |
|
4463
|
|
|
|
|
|
|
[@y] # y |
|
4464
|
|
|
|
|
|
|
], |
|
4465
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ], |
|
4466
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ] |
|
4467
|
|
|
|
|
|
|
], |
|
4468
|
|
|
|
|
|
|
HGI => [ |
|
4469
|
|
|
|
|
|
|
[ |
|
4470
|
|
|
|
|
|
|
[@xw], # x |
|
4471
|
|
|
|
|
|
|
[ map { 1.9 - 1.1 / $_ } @xw ] # y |
|
4472
|
|
|
|
|
|
|
], |
|
4473
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ], |
|
4474
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ] |
|
4475
|
|
|
|
|
|
|
] |
|
4476
|
|
|
|
|
|
|
}, |
|
4477
|
|
|
|
|
|
|
'output.file' => 'output.images/single.wide.png', |
|
4478
|
|
|
|
|
|
|
'plot.type' => 'wide', |
|
4479
|
|
|
|
|
|
|
color => { |
|
4480
|
|
|
|
|
|
|
Clinical => 'blue', |
|
4481
|
|
|
|
|
|
|
HGI => 'green' |
|
4482
|
|
|
|
|
|
|
}, |
|
4483
|
|
|
|
|
|
|
title => 'Visualization of similar lines plotted together', |
|
4484
|
|
|
|
|
|
|
fh => $fh, |
|
4485
|
|
|
|
|
|
|
execute => 0, |
|
4486
|
|
|
|
|
|
|
}); |
|
4487
|
|
|
|
|
|
|
# the last plot should have C<< execute =E 1 >> |
|
4488
|
|
|
|
|
|
|
plt({ |
|
4489
|
|
|
|
|
|
|
data => [ |
|
4490
|
|
|
|
|
|
|
[ |
|
4491
|
|
|
|
|
|
|
[@xw], # x |
|
4492
|
|
|
|
|
|
|
[@y] # y |
|
4493
|
|
|
|
|
|
|
], |
|
4494
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ], |
|
4495
|
|
|
|
|
|
|
[ [@xw], [ map { $_ + rand_between( -0.5, 0.5 ) } @y ] ] |
|
4496
|
|
|
|
|
|
|
], |
|
4497
|
|
|
|
|
|
|
'output.file' => 'output.images/single.array.png', |
|
4498
|
|
|
|
|
|
|
'plot.type' => 'wide', |
|
4499
|
|
|
|
|
|
|
color => 'red', |
|
4500
|
|
|
|
|
|
|
title => 'Visualization of similar lines plotted together', |
|
4501
|
|
|
|
|
|
|
fh => $fh, |
|
4502
|
|
|
|
|
|
|
execute => 1, |
|
4503
|
|
|
|
|
|
|
}); |