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)
Precompiling UnicodePlots
  ✓ Crayons
  ✓ MarchingCubes
  ✓ UnicodePlots
  3 dependencies successfully precompiled in 35 seconds. 41 already precompiled.
Precompiling IntervalSetsExt
  ✓ UnicodePlots → IntervalSetsExt
  1 dependency successfully precompiled in 1 seconds. 46 already precompiled.
Precompiling FreeTypeExt
  ✓ UnicodePlots → FreeTypeExt
  1 dependency successfully precompiled in 1 seconds. 53 already precompiled.
                ┌                                        ┐ 
   [-3.0, -2.8)  4                                      
   [-2.8, -2.6)  1                                       
   [-2.6, -2.4)  1                                       
   [-2.4, -2.2) █████ 11                                 
   [-2.2, -2.0) ██████ 15                                
   [-2.0, -1.8) █████ 12                                 
   [-1.8, -1.6) █████ 11                                 
   [-1.6, -1.4) ██████████ 23                            
   [-1.4, -1.2) ████████████████ 37                      
   [-1.2, -1.0) █████████████████████████ 57             
   [-1.0, -0.8) ████████████████████████ 53              
   [-0.8, -0.6) █████████████████████ 47                 
   [-0.6, -0.4) █████████████████████████████████ 73     
   [-0.4, -0.2) ██████████████████████████████ 68        
   [-0.2, -0.0) ████████████████████████████████ 72      
   [-0.0,  0.2) ██████████████████████████████ 68        
   [ 0.2,  0.4) ████████████████████████████████████  79  
   [ 0.4,  0.6) ████████████████████████████████ 72      
   [ 0.6,  0.8) ████████████████████████████ 62          
   [ 0.8,  1.0) ██████████████████████ 49                
   [ 1.0,  1.2) ██████████████████ 41                    
   [ 1.2,  1.4) ██████████████████████ 49                
   [ 1.4,  1.6) ██████████████ 32                        
   [ 1.6,  1.8) █████████ 21                             
   [ 1.8,  2.0) █████ 13                                 
   [ 2.0,  2.2) ████ 10                                  
   [ 2.2,  2.4) ██ 6                                     
   [ 2.4,  2.6)  3                                      
   [ 2.6,  2.8) ██ 6                                     
   [ 2.8,  3.0)  3                                      
   [ 3.0,  3.2)  1                                       
                └                                        ┘ 
                                 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 StatsPlots
  ✓ Arpack_jll
  ✓ Distances
  ✓ Widgets
  ✓ Arpack
  ✓ SentinelArrays
  ✓ Distances → DistancesSparseArraysExt
  ✓ TableOperations
  ✓ Distances → DistancesChainRulesCoreExt
  ✓ MultivariateStats
  ✓ NearestNeighbors
  ✓ Clustering
  ✓ StatsPlots
  12 dependencies successfully precompiled in 8 seconds. 225 already precompiled.

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.