Plotting

Author

Marie-Hélène Burle

This section demos briefly the very basics of plotting in Julia.

You will learn a lot more about plotting in the next section.

Plotting in the REPL

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)  3                                      
   [-3.0, -2.8)  2                                       
   [-2.8, -2.6)  3                                      
   [-2.6, -2.4)  2                                       
   [-2.4, -2.2)  1                                       
   [-2.2, -2.0) ███ 10                                   
   [-2.0, -1.8) ████ 14                                  
   [-1.8, -1.6) ███ 9                                    
   [-1.6, -1.4) █████████ 27                             
   [-1.4, -1.2) █████████████ 39                         
   [-1.2, -1.0) ████████████ 36                          
   [-1.0, -0.8) █████████████████ 50                     
   [-0.8, -0.6) ██████████████████████ 64                
   [-0.6, -0.4) ████████████████████████ 70              
   [-0.4, -0.2) ██████████████████████ 65                
   [-0.2, -0.0) █████████████████████████ 75             
   [-0.0,  0.2) ███████████████████████████████████  101  
   [ 0.2,  0.4) ███████████████████████████ 78           
   [ 0.4,  0.6) ███████████████████████ 69               
   [ 0.6,  0.8) █████████████████████ 62                 
   [ 0.8,  1.0) ██████████████████ 53                    
   [ 1.0,  1.2) ██████████████ 42                        
   [ 1.2,  1.4) ██████████████ 42                        
   [ 1.4,  1.6) █████████ 26                             
   [ 1.6,  1.8) ██████ 19                                
   [ 1.8,  2.0) ██████ 18                                
   [ 2.0,  2.2)  5                                      
   [ 2.2,  2.4) ██ 8                                     
   [ 2.4,  2.6)  1                                       
   [ 2.6,  2.8)  3                                      
   [ 2.8,  3.0)  1                                       
   [ 3.0,  3.2)   0                                       
   [ 3.2,  3.4)  1                                       
                └                                        ┘ 
                                 Frequency                 

Plotting libraries

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...
   2400.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.