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 scatter stem 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 clabels clear clim color colormap crange figsize font grid horizontal isovalue kind label labels levels linewidth location nbins ratio rotation scale size spec subplot tilt title update xaxis xflip xform xlabel xlim xlog xrange xticks yaxis yflip ylabel ylim ylog zflip yrange yticks viewport vp where window zaxis 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 }.freeze
Class Attribute Summary collapse
-
.last_plot ⇒ Object
Returns the value of attribute last_plot.
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.
Instance Method Summary collapse
- #colorbar(off = 0, colors = 256) ⇒ Object
- #draw_axes(kind, pass = 1) ⇒ Object
- #draw_legend ⇒ Object
- #draw_polar_axes ⇒ 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
- #set_viewport(kind, subplot) ⇒ Object
- #set_window(kind) ⇒ Object
- #to_svg ⇒ Object
Constructor Details
#initialize(*raw_args) ⇒ Plot
Returns a new instance of Plot.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gr/plot.rb', line 91 def initialize(*raw_args) @kvs = raw_args.last.is_a?(Hash) ? raw_args.pop : {} @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 = 0 @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.
86 87 88 |
# File 'lib/gr/plot.rb', line 86 def last_plot @last_plot end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
89 90 91 |
# File 'lib/gr/plot.rb', line 89 def args @args end |
#kvs ⇒ Object
Returns the value of attribute kvs.
89 90 91 |
# File 'lib/gr/plot.rb', line 89 def kvs @kvs end |
#scheme ⇒ Object
Returns the value of attribute scheme.
89 90 91 |
# File 'lib/gr/plot.rb', line 89 def scheme @scheme end |
Instance Method Details
#colorbar(off = 0, colors = 256) ⇒ Object
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/gr/plot.rb', line 571 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.5 * (zmax - zmin) / (colors - 1) GR.setwindow(0, 1, zmin, zmax) GR.([1] + 0.02 + off, [1] + 0.05 + off, [2], [3]) l = linspace(1000, 1255, colors).map(&: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) GR.axes(0, ztick, 1, zmin, 0, 1, 0.005) else GR.setscale(GR::OPTION_Y_LOG) GR.axes(0, 2, 1, zmin, 0, 1, 0.005) end GR.restorestate end |
#draw_axes(kind, pass = 1) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/gr/plot.rb', line 324 def draw_axes(kind, pass = 1) = kvs[:viewport] vp = kvs[:vp] ratio = kvs[:ratio] 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] if pass == 1 && drawgrid GR.grid3d(xtick, 0, ztick, xorg[0], yorg[1], zorg[0], 2, 0, 2) GR.grid3d(0, ytick, 0, xorg[0], yorg[1], zorg[0], 0, 2, 0) else GR.axes3d(xtick, 0, ztick, xorg[0], yorg[0], zorg[0], majorx, 0, majorz, -ticksize) GR.axes3d(0, ytick, 0, xorg[1], yorg[0], zorg[0], 0, majory, 0, ticksize) end else charheight = [0.018 * diag, 0.012].max GR.setcharheight(charheight) if %i[heatmap nonuniformheatmap shade].include?(kind) ticksize = -ticksize elsif drawgrid GR.grid(xtick, ytick, 0, 0, majorx, majory) end if kvs.has_key?(:xticklabels) || kvs.has_key?(:yticklabels) fx = if kvs.has_key?(:xticklabels) GRCommons::Fiddley::Function.new( :void, %i[double double string double] ) do |x, y, _svalue, value| label = value < 0 ? '' : kvs[:xticklabels][value] || '' 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| label = value < 0 ? '' : kvs[:yticklabels][value] || '' 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 GR.axes(xtick, ytick, xorg[0], yorg[0], majorx, majory, ticksize) end GR.axes(xtick, ytick, xorg[1], yorg[1], -majorx, -majory, -ticksize) 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 if %i[wireframe surface plot3 scatter3 trisurf volume].include?(kind) xlabel = (kvs[:xlabel] || '').to_s ylabel = (kvs[:ylabel] || '').to_s zlabel = (kvs[:zlabel] || '').to_s GR.titles3d(xlabel, ylabel, zlabel) else 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 if 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 end end |
#draw_legend ⇒ Object
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 |
# File 'lib/gr/plot.rb', line 973 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 ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/gr/plot.rb', line 420 def draw_polar_axes = kvs[:viewport] 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 + 0.5).round (n + 1).times do |i| r = i.to_f / n if i.even? GR.setlinecolorind(88) GR.drawarc(-r, r, -r, r, 0, 359) if i > 0 GR.settextalign(GR::TEXT_HALIGN_LEFT, GR::TEXT_VALIGN_HALF) x, y = GR.wctondc(0.05, r) GR.text(x, y, (rmin + i * tick).to_s) # FIXME: round. significant digits. else GR.setlinecolorind(90) GR.drawarc(-r, r, -r, r, 0, 359) end end 0.step(by: 45, to: 315) do |alpha| sinf = Math.sin(alpha * Math::PI / 180) cosf = Math.cos(alpha * Math::PI / 180) 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 GR.restorestate end |
#plot_data(_figure = true) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 |
# File 'lib/gr/plot.rb', line 604 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 # Not yet. 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) linewidth = kvs[:linewidth] # Slightly different from Julia, # Because the implementation of polyline and polymarker is different. z ||= linewidth # FIXME GR.polyline(x, y, z, c) if hasline(mask) GR.polymarker(x, y, z, c) if hasmarker(mask) when :step 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) z = z&.map { |i| i * 0.01 } c = c&.map { |i| normalize_color(i, *kvs[:crange]) } GR.polymarker(x, y, z, c) when :stem GR.setlinecolorind(1) GR.polyline(kvs[:window][0..1], [0, 0]) 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]) end GR.polymarker(x, y) when :hist if kvs[:horizontal] xmin = kvs[:window][0] x.length.times do |i| GR.setfillcolorind(989) GR.setfillintstyle(GR::INTSTYLE_SOLID) GR.fillrect(xmin, x[i], y[i], y[i + 1]) GR.setfillcolorind(1) GR.setfillintstyle(GR::INTSTYLE_HOLLOW) GR.fillrect(xmin, x[i], y[i], y[i + 1]) end else 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 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 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 kvs[:zrange] = [cmin, cmax] 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 GR._contourf_(x, y, h, z, clabels ? 1 : 0) 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 if z.all? { |zi| zi.size = z[0].size } w = z.size h = z[0].size else raise end 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 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| (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 require 'gr3' GR3.clear dmin, dmax = GR3.volume(z, algorithm) 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) 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.to_a.include? Float::NAN # FIXME: Ruby is different from Julia? # How to check NArray? 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) if kvs[:update] GR.updatews # if GR.isinline() # restore_context() # return GR.show() # end end # flag && restore_context() end |
#plot_img(img) ⇒ Object
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 |
# File 'lib/gr/plot.rb', line 473 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 height, width = img.shape cmin, cmax = kvs[:crange] data = img.map { |i| normalize_color(i, cmin, cmax) } data = data.map { |i| (1000 + i * 255).round } 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.has_key?(:xflip) tmp = xmax xmax = xmin xmin = tmp end if kvs.has_key?(: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
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 |
# File 'lib/gr/plot.rb', line 528 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] || 70) * Math::PI / 180.0) r = 2.5 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
459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/gr/plot.rb', line 459 def plot_polar(θ, ρ) window = kvs[:window] rmax = window[3].to_f ρ = ρ.map { |i| i / rmax } n = ρ.length x = [] y = [] n.times do |i| x << ρ[i] * Math.cos(θ[i]) y << ρ[i] * Math.sin(θ[i]) end GR.polyline(x, y) end |
#set_viewport(kind, subplot) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/gr/plot.rb', line 115 def (kind, subplot) mwidth, mheight, width, height = GR.inqdspsize dpi = width / mwidth * 0.0254 w, h = if kvs[:figsize] [(0.0254 * width * kvs[:figsize][0] / mwidth), (0.0254 * height * kvs[:figsize][1] / mheight)] elsif dpi > 200 kvs[:size].map { |i| i * dpi / 100 } else kvs[:size] end vp = subplot.clone if w > h ratio = h / w.to_f msize = mwidth * w / width GR.(0, msize, 0, msize * ratio) GR.setwswindow(0, 1, 0, ratio) vp[2] *= ratio vp[3] *= ratio else ratio = w / h.to_f msize = mheight * h / height GR.(0, msize * ratio, 0, msize) GR.setwswindow(0, 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 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, h = legend_size [1] -= w + 0.1 end end GR.(*) kvs[:viewport] = kvs[:vp] = vp kvs[:ratio] = ratio if 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 if %i[polar polarhist polarheatmap nonuniformpolarheatmap].include? kind xmin, xmax, ymin, ymax = xcenter = 0.5 * (xmin + xmax) ycenter = 0.5 * (ymin + ymax) r = 0.5 * [xmax - xmin, ymax - ymin].min GR.(xcenter - r, xcenter + r, ycenter - r, ycenter + r) end end |
#set_window(kind) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/gr/plot.rb', line 209 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 %i[heatmap polarheatmap].include?(kind) && kvs.has_key?(:xlim) xmin -= 0.5 xmax += 0.5 end xtick, majorx = if (scale & GR::OPTION_X_LOG) == 0 if !%i[heatmap polarheatmap].include?(kind) && !kvs.has_key?(:xlim) && !kvs[:panzoom] 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 %i[heatmap polarheatmap].include?(kind) && kvs.has_key?(:ylim) ymin -= 0.5 ymax += 0.5 end if kind == :hist if kvs[:horizontal] && !kvs.has_key?(:xlim) xmin = (scale & GR::OPTION_X_LOG) == 0 ? 0 : 1 elsif !kvs.has_key?(:ylim) ymin = (scale & GR::OPTION_Y_LOG) == 0 ? 0 : 1 end end ytick, majory = if (scale & GR::OPTION_Y_LOG) == 0 if !%i[heatmap polarheatmap].include?(kind) && !kvs.has_key?(:ylim) && !kvs[:panzoom] 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) if 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 trisurf].include?(kind) GR.setwindow(-1, 1, -1, 1) else GR.setwindow(xmin, xmax, ymin, ymax) 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 |