Class: GR::Plot
- Inherits:
-
Object
- Object
- GR::Plot
- Defined in:
- lib/gr/plot.rb
Overview
This class offers a simple, matlab-style API built on top of the GR package. The class name Plot may be changed in the future.
Constant Summary collapse
- PLOT_KIND =
Plot kinds conform to GR.jl
%i[ line step stairs scatter stem bar hist contour contourf hexbin heatmap nonuniformheatmap wireframe surface plot3 scatter3 imshow isosurface polar polarhist polarheatmap nonuniformpolarheatmap trisurf tricont shade volume ].freeze
- KW_ARGS =
Keyword options conform to GR.jl.
%i[ accelerate algorithm alpha ax backgroundcolor barwidth baseline borderwidth clabels clines clear clim color colormap crange dpi figsize font grid horizontal isovalue keepaspect kind label labels levels linewidth location markersize nbins panzoom ratio rotation scale size spec subplot theta_direction theta_zero_location tilt title update viewport vp where window xaxis xflip xform xlabel xlim xlog xrange xticks yaxis yflip ylabel ylim ylog yrange yticks zaxis zflip zlabel zlim zlog zrange zticks ].freeze
- FONTS =
{ times_roman: 101, times_italic: 102, times_bold: 103, times_bolditalic: 104, helvetica_regular: 105, helvetica_oblique: 106, helvetica_bold: 107, helvetica_boldoblique: 108, courier_regular: 109, courier_oblique: 110, courier_bold: 111, courier_boldoblique: 112, symbol: 113, bookman_light: 114, bookman_lightitalic: 115, bookman_demi: 116, bookman_demiitalic: 117, newcenturyschlbk_roman: 118, newcenturyschlbk_italic: 119, newcenturyschlbk_bold: 120, newcenturyschlbk_bolditalic: 121, avantgarde_book: 122, avantgarde_bookoblique: 123, avantgarde_demi: 124, avantgarde_demioblique: 125, palatino_roman: 126, palatino_italic: 127, palatino_bold: 128, palatino_bolditalic: 129, zapfchancery_mediumitalic: 130, zapfdingbats: 131, cmuserif_math: 232, # original: cmuserif-math dejavusans: 233, stix_two_math: 234 }.freeze
- THETA_ZERO_LOCATION =
{ 'E' => 0, 'N' => Math::PI / 2, 'W' => Math::PI, 'S' => 1.5 * Math::PI }.freeze
- COLORS =
[ [0xffffff, 0x000000, 0xff0000, 0x00ff00, 0x0000ff, 0x00ffff, 0xffff00, 0xff00ff], [0x282c34, 0xd7dae0, 0xcb4e42, 0x99c27c, 0x85a9fc, 0x5ab6c1, 0xd09a6a, 0xc57bdb], [0xfdf6e3, 0x657b83, 0xdc322f, 0x859900, 0x268bd2, 0x2aa198, 0xb58900, 0xd33682], [0x002b36, 0x839496, 0xdc322f, 0x859900, 0x268bd2, 0x2aa198, 0xb58900, 0xd33682] ].freeze
- DISTINCT_CMAP =
[0, 1, 984, 987, 989, 983, 994, 988].freeze
Class Attribute Summary collapse
-
.last_plot ⇒ Object
Returns the value of attribute last_plot.
-
.scheme ⇒ Object
Returns the value of attribute scheme.
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#kvs ⇒ Object
Returns the value of attribute kvs.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
Class Method Summary collapse
Instance Method Summary collapse
- #colorbar(off = 0, colors = 256) ⇒ Object
- #draw_axes(kind, pass = 1) ⇒ Object
- #draw_legend ⇒ Object
- #draw_polar_axes(pass = 1) ⇒ Object
-
#initialize(*raw_args) ⇒ Plot
constructor
A new instance of Plot.
- #plot_data(_figure = true) ⇒ Object
- #plot_img(img) ⇒ Object
- #plot_iso(v) ⇒ Object
- #plot_polar(θ, ρ) ⇒ Object
- #rgb(color) ⇒ Object
- #set_viewport(kind, subplot) ⇒ Object
- #set_window(kind) ⇒ Object
- #to_svg ⇒ Object
Constructor Details
#initialize(*raw_args) ⇒ Plot
Returns a new instance of Plot.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/gr/plot.rb', line 203 def initialize(*raw_args) # Keywords are cloned to avoid disruptive changes @kvs = raw_args.last.is_a?(Hash) ? raw_args.pop.clone : {} @args = plot_args(raw_args) # method name is the same as Julia/Python # Check keyword options. kvs.each_key { |k| warn "Unknown keyword: #{k}" unless KW_ARGS.include? k } # label(singular form) is a original keyword arg which GR.jl does not have. kvs[:labels] ||= [kvs[:label]] if kvs.has_key? :label # Don't use ||= here, because we need to tell `false` from `nil` kvs[:size] = [600, 450] unless kvs.has_key? :size kvs[:ax] = false unless kvs.has_key? :ax kvs[:subplot] = [0, 1, 0, 1] unless kvs.has_key? :subplot kvs[:clear] = true unless kvs.has_key? :clear kvs[:update] = true unless kvs.has_key? :update @scheme = self.class.scheme @background = 0xffffff # @handle = nil # This variable will be used in gr_meta self.class.last_plot = self end |
Class Attribute Details
.last_plot ⇒ Object
Returns the value of attribute last_plot.
192 193 194 |
# File 'lib/gr/plot.rb', line 192 def last_plot @last_plot end |
.scheme ⇒ Object
Returns the value of attribute scheme.
192 193 194 |
# File 'lib/gr/plot.rb', line 192 def scheme @scheme end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
201 202 203 |
# File 'lib/gr/plot.rb', line 201 def args @args end |
#kvs ⇒ Object
Returns the value of attribute kvs.
201 202 203 |
# File 'lib/gr/plot.rb', line 201 def kvs @kvs end |
#scheme ⇒ Object
Returns the value of attribute scheme.
201 202 203 |
# File 'lib/gr/plot.rb', line 201 def scheme @scheme end |
Class Method Details
.usecolorscheme(index) ⇒ Object
194 195 196 197 198 |
# File 'lib/gr/plot.rb', line 194 def usecolorscheme(index) raise 'Invalid color scheme' unless index >= 1 && index <= 4 @scheme = index end |
Instance Method Details
#colorbar(off = 0, colors = 256) ⇒ Object
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'lib/gr/plot.rb', line 783 def (off = 0, colors = 256) GR.savestate = kvs[:viewport] zmin, zmax = kvs[:zrange] mask = (GR::OPTION_Z_LOG | GR::OPTION_FLIP_Y | GR::OPTION_FLIP_Z) = if kvs.has_key?(:zflip) (GR.inqscale | GR::OPTION_FLIP_Y) elsif kvs.has_key?(:yflip) GR.inqscale & ~GR::OPTION_FLIP_Y else GR.inqscale end GR.setscale( & mask) h = 0 # 0.5 * (zmax - zmin) / (colors - 1) GR.setwindow(0, 1, zmin, zmax) GR.setclipregion(GR::REGION_RECTANGLE) GR.([1] + 0.02 + off, [1] + 0.05 + off, [2], [3]) l = linspace(0, 1, colors).map { |i| (1000 + i * 255).round } GR.cellarray(0, 1, zmax + h, zmin - h, 1, colors, l) GR.setlinecolorind(1) diag = Math.sqrt(([1] - [0])**2 + ([3] - [2])**2) charheight = [0.016 * diag, 0.012].max GR.setcharheight(charheight) if kvs[:scale] & GR::OPTION_Z_LOG == 0 ztick = auto_tick(zmin, zmax) y_axis = GR.axis('Y', position: 1, tick: ztick, org: zmin, major_count: 1, tick_size: 0.005) GR.drawaxis('Y', y_axis) else GR.setscale(GR::OPTION_Y_LOG) y_axis = GR.axis('Y', position: 1, tick: 2, org: zmin, major_count: 1, tick_size: 0.005) GR.drawaxis('Y', y_axis) end GR.restorestate end |
#draw_axes(kind, pass = 1) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/gr/plot.rb', line 446 def draw_axes(kind, pass = 1) = kvs[:viewport] vp = kvs[:vp] xtick, xorg, majorx = kvs[:xaxis] ytick, yorg, majory = kvs[:yaxis] drawgrid = kvs.has_key?(:grid) ? kvs[:grid] : true xtick = 10 if kvs[:scale] & GR::OPTION_X_LOG != 0 ytick = 10 if kvs[:scale] & GR::OPTION_Y_LOG != 0 GR.setlinecolorind(1) diag = Math.sqrt(([1] - [0])**2 + ([3] - [2])**2) GR.setlinewidth(1) ticksize = 0.0075 * diag if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) charheight = [0.024 * diag, 0.012].max GR.setcharheight(charheight) ztick, zorg, majorz = kvs[:zaxis] rotation = kvs[:rotation] || 40 tilt = kvs[:tilt] || 60 zi = tilt >= 0 && tilt <= 90 ? 0 : 1 # Julia: 1-based index -> Ruby: 0-based index xlabel = (kvs[:xlabel] || '').to_s ylabel = (kvs[:ylabel] || '').to_s zlabel = (kvs[:zlabel] || '').to_s GR.setcharheight(charheight * 1.5) GR.settitles3d(xlabel, ylabel, zlabel) GR.setcharheight(charheight) if pass == 1 && drawgrid if rotation >= 0 && rotation < 90 GR.grid3d(xtick, 0, ztick, xorg[0], yorg[1], zorg[zi], 2, 0, 2) GR.grid3d(0, ytick, 0, xorg[0], yorg[1], zorg[zi], 0, 2, 0) elsif rotation >= 90 && rotation < 180 GR.grid3d(xtick, 0, ztick, xorg[1], yorg[1], zorg[zi], 2, 0, 2) GR.grid3d(0, ytick, 0, xorg[1], yorg[1], zorg[zi], 0, 2, 0) elsif rotation >= 180 && rotation < 270 GR.grid3d(xtick, 0, ztick, xorg[1], yorg[0], zorg[zi], 2, 0, 2) GR.grid3d(0, ytick, 0, xorg[1], yorg[0], zorg[zi], 0, 2, 0) else GR.grid3d(xtick, 0, ztick, xorg[0], yorg[0], zorg[0], 2, 0, 2) GR.grid3d(0, ytick, 0, xorg[0], yorg[0], zorg[zi], 0, 2, 0) end elsif rotation >= 0 && rotation < 90 GR.axes3d(xtick, 0, ztick, xorg[0], yorg[0], zorg[zi], majorx, 0, majorz, -ticksize) GR.axes3d(0, ytick, 0, xorg[1], yorg[0], zorg[zi], 0, majory, 0, ticksize) elsif rotation >= 90 && rotation < 180 GR.axes3d(0, 0, ztick, xorg[0], yorg[1], zorg[zi], 0, 0, majorz, -ticksize) GR.axes3d(xtick, ytick, 0, xorg[0], yorg[0], zorg[zi], majorx, majory, 0, -ticksize) elsif rotation >= 180 && rotation < 270 GR.axes3d(xtick, 0, ztick, xorg[1], yorg[1], zorg[zi], majorx, 0, majorz, ticksize) GR.axes3d(0, ytick, 0, xorg[0], yorg[0], zorg[zi], 0, majory, 0, -ticksize) else GR.axes3d(0, 0, ztick, xorg[1], yorg[0], zorg[zi], 0, 0, majorz, -ticksize) GR.axes3d(xtick, ytick, 0, xorg[1], yorg[1], zorg[zi], majorx, majory, 0, ticksize) end else charheight = [0.018 * diag, 0.012].max GR.setcharheight(charheight) if %i[heatmap nonuniformheatmap shade contourf].include?(kind) ticksize = -ticksize drawgrid = false if kind == :shade end if kvs.has_key?(:xticklabels) || kvs.has_key?(:yticklabels) GR.grid(xtick, ytick, 0, 0, majorx, majory) if drawgrid fx = if kvs.has_key?(:xticklabels) GRCommons::Fiddley::Function.new( :void, %i[double double string double] ) do |x, y, _svalue, value| idx = value.round - 1 label = if idx >= 0 && idx < kvs[:xticklabels].size kvs[:xticklabels][idx] else '' end GR.textext(x, y, label) end else GRCommons::Fiddley::Function.new( :void, %i[double double string double] ) do |x, y, _svalue, value| GR.textext(x, y, value.to_s) end end fy = if kvs.has_key?(:yticklabels) GRCommons::Fiddley::Function.new( :void, %i[double double string double] ) do |x, y, _svalue, value| idx = value.round - 1 label = if idx >= 0 && idx < kvs[:yticklabels].size kvs[:yticklabels][idx] else '' end GR.textext(x, y, label) end else GRCommons::Fiddley::Function.new( :void, %i[double double string double] ) do |x, y, _svalue, value| GR.textext(x, y, value.to_s) end end GR.axeslbl(xtick, ytick, xorg[0], yorg[0], majorx, majory, ticksize, fx, fy) else x_axis = GR.axis('X', tick: xtick, org: xorg[0], major_count: majorx, tick_size: ticksize) y_axis = GR.axis('Y', tick: ytick, org: yorg[0], major_count: majory, tick_size: ticksize) = GR::AXES_SIMPLE_AXES | GR::AXES_TWIN_AXES |= GR::AXES_WITH_GRID if drawgrid GR.drawaxes(x_axis, y_axis, ) end end if kvs.has_key?(:title) GR.savestate GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_TOP) text(0.5 * ([0] + [1]), vp[3], kvs[:title].to_s) GR.restorestate end return if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) if kvs.has_key?(:xlabel) GR.savestate GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_BOTTOM) text(0.5 * ([0] + [1]), vp[2] + 0.5 * charheight, kvs[:xlabel].to_s) GR.restorestate end return unless kvs.has_key?(:ylabel) GR.savestate GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_TOP) GR.setcharup(-1, 0) text(vp[0] + 0.5 * charheight, 0.5 * ([2] + [3]), kvs[:ylabel].to_s) GR.restorestate end |
#draw_legend ⇒ Object
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 |
# File 'lib/gr/plot.rb', line 1241 def draw_legend w, h = legend_size = kvs[:viewport] location = kvs[:location] || 1 num_labels = kvs[:labels].length GR.savestate GR.selntran 0 GR.setscale 0 px = case location when 11, 12, 13 [1] + 0.11 when 8, 9, 10 0.5 * ([0] + [1] - w + 0.05) when 2, 3, 6 [0] + 0.11 else [1] - 0.05 - w end py = case location when 5, 6, 7, 10, 12 0.5 * ([2] + [3] + h - 0.03) when 13 [2] + h when 3, 4, 8 [2] + h + 0.03 when 11 [3] - 0.03 else [3] - 0.06 end GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.setfillcolorind(0) GR.fillrect(px - 0.08, px + w + 0.02, py + 0.03, py - h) GR.setlinetype(GR::LINETYPE_SOLID) GR.setlinecolorind(1) GR.setlinewidth(1) GR.drawrect(px - 0.08, px + w + 0.02, py + 0.03, py - h) i = 0 GR.uselinespec(' ') args.each do |_x, _y, _z, _c, spec| if i < num_labels label = kvs[:labels][i] label = label.to_s _tbx, tby = inqtext(0, 0, label) dy = [(tby[2] - tby[0]) - 0.03, 0].max py -= 0.5 * dy end GR.savestate mask = GR.uselinespec(spec || '') GR.polyline([px - 0.07, px - 0.01], [py, py]) if hasline(mask) GR.polymarker([px - 0.06, px - 0.02], [py, py]) if hasmarker(mask) GR.restorestate GR.settextalign(GR::TEXT_HALIGN_LEFT, GR::TEXT_VALIGN_HALF) if i < num_labels text(px, py, label) py -= 0.5 * dy i += 1 end py -= 0.03 end GR.selntran(1) GR.restorestate end |
#draw_polar_axes(pass = 1) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/gr/plot.rb', line 578 def draw_polar_axes(pass = 1) = kvs[:viewport] vp = kvs[:vp] diag = Math.sqrt(([1] - [0])**2 + ([3] - [2])**2) charheight = [0.018 * diag, 0.012].max window = kvs[:window] rmin = window[2] rmax = window[3] GR.savestate GR.setcharheight(charheight) GR.setlinetype(GR::LINETYPE_SOLID) tick = auto_tick(rmin, rmax) n = ((rmax - rmin) / tick).truncate if n <= 4 tick /= 2.0 n *= 2 end if pass == 1 GR.selntran(1) (n + 1).times do |i| r = i.to_f * tick / (rmax - rmin) if r > 0 && r < 1 if i.even? GR.setlinecolorind(88) GR.drawarc(-r, r, -r, r, 0, 360) else GR.setlinecolorind(90) GR.drawarc(-r, r, -r, r, 0, 360) end end end GR.setclip(0) GR.setlinecolorind(88) GR.drawarc(-1, 1, -1, 1, 0, 360) GR.setclip(1) sign = (kvs[:theta_direction] || 1) > 0 ? 1 : -1 offs = THETA_ZERO_LOCATION[kvs[:theta_zero_location] || 'E'] 0.step(by: 45, to: 315) do |alpha| sinf = Math.sin((alpha * sign) * Math::PI / 180 + offs) cosf = Math.cos((alpha * sign) * Math::PI / 180 + offs) GR.setlinecolorind(88) GR.polyline([cosf, 0], [sinf, 0]) GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_HALF) x, y = GR.wctondc(1.1 * cosf, 1.1 * sinf) GR.textext(x, y, "#{alpha}^o") end if kvs.has_key?(:title) GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_TOP) text(0.5 * ([0] + [1]), vp[3] - 0.02, kvs[:title].to_s) end end if pass == 2 start = (rmin / tick).floor.truncate (n + 1).times do |i| j = start + i next unless j * tick >= rmin r = i.to_f * tick / (rmax - rmin) next unless i.even? GR.settextalign(GR::TEXT_HALIGN_LEFT, GR::TEXT_VALIGN_HALF) x, y = GR.wctondc(0.05, r) fmt = GR.getformat(start, rmin, rmax, tick, 2) s = GR.ftoa(j * tick, fmt) GR.text(x, y, s) end end GR.restorestate end |
#plot_data(_figure = true) ⇒ Object
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 |
# File 'lib/gr/plot.rb', line 827 def plot_data(_figure = true) # GR.init # target = GR.displayname # if flag && target != None # if target == "js" || target == "meta" # send_meta(0) # else # send_serialized(target) # end # return # end kind = kvs[:kind] || :line GR.clearws if kvs[:clear] if scheme != 0 8.times do |colorind| color = COLORS[scheme - 1][colorind] r, g, b = rgb(color) GR.setcolorrep(colorind, r, g, b) GR.setcolorrep(DISTINCT_CMAP[colorind], r, g, b) if scheme != 1 end r, g, b = rgb(COLORS[scheme - 1][0]) r2, g2, b2 = rgb(COLORS[scheme - 1][1]) rdiff = r2 - r gdiff = g2 - g bdiff = b2 - b 12.times do |colorind| f = colorind / 11.0 GR.setcolorrep(91 - colorind, r + f * rdiff, g + f * gdiff, b + f * bdiff) end end if kvs.has_key?(:font) name = kvs[:font] # 'Cmuserif-Math' => :cmuserif_math sym_name = name.to_s.gsub('-', '_').downcase.to_sym if FONTS.include?(sym_name) font = FONTS[sym_name] GR.settextfontprec(font, font > 200 ? 3 : 0) else font = GR.loadfont(name) if font >= 0 GR.settextfontprec(font, 3) else warn "Unknown font name: #{name}" end end else # The following fonts are the default in GR.jl # Japanese, Chinese, Korean, etc. cannot be displayed. # GR.settextfontprec(232, 3) # CM Serif Roman end (kind, kvs[:subplot]) unless kvs[:ax] set_window(kind) if %i[polar polarhist].include?(kind) draw_polar_axes elsif !%i[imshow isosurface polarheatmap nonuniformpolarheatmap].include?(kind) draw_axes(kind) end end if kvs.has_key?(:colormap) GR.setcolormap(kvs[:colormap]) else GR.setcolormap(GR::COLORMAP_VIRIDIS) end GR.uselinespec(' ') args.each do |x, y, z, c, spec| spec ||= kvs[:spec] ||= '' GR.savestate GR.settransparency(kvs[:alpha]) if kvs.has_key?(:alpha) case kind when :line mask = GR.uselinespec(spec) if c linewidth = kvs[:linewidth] || 1 z = Array.new(x.length, linewidth) GR.polyline(x, y, z, c) else if hasline(mask) linewidth = kvs[:linewidth] || 1 GR.setlinewidth(linewidth) GR.polyline(x, y) end if hasmarker(mask) markersize = kvs[:markersize] || 1 GR.setmarkersize(markersize) borderwidth = kvs[:borderwidth] || 1 GR.setborderwidth(borderwidth) GR.polymarker(x, y) end end when :step, :stairs mask = GR.uselinespec(spec) if hasline(mask) where = kvs[:where] || 'mid' n = x.length xs = [x[0]] case where when 'pre' ys = [y[0]] (n - 1).times do |i| xs << x[i] << x[i + 1] ys << y[i + 1] << y[i + 1] end when 'post' ys = [y[0]] (n - 1).times do |i| xs << x[i + 1] << x[i + 1] ys << y[i] << y[i + 1] end else ys = [] (n - 1).times do |i| xs << 0.5 * (x[i] + x[i + 1]) << 0.5 * (x[i] + x[i + 1]) ys << y[i] << y[i] end xs << x[n - 1] ys << y[n - 1] << y[n - 1] end GR.polyline(xs, ys) end GR.polymarker(x, y) if hasmarker(mask) when :scatter GR.setmarkertype(GR::MARKERTYPE_SOLID_CIRCLE) if z || c if c cmin, cmax = kvs[:crange] c = c.map { |i| normalize_color(i, cmin, cmax) } c = c.map { |i| (1000 + i * 255).round } end GR.polymarker(x, y, z, c) else GR.polymarker(x, y) end when :stem GR.setmarkertype(GR::MARKERTYPE_SOLID_CIRCLE) GR.uselinespec(spec) x = x.to_a if narray?(x) y = y.to_a if narray?(y) x.zip(y).each do |xi, yi| GR.polyline([xi, xi], [0, yi]) GR.polymarker([xi], [yi]) end GR.setlinecolorind(1) GR.polyline(kvs[:window][0..1], [0, 0]) when :bar (0...x.length).step(2) do |i| GR.setfillcolorind(989) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.fillrect(x[i], x[i + 1], y[i], y[i + 1]) GR.setfillcolorind(1) GR.setfillintstyle(GR::INTSTYLE_HOLLOW) GR.fillrect(x[i], x[i + 1], y[i], y[i + 1]) end when :hist ymin = kvs[:window][2] y.length.times do |i| GR.setfillcolorind(989) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.fillrect(x[i], x[i + 1], ymin, y[i]) GR.setfillcolorind(1) GR.setfillintstyle(GR::INTSTYLE_HOLLOW) GR.fillrect(x[i], x[i + 1], ymin, y[i]) end when :polarhist ymax = kvs[:window][3].to_f ρ = y.map { |i| i / ymax } θ = x.map { |i| i * 180 / Math::PI } (1...ρ.length).each do |i| GR.setfillcolorind(989) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.fillarc(-ρ[i], ρ[i], -ρ[i], ρ[i], θ[i - 1], θ[i]) GR.setfillcolorind(1) GR.setfillintstyle(GR::INTSTYLE_HOLLOW) GR.fillarc(-ρ[i], ρ[i], -ρ[i], ρ[i], θ[i - 1], θ[i]) end draw_polar_axes(2) when :polarheatmap, :nonuniformpolarheatmap w, h = z.shape cmap = colormap cmin, cmax = kvs[:zrange] data = z.map { |i| normalize_color(i, cmin, cmax) } data.reverse(axis: 0) if kvs[:xflip] data.reverse(axis: 1) if kvs[:yflip] colors = data * 255 + 1000 colors = colors.transpose # Julia is column major case kind when :polarheatmap GR.polarcellarray(0, 0, 0, 360, 0, 1, w, h, colors) when :nonuniformpolarheatmap ymax = y.max.to_f ρ = y.map { |i| i / ymax } θ = x.map { |i| i * 180 / Math::PI } GR.nonuniformpolarcellarray(θ, ρ, w, h, colors) end draw_polar_axes(1) draw_polar_axes(2) kvs[:zrange] = [cmin, cmax] (0.025) when :contour, :contourf zmin, zmax = kvs[:zrange] if narray?(z) && z.ndim == 2 a, b = z.shape x = (1..b).to_a y = (1..a).to_a zmin, zmax = z.minmax elsif equal_length(x, y, z) x, y, z = GR.gridit(x, y, z, 200, 200) zmin, zmax = z.compact.minmax # compact : removed nil end # kvs[:zlim] is supposed to be Array or Range if kvs.has_key?(:zlim) zmin = kvs[:zlim].first if kvs[:zlim].first zmax = kvs[:zlim].last if kvs[:zlim].last end GR.setprojectiontype(0) GR.setspace(zmin, zmax, 0, 90) levels = kvs[:levels] || 0 clabels = kvs[:clabels] || false if levels.is_a? Integer hmin, hmax = GR.adjustrange(zmin, zmax) h = linspace(hmin, hmax, levels == 0 ? 21 : levels + 1) else h = levels end case kind when :contour GR._contour_(x, y, h, z, clabels ? 1 : 1000) when :contourf clines = kvs.has_key?(:clines) ? kvs[:clines] : true GR._contourf_(x, y, h, z, if clines clabels ? 1 : 0 else -1 end) end (0, h.length) when :hexbin nbins = kvs[:nbins] || 40 cntmax = GR._hexbin_(x, y, nbins) if cntmax > 0 kvs[:zrange] = [0, cntmax] end when :heatmap, :nonuniformheatmap case z when Array raise unless z.all? { |zi| zi.size = z[0].size } w = z.size h = z[0].size when ->(obj) { narray?(obj) } w, h = z.shape else raise end cmap = colormap cmin, cmax = kvs[:crange] levels = kvs[:levels] || 256 data = z.flatten.to_a.map { |i| normalize_color(i, cmin, cmax) } # NArray -> Array if kind == :heatmap && !ENV['GR_SCALE_FACTOR'] rgba = data.map { |v| to_rgba(v, cmap) } GR.drawimage(0.5, w + 0.5, h + 0.5, 0.5, w, h, rgba) else colors = data.map { |i| (i.nan? ? 1256 : 1000 + i * 255).round } GR.nonuniformcellarray(x, y, w, h, colors) end (0, levels) when :wireframe if narray?(z) && z.ndim == 2 a, b = z.shape x = (1..b).to_a y = (1..a).to_a elsif equal_length(x, y, z) x, y, z = GR.gridit(x, y, z, 50, 50) end GR.setfillcolorind(0) GR._surface_(x, y, z, GR::OPTION_FILLED_MESH) draw_axes(kind, 2) when :surface if narray?(z) && z.ndim == 2 a, b = z.shape x = (1..b).to_a y = (1..a).to_a elsif equal_length(x, y, z) x, y, z = GR.gridit(x, y, z, 200, 200) end if kvs[:accelerate] == false GR._surface_(x, y, z, GR::OPTION_COLORED_MESH) else require 'gr3' GR3.clear GR3.surface(x, y, z, GR::OPTION_COLORED_MESH) end draw_axes(kind, 2) (0.05) when :volume algorithm = kvs[:algorithm] || 0 w, h, ratio = GR.inqvpsize GR.setpicturesizeforvolume((w * ratio).round, (h * ratio).round) require 'gr3' GR3.clear ambient, diffuse, specular, specular_power = GR3.getlightparameters GR3.setlightparameters(0.8, 0.2, 0.1, 10.0) dmin, dmax = GR3.volume(z, algorithm) GR3.setlightparameters(ambient, diffuse, specular, specular_power) draw_axes(kind, 2) kvs[:zrange] = [dmin, dmax] (0.05) when :plot3 mask = GR.uselinespec(spec) GR.polyline3d(x, y, z) if hasline(mask) GR.polymarker3d(x, y, z) if hasmarker(mask) draw_axes(kind, 2) when :scatter3 GR.setmarkertype(GR::MARKERTYPE_SOLID_CIRCLE) if c cmin, cmax = kvs[:crange] c = c.map { |i| normalize_color(i, cmin, cmax) } cind = c.map { |i| (1000 + i * 255).round } x.length.times do |i| GR.setmarkercolorind(cind[i]) GR.polymarker3d([x[i]], [y[i]], [z[i]]) end else GR.polymarker3d(x, y, z) end draw_axes(kind, 2) when :imshow plot_img(z) when :isosurface plot_iso(z) when :polar GR.uselinespec(spec) plot_polar(x, y) draw_polar_axes(2) when :trisurf GR.trisurface(x, y, z) draw_axes(kind, 2) (0.05) when :tricont zmin, zmax = kvs[:zrange] levels = linspace(zmin, zmax, 20) GR.tricontour(x, y, z, levels) when :shade xform = kvs[:xform] || 5 if (x.respond_to?(:isnan) && x.isnan.any?) || (x.is_a?(Array) && x.include?(Float::NAN)) GR.shadelines(x, y, xform: xform) else GR.shadepoints(x, y, xform: xform) end when :bar 0.step(x.length - 1, 2) do |i| GR.setfillcolorind(989) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.fillrect(x[i], x[i + 1], y[i], y[i + 1]) GR.setfillcolorind(1) GR.setfillintstyle(GR::INTSTYLE_HOLLOW) GR.fillrect(x[i], x[i + 1], y[i], y[i + 1]) end end GR.restorestate end draw_legend if %i[line step scatter stem].include?(kind) && kvs.has_key?(:labels) return unless kvs[:update] GR.updatews # if GR.isinline() # restore_context() # return GR.show() # end # flag && restore_context() end |
#plot_img(img) ⇒ Object
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/gr/plot.rb', line 673 def plot_img(img) = kvs[:vp].clone [3] -= 0.05 if kvs.has_key?(:title) vp = kvs[:vp] if img.is_a? String width, height, data = GR.readimage(img) else if narray?(img) height, width = img.shape data = img else height = img.length width = img[0].length data = img.flatten end cmin, cmax = kvs[:crange] if narray?(data) data = (data - cmin) / (cmax - cmin) data = (data * 255 + 1000).round.cast_to(Numo::Int32) else data = data.map { |i| (1000 + normalize_color(i, cmin, cmax) * 255).round } end end if width * ([3] - [2]) < height * ([1] - [0]) w = width.to_f / height * ([3] - [2]) xmin = [0.5 * ([0] + [1] - w), [0]].max xmax = [0.5 * ([0] + [1] + w), [1]].min ymin = [2] ymax = [3] else h = height.to_f / width * ([1] - [0]) xmin = [0] xmax = [1] ymin = [0.5 * ([3] + [2] - h), [2]].max ymax = [0.5 * ([3] + [2] + h), [3]].min end GR.selntran(0) GR.setscale(0) if kvs[:xflip] tmp = xmax xmax = xmin xmin = tmp end if kvs[:yflip] tmp = ymax ymax = ymin ymin = tmp end if img.is_a? String GR.drawimage(xmin, xmax, ymin, ymax, width, height, data) else GR.cellarray(xmin, xmax, ymin, ymax, width, height, data) end if kvs.has_key?(:title) GR.savestate GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_TOP) text(0.5 * ([0] + [1]), vp[3], kvs[:title].to_s) GR.restorestate end GR.selntran(1) end |
#plot_iso(v) ⇒ Object
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
# File 'lib/gr/plot.rb', line 739 def plot_iso(v) = kvs[:viewport] if [3] - [2] < [1] - [0] width = [3] - [2] centerx = 0.5 * ([0] + [1]) xmin = [centerx - 0.5 * width, [0]].max xmax = [centerx + 0.5 * width, [1]].min ymin = [2] ymax = [3] else height = [1] - [0] centery = 0.5 * ([2] + [3]) xmin = [0] xmax = [1] ymin = [centery - 0.5 * height, [2]].max ymax = [centery + 0.5 * height, [3]].min end GR.selntran(0) v = Numo::DFloat.cast(v) if v.is_a? Array values = ((v - v.min) / (v.max - v.min) * (2**16 - 1)).round values = Numo::UInt16.cast(values) nx, ny, nz = v.shape isovalue = ((kvs[:isovalue] || 0.5) - v.min) / (v.max - v.min) rotation = ((kvs[:rotation] || 40) * Math::PI / 180.0) tilt = ((kvs[:tilt] || 60) * Math::PI / 180.0) r = 2.5 require 'gr3' GR3.clear mesh = GR3.createisosurfacemesh(values, [2.0 / (nx - 1), 2.0 / (ny - 1), 2.0 / (nz - 1)], [-1, -1, -1], (isovalue * (2**16 - 1)).round) color = kvs[:color] || [0.0, 0.5, 0.8] GR3.setbackgroundcolor(1, 1, 1, 0) GR3.drawmesh(mesh, 1, [0, 0, 0], [0, 0, 1], [0, 1, 0], color, [1, 1, 1]) GR3.cameralookat(r * Math.sin(tilt) * Math.sin(rotation), r * Math.cos(tilt), r * Math.sin(tilt) * Math.cos(rotation), 0, 0, 0, 0, 1, 0) GR3.drawimage(xmin, xmax, ymin, ymax, 500, 500, GR3::DRAWABLE_GKS) GR3.deletemesh(mesh) GR.selntran(1) end |
#plot_polar(θ, ρ) ⇒ Object
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
# File 'lib/gr/plot.rb', line 656 def plot_polar(θ, ρ) window = kvs[:window] rmin = window[2] rmax = window[3] sign = (kvs[:theta_direction] || 1) > 0 ? 1 : -1 offs = THETA_ZERO_LOCATION[kvs[:theta_zero_location] || 'E'] ρ = ρ.map { |i| (i - rmin) / (rmax - rmin) } n = ρ.length x = [] y = [] n.times do |i| x << ρ[i] * Math.cos(θ[i] * sign + offs) y << ρ[i] * Math.sin(θ[i] * sign + offs) end GR.polyline(x, y) end |
#rgb(color) ⇒ Object
819 820 821 822 823 824 825 |
# File 'lib/gr/plot.rb', line 819 def rgb(color) [ ((color >> 16) & 0xff) / 255.0, ((color >> 8) & 0xff) / 255.0, (color & 0xff) / 255.0 ] end |
#set_viewport(kind, subplot) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/gr/plot.rb', line 228 def (kind, subplot) mwidth, mheight, width, height = GR.inqdspsize if kvs.has_key?(:figsize) w, h = kvs[:figsize] if w < 2 && h < 2 w = width * w / mwidth h = height * h / mheight end else dpi = kvs[:dpi] dpi ||= (width / mwidth * 0.0254).round if dpi > 200 w, h = kvs[:size].map { |i| i * dpi / 100.0 } else w, h = kvs[:size] end end vp = subplot.clone.map(&:to_f) if w > h ratio = w / h.to_f msize = mwidth * w / width GR.(0, msize, 0, msize / ratio) GR.setwswindow(0, 1, 0, 1 / ratio) vp[2] /= ratio vp[3] /= ratio else ratio = h / w.to_f msize = mheight * h / height GR.(0, msize / ratio, 0, msize) GR.setwswindow(0, 1 / ratio, 0, 1) vp[0] /= ratio vp[1] /= ratio end if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) extent = [vp[1] - vp[0], vp[3] - vp[2]].min vp1 = 0.5 * (vp[0] + vp[1] - extent) vp2 = 0.5 * (vp[0] + vp[1] + extent) vp3 = 0.5 * (vp[2] + vp[3] - extent) vp4 = 0.5 * (vp[2] + vp[3] + extent) else vp1, vp2, vp3, vp4 = vp end left_margin = kvs.has_key?(:ylabel) ? 0.05 : 0 right_margin = if %i[contour contourf tricont hexbin heatmap nonuniformheatmap polarheatmap nonuniformpolarheatmap surface trisurf volume].include?(kind) (vp2 - vp1) * 0.1 else 0 end bottom_margin = kvs.has_key?(:xlabel) ? 0.05 : 0 top_margin = kvs.has_key?(:title) ? 0.075 : 0 = [vp1 + (0.075 + left_margin) * (vp2 - vp1), vp1 + (0.95 - right_margin) * (vp2 - vp1), vp3 + (0.075 + bottom_margin) * (vp4 - vp3), vp3 + (0.975 - top_margin) * (vp4 - vp3)] if %i[line step scatter stem].include?(kind) && kvs[:labels] location = kvs[:location] || 1 if [11, 12, 13].include?(location) w_legend, _h_legend = legend_size [1] -= w_legend + 0.1 end end if %i[polar polarhist polarheatmap nonuniformpolarheatmap].include?(kind) xmin, xmax, ymin, ymax = xcenter = 0.5 * (xmin + xmax) ycenter = 0.5 * (ymin + ymax) r = 0.45 * [xmax - xmin, ymax - ymin].min if kvs.has_key?(:title) r *= 0.975 ycenter -= 0.025 * r end [0] = xcenter - r [1] = xcenter + r [2] = ycenter - r [3] = ycenter + r end GR.(*) kvs[:viewport] = kvs[:vp] = vp kvs[:ratio] = ratio return unless kvs[:backgroundcolor] GR.savestate GR.selntran(0) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.setfillcolorind(kvs[:backgroundcolor]) if w > h GR.fillrect(subplot[0], subplot[1], ratio * subplot[2], ratio * subplot[3]) else GR.fillrect(ratio * subplot[0], ratio * subplot[1], subplot[2], subplot[3]) end GR.selntran(1) GR.restorestate end |
#set_window(kind) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/gr/plot.rb', line 335 def set_window(kind) scale = 0 unless %i[polar polarhist polarheatmap nonuniformpolarheatmap].include?(kind) scale |= GR::OPTION_X_LOG if kvs[:xlog] scale |= GR::OPTION_Y_LOG if kvs[:ylog] scale |= GR::OPTION_Z_LOG if kvs[:zlog] scale |= GR::OPTION_FLIP_X if kvs[:xflip] scale |= GR::OPTION_FLIP_Y if kvs[:yflip] scale |= GR::OPTION_FLIP_Z if kvs[:zflip] end kvs[:scale] = scale if kvs.has_key?(:panzoom) xmin, xmax, ymin, ymax = GR.panzoom(*kvs[:panzoom]) kvs[:xrange] = [xmin, xmax] kvs[:yrange] = [ymin, ymax] else minmax(kind) end major_count = if %i[wireframe surface plot3 scatter3 polar polarhist polarheatmap nonuniformpolarheatmap trisurf volume].include?(kind) 2 else 5 end kvs[:xticks] = [kvs[:xticks], major_count] if kvs[:xticks].is_a? Numeric kvs[:yticks] = [kvs[:yticks], major_count] if kvs[:yticks].is_a? Numeric kvs[:zticks] = [kvs[:zticks], major_count] if kvs[:zticks].is_a? Numeric xmin, xmax = kvs[:xrange] if kind == :heatmap && !kvs.has_key?(:xlim) xmin -= 0.5 xmax += 0.5 end xtick, majorx = if (scale & GR::OPTION_X_LOG) == 0 if !kvs.has_key?(:xlim) && !kvs[:panzoom] && !%i[heatmap polarheatmap nonuniformpolarheatmap].include?(kind) xmin, xmax = GR.adjustlimits(xmin, xmax) end if kvs.has_key?(:xticks) kvs[:xticks] else [auto_tick(xmin, xmax) / major_count, major_count] end else [1, 1] end xorg = (scale & GR::OPTION_FLIP_X) == 0 ? [xmin, xmax] : [xmax, xmin] kvs[:xaxis] = xtick, xorg, majorx ymin, ymax = kvs[:yrange] if kind == :heatmap && !kvs.has_key?(:ylim) ymin -= 0.5 ymax += 0.5 end if kind == :hist && !kvs.has_key?(:ylim) ymin = (scale & GR::OPTION_Y_LOG) == 0 ? 0 : 1 end ytick, majory = if (scale & GR::OPTION_Y_LOG) == 0 if !kvs.has_key?(:ylim) && !kvs[:panzoom] && !%i[heatmap polarheatmap nonuniformpolarheatmap].include?(kind) ymin, ymax = GR.adjustlimits(ymin, ymax) end if kvs.has_key?(:yticks) kvs[:yticks] else [auto_tick(ymin, ymax) / major_count, major_count] end else [1, 1] end yorg = (scale & GR::OPTION_FLIP_Y) == 0 ? [ymin, ymax] : [ymax, ymin] kvs[:yaxis] = ytick, yorg, majory if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) zmin, zmax = kvs[:zrange] ztick, majorz = if (scale & GR::OPTION_Z_LOG) == 0 zmin, zmax = GR.adjustlimits(zmin, zmax) unless kvs.has_key?(:zlim) if kvs.has_key?(:zticks) kvs[:zticks] else [auto_tick(zmin, zmax) / major_count, major_count] end else [1, 1] end zorg = (scale & GR::OPTION_FLIP_Z) == 0 ? [zmin, zmax] : [zmax, zmin] kvs[:zaxis] = ztick, zorg, majorz end kvs[:window] = xmin, xmax, ymin, ymax if %i[polar polarhist polarheatmap nonuniformpolarheatmap].include?(kind) GR.setwindow(-1, 1, -1, 1) GR.setclipregion(GR::REGION_ELLIPSE) else GR.setwindow(xmin, xmax, ymin, ymax) GR.setclipregion(GR::REGION_RECTANGLE) end if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) rotation = kvs[:rotation] || 40 tilt = kvs[:tilt] || 60 GR.setwindow3d(xmin, xmax, ymin, ymax, zmin, zmax) GR.setspace3d(-rotation, tilt, 30, 0) end kvs[:scale] = scale GR.setscale(scale) end |