Plotting with PyPlot.jl

  • GitHub Repository
  • Python Documentation
  • Google and stackoverflow are your friends
  • Python has classes, Julia does not. Object members are mapped to dictionary entries. For instance:
    • Python: axis.annotate() -- Julia axis[:annotate]()

Imports


In [2]:
using PyPlot

Basics

Simple line scatter

  • Pass data directly as inputs

In [14]:
function linescatter()
    x = [1, 2, 3, 4]
    y = [10, 15, 13, 17]
    plot(x,y)
end
linescatter()


WARNING: Method definition linescatter() in module Main at In[13]:2 overwritten at In[14]:2.
Out[14]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x31f7d7da0>

Customize and add multiple traces to one plot

  • Multiple approaches docs
    • Plot each trace at a time - set hold(true) to keep traces on plot
    • Markers are strings: "o", "-o", "--", "-", "^"
    • Marker can take color: green trangle = "g^"

In [31]:
function multiple_scatter_traces()
    x=1:4
    y1 = [10, 15, 13, 17]
    y2 = [16, 5, 11, 9]
    y3 = [12, 9, 15, 12]
    y4 = [5, 10, 8, 12]
    plot(y1, label="marker", "o")
    hold(true)
    plot(y2, label="line+marker", "-o")
    plot(y3, label="line", "-")
    plot(y4, label="dash", "--")
end
multiple_scatter_traces()


WARNING: Method definition multiple_scatter_traces() in module Main at In[30]:2 overwritten at In[31]:2.
Out[31]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x31d615908>

Data labels and Figure Layout

  • Add the array of labels as an attribute to each trace
  • Layout modifies the "Figure Pane Attributes"

In [13]:
function data_labels()
    x = 1:5
    y1 = [1, 6, 3, 6, 1]
    y2 = [4, 1, 7, 1, 4]
    y1_labels = ["A-1", "A-2", "A-3", "A-4", "A-5"]
    y2_labels = ["B-a", "B-b", "B-c", "B-d", "B-e"]

    f, ax = subplots(1, 1)
    title("Data Labels on the Plot")
    plot(x, y1, label="Team A")
    plot(x, y2, label="Team B")
    ax[:legend](loc="upper left", shadow=true)
   
    for idx= 1:length(x)
        ax[:annotate](s=y1_labels[idx], xy = (x[idx], y1[idx]))
        ax[:annotate](s=y2_labels[idx], xy = (x[idx], y2[idx]))
        idx = idx + 1
    end    
end
data_labels()


WARNING: Method definition data_labels() in module Main at In[12]:2 overwritten at In[13]:2.

Area


In [9]:
function area1()
    fig, ax = subplots()
    y1=[0, 2, 3, 5] #, fill="tozeroy")
    y2=[3, 5, 1, 7] #, fill="tonexty")
    plot(y1, label="lines")
    plot(y2, label="lines")
    ax[:legend](loc="upper center", shadow=true)
end
area1()


WARNING: Method definition area1() in module Main at In[8]:2 overwritten at In[9]:2.
Out[9]:
PyObject <matplotlib.legend.Legend object at 0x3233a2b70>

Other visual interpretations of Matrix Data and Subplots


In [ ]:
function matrix_subplots()

end

matrix_subplots()

Advanced Layouts


In [ ]:
function advanced_layouts()

end
advanced_layouts()

Stats Plots


In [ ]:
function grouped_bar_example()

end

grouped_bar_example()

In [ ]:
function stacked_bar_example()
    
end()
stacked_bar_example()

In [ ]:
function two_hists()

end

two_hists()

Box (Whisker) Plots


In [ ]:
function box_plot()
   
end
box_plot()

Using DataFrames


In [ ]:
function data_frame_scatter()
    iris = dataset("datasets", "iris");
    display(head(iris))
end

data_frame_scatter()