Plotting

Author

Marie-Hélène Burle

It can be convenient to plot directly in the REPL (for instance when using SSH).

using UnicodePlots
histogram(randn(1000), nbins=40)
                ┌                                        ┐ 
   [-3.6, -3.4)  1                                       
   [-3.4, -3.2)   0                                       
   [-3.2, -3.0)   0                                       
   [-3.0, -2.8)   0                                       
   [-2.8, -2.6)  2                                       
   [-2.6, -2.4)  2                                       
   [-2.4, -2.2)  4                                      
   [-2.2, -2.0)  4                                      
   [-2.0, -1.8) ██████ 16                                
   [-1.8, -1.6) ███████████ 28                           
   [-1.6, -1.4) ███████ 19                               
   [-1.4, -1.2) █████████████ 33                         
   [-1.2, -1.0) █████████████████ 43                     
   [-1.0, -0.8) ███████████████████████ 57               
   [-0.8, -0.6) ████████████████████████ 60              
   [-0.6, -0.4) █████████████████████████████ 71         
   [-0.4, -0.2) ███████████████████████████████ 76       
   [-0.2, -0.0) ████████████████████████████████████  88  
   [-0.0,  0.2) ██████████████████████████ 65            
   [ 0.2,  0.4) ███████████████████████████████ 76       
   [ 0.4,  0.6) ███████████████████████████████ 77       
   [ 0.6,  0.8) ████████████████████████ 61              
   [ 0.8,  1.0) ███████████████████ 48                   
   [ 1.0,  1.2) █████████████████ 43                     
   [ 1.2,  1.4) ███████████████ 38                       
   [ 1.4,  1.6) ████████████ 31                          
   [ 1.6,  1.8) ██████████ 26                            
   [ 1.8,  2.0) ████ 11                                  
   [ 2.0,  2.2) ██ 5                                     
   [ 2.2,  2.4) ██ 6                                     
   [ 2.4,  2.6) ██ 5                                     
   [ 2.6,  2.8)  1                                       
   [ 2.8,  3.0)  3                                      
                └                                        ┘ 
                                 Frequency                 

Most of the time however, you will want to make nicer looking graphs. There are many options to plot in Julia.

Plots is a convenient Julia package which allows to use the same code with several graphing backends such as the GR framework (great for speed), Plotly.js (allows interaction with your graphs in a browser), or PyPlot. The default backend is the GR framework.

StatsPlots is an enhanced version with added stats functionality.

Example:

# First run takes time as the package needs to compile
using StatsPlots
StatsPlots.histogram(randn(1000), bins=40)
Precompiling packages...
    898.3 msQuartoNotebookWorkerTablesExt (serial)
  1 dependency successfully precompiled in 1 seconds
Precompiling packages...
   1032.5 msQuartoNotebookWorkerJSONExt (serial)
  1 dependency successfully precompiled in 1 seconds
Precompiling packages...
   2217.9 msQuartoNotebookWorkerPlotsExt (serial)
  1 dependency successfully precompiled in 2 seconds

Here, we need to explicitly run StatsPlots.histogram rather than histogram to prevent a conflict with the function of the same name from the package UnicodePlots.