plotgallery3d

# Differences

This shows you the differences between two versions of the page.

 — plotgallery3d [2018/12/27 13:27] (current) Line 1: Line 1: + + + ~~TOC 1-3 wide~~ + + --- + + ^  [[plotting|Plotting Introduction]] ​ ^  [[plotattributes|Plot Attributes]] ​ ^  [[plotgallery2d|2-D Gallery]] ​ ^  [[plotgallery3d|3-D Gallery]] ​ ^ + + + --- + + + ```juliaeval + julia> pkgchk.( "​julia"​ => v"​1.0.3",​ "​Plots"​ => v"​0.21.0"​ ); + + ``` + + + # 3-D Plot Gallery + + ## Making Grids and Interpolating Data + + Julia Plots.jl can always easily plot functions. ​ It takes an x-vector and a y-vector, and a function operating on them. + + ```juliarepl + + x= [1.0:​0.1:​5;​];​ y= [4.0:​0.1:​9.0;​];​ f(x,y)= sin(x)*cos(y);​ + + ``` + + Plotting data (i.e., not mathematical functions) can be more difficult. Some functions (like the "​surface"​ plot) know how to work with data, but other functions (like the "​contour"​ plot) do not.  Note also that different z values mean intrinsically different things in the two cases. ​ With a function, Plots samples length(x)*length(y) points. ​ With data, length(x)==length(y)==length(z). + + ```juliarepl + + n=100; xr= 10*rand(n); yr= 10*rand(n); zr= Float64[ sin(xr[i])+cos(yr[i]) for i=1:n]; + + ``` + + This chapter focuses on Plots.jl, but if the need arises, there are other graphics solutions that do allow contour-plotting data, such as [GMT](https://​discourse.julialang.org/​t/​interpolated-data-contour-plot-like-surface/​15040/​6?​u=iwelch). ​ It should be `x,​y,​z=GMT.peaks();​ contour([x[:​] y[:] z[:]], cont=1, annot=2, frame="​a",​ fmt="​png",​ show=1)`. + + ## Surface 3-D Plot With Data + + ```juliaeval + + using Plots + n=100; xr= 10*rand(n); yr= 10*rand(n); zr= Float64[ sin(xr[i])+cos(yr[i]) for i=1:n]; + surface( xr, yr, zr, size=[800,​480] ) + savefig( "​plotting/​surfaceiplot.png"​ ) + + ``` + + {{surfaceiplot.png}} + + * The color bar on the right is the legend. ​ Thus, it can be turned off with `legend=false`. + + FIXME How do I work + + * plot( x, Y, clims=);​savefig( "​plotting/​ clims.png"​ );  ## the color bar limit + + * plot( x, Y, color_palette=);​savefig( "​plotting/​ color_palette.png"​ ); + + * plot( x, Y, colorbar=);​savefig( "​plotting/​ colorbar.png"​ ); + + * plot( x, Y, colorbar_title=);​savefig( "​plotting/​ colorbar_title.png"​ ); + + ## Function-Based 3-D Plots + + ### Plain Plot + + ```juliaeval + + using Plots + n=50; + xr= range(1, stop=100, length=n) + rand(n)/​10.0;​ + yr= range(100, stop=200, length=n) + rand(n)/​10.0;​ + zr= Float64[ sin(xr[i])+cos(yr[i]) for i=1:n]; + + ptl= plot(xr, yr, zr, camera=(10,​45),​ title="​camera 10 45", legend=false,​ size=[800,​500]);​ + scatter!(ptl,​ xr, yr, zr, c=gray(1:​n)) + + ptr= plot( deepcopy(pl),​ camera=(90,​45),​ title="​camera 90 45", legend=false ); + scatter!(ptr,​ xr, yr, zr) + + pbl= plot(xr, yr, zr, camera=(45,​45),​ title="​camera 45 45", legend=false ); + scatter!(pbl,​ xr, yr, zr) + + pbr= plot( deepcopy(pl),​ camera=(90,​90),​ title="​camera 90 90", legend=false ); + scatter!(pbr,​ xr, yr, zr) + + plot( ptl, ptr, pbl, pbr, layout=4 ) + savefig("​plotting/​surface-angle.png"​) + + ``` + + {{surface-angle.png}} + + ### Surface Plot + + ```juliaeval + + using Plots + x= [1.0:​0.1:​5;​];​ y= [4.0:​0.1:​9.0;​];​ f(x,y)= sin(x)*cos(y);​ + p1= surface( x, y, f,      camera=(30,​30),​ legend=false,​ title="​camera 30 30 (default)",​ size=[800,​500] ); # 30 azimuth, 30 elevation is the default + p2= surface( deepcopy(p1),​ camera=(30,​80),​ legend=false,​ title="​camera 30 80" ); ## note the deepcopy! + p3= surface( deepcopy(p1),​ camera=(80,​30),​ legend=false,​ title="​camera 80 30" ); + p4= surface( deepcopy(p1),​ camera=(80,​80),​ legend=false,​ title="​camera 80 80" ); + plot( p1, p2, p3, p4 , layout=4 ​ ) + savefig( "​plotting/​surface-cameras.png"​ ); + + ``` + + {{surface-cameras.png}} + + ### Wireframe Plot + + ```juliaeval + + using Plots + x= [1.0:​0.1:​5;​];​ y= [4.0:​0.1:​9.0;​];​ f(x,y)= sin(x)*cos(y);​ + pl= plot(x, y, f, title="​camera 30 30 (default)",​ legend=false,​ seriestype=:​wireframe,​ size=[800,​500]);​ + pr= plot( deepcopy(pl),​ camera=(80,​60),​ title="​camera 80 60", legend=false,​ seriestype=:​wireframe);​ + plot( pl, pr, layout=2 ) + savefig("​plotting/​wireframe-angle.png"​) + + ``` + + {{wireframe-angle.png}} + + ### Contour Plot + + Contour needs a function as the third argument. ​ It does not work with a set of points! + + #### Regular Grid + + Contour With Parameters + + ```juliaeval + + using Plots + x= [1:5;]; y= [3:9;]; f(x,y)= x^2*sqrt(y);​ + contour( x, y, f, nlevels=100,​ width=2, size=[800,​480] ) + savefig("​plotting/​contour.png"​) + + ``` + + {{contour.png}} + + #### Irregular Grid + + Contour plots can work with irregular grids, but the values have to be sorted. + + ```juliaeval + + using Plots + f(x,y)= (3x + y^2) * abs(sin(x) + cos(y)) + x= sort( rand(100)*20 ) + y= sort( rand(100)*10 ) + contour(x,​y,​f,​ size=[800,​480]) + savefig("​plotting/​contour-surface-irreg.png"​) + + ``` + + {{contour-surface-irreg.png}} + + ### Colored Contour Plot (Contourf) + + ```juliaeval + + using Plots + x= [1:10;]; y= [0:9;]; f(x,y)= x^2*sqrt(y);​ + contourf( x, y, f, size=[800,​480] ) + savefig("​plotting/​contourf.png"​) + + ``` + + {{contourf.png}} + + ### Quiver (4-D Gradient) Plot + + ```juliaeval + + using Plots + u= collect(1:​10);​ + v=collect(1 : 0.1 : 1.9); + quiver(81:​90,​ 21:30, gradient=( 1:10, 5:-1:-4), legend=false) ##​ x and y, then draw gradient + scatter!(81:​90,​ 21:30) ## show the start of each arrow + savefig("​plotting/​quiver.png"​) + + ``` + + {{quiver.png}} + + ### Heat Plot + + ```juliaeval + + using Plots + xh= [8:32;]; yh= [2:32;]; f(x,y)= x*10+y; + annotations= [ (xh[i],​yh[j],​text(trunc(f(xh[i],​yh[j])),​7)) for i=1:​length(xh) for j=1:​length(yh)]; ​ ## 5-point font + heatmap( xh, yh, f, size=[800,​480],​ annotations=annotations,​ legend=false )  ## legend=false removes the sidebar + savefig("​plotting/​heatmap.png"​) + + ``` + + {{heatmap.png}} + + ## Gallery of Various Multi-Feature 3-D Plots + + ### Rosenbrock Surface Plot + + ```juliaeval + + using Plots + rosenbrock(x::​Vector{<:​Real})::​Float64= (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2; + x= -1.5:​0.1:​1.5;​ y= -1.5:​0.1:​1.5;​ z= Surface((x,​y)->​rosenbrock([x,​y]),​ x, y); + surface(x,​y,​z,​ linealpha=0.3,​ size=(600,​600),​ fc=:heat) + savefig("​plotting/​rosenhat.png"​) + + ``` + + {{rosenhat.png}} + + ### Spiral Plot + + ```juliaeval + + using Plots + tvec= 0:0.1:4*pi; + plot(tvec, sin.(tvec), cos.(tvec), legend=false) ​  ## legend here would just be "​y1"​ + savefig("​plotting/​spiral.png"​) + + ``` + + {{spiral.png}} + + ### Circler Plot + + ```juliaeval + + using Plots + N = 200; d = 8*pi/N; x= [d*n*cos.(d*n) for n in 0:N]; y= [d*n*sin.(d*n) for n in 0:N]; z= 0.0:N; + plot( x, y, z, marker=:​circle,​ legend=false) ​      ## legend here would just be "​y1"​ + savefig("​plotting/​circler.png"​) + + ``` + + {{circler.png}} + + ### Cool Spikes + + ```juliaeval + + using Plots + f(x,y)= (3x + y^2) * abs(sin(x) + cos(y)) + x= 0:0.1:20 + y= 0:0.1:10 + z= [f(i,j) for i in x, j in y] + plot(x,y,z, st=:​surface) + savefig("​plotting/​contour-surface.png"​) + + ``` + + {{contour-surface.png}} + + # Backmatter + + FIXME **Please suggest graphs that should be added, ideally with code.** + + 3-D plot on top, contour plot on bottom floor (and/or side walls) + + ## Unknown and/or Unillustrated Parameters + + plot( x, Y, z=); savefig( "​plotting/​ z.png" ); + + plot( x, Y, line_z=); savefig( "​plotting/​ line_z.png"​ ); + + plot( x, Y, marker_z=); savefig( "​plotting/​ marker_z.png"​ ); + + plot( x, Y, contour_labels=);​ savefig( "​plotting/​ contour_labels.png"​ ); + + plot( x, Y, contours=); savefig( "​plotting/​ contours.png"​ ); + + plot( x, Y, fill_z=); savefig( "​plotting/​ fill_z.png"​ ); + + plot( x, Y, levels=); savefig( "​plotting/​ levels.png"​ );  does this work for surface plot, too? + + plot( x, Y, match_dimensions=);​ savefig( "​plotting/​ match_dimensions.png"​ ); #TEXT ## Heatmap + + also, many seriestypes,​ such as path3d, scatter3d, contour3d, wireframe, volume, etc. + + cite http://​www.cas.cmc.osaka-u.ac.jp/​~paoon/​misc/​julia/​post/​trouble-plots-package/​ + + plot( x, Y, projection=polar);​savefig( "​plotting/​projectionpolar.png"​ );  # default = none ; also 3d + + ## Commonly Useful Packages on Julia Repository + + ## Notes + + ## References + + + + --- + + WARNING Do not edit this page.  It is autogenerated from `plotgallery3d.jl` with `mkplotgallerytxt.pl`. ​ The plots themselves are generated by running the julia files themselves. +