using PlotlyJS
Plotly javascript loaded.
To load again call
init_notebook(true)
WARNING: Method definition require(Symbol) in module Base at loading.jl:317 overwritten in module Main at /Users/sglyon/.julia/v0.5/Requires/src/require.jl:12.
The PlotlyJS.jl API has two main layers:
plotly.js figures are constructed by calling the (javascript) function:
Plotly.newPlot(graphdiv, data, layout)
where
graphdiv
is an html div
element where the plot should appeardata
is an array of JSON objects describing the various trace
s in the visualizationlayout
is a JSON object describing the layout properties of the visualization.In Julia we construct plots using the function
plot(data, layout)
where
data
is an Array
of traces (or a single trace)layout
is an instance of Layout
(NOTE: graphdiv
is hidden from the user and is a randomly generated UUID attached to the plot)
Let's look at how we can build these trace
s and a Layout
in Julia
Construct traces using the tracename as a function and setting keyword arguments:
trace1 = scatter(y=rand(10), marker_color="red",
marker_line_width=2.0)
trace2 = contour(x=1:5, y=1:10, z=randn(5, 10))
trace3 = bar(x=1:10, y=rand(1:10, 10), name="mybar")
bar with fields name, type, x, and y
marker_color
.{"marker": {"color": "red"}}
print(json(trace1, 2))
{ "y": [ 0.633990156621, 0.6236290008669474, 0.3200607908147035, 0.575944271085947, 0.25443412736669635, 0.534191469861913, 0.2830423111502989, 0.25638870636628286, 0.43323680589145663, 0.06281959363085088 ], "type": "scatter", "marker": { "line": { "width": 2.0 }, "color": "red" } }
Build a layout by constructing a Layout
object:
layout = Layout(xaxis=attr(range=[0, 10],
title="assets"),
yaxis_title="consumption",
title="??")
layout with fields margin, title, xaxis, and yaxis
attr
function. This allows you to create groups of nested attributes.foo=attr(x=1, y=2)
produces {"foo": {"x": 1, "y": 2}}
foo_x=1, foo_y=2
attr
can be used when building traces alsoprint(json(layout, 2))
{ "yaxis": { "title": "consumption" }, "xaxis": { "range": [ 0, 10 ], "title": "assets" }, "title": "??", "margin": { "r": 50, "l": 50, "b": 50, "t": 60 } }
To actually display a plot, we tie one or more traces together with a layout by calling the plot
function:
plot(trace1, layout)
# layout optional
plot(trace2)
plot([trace1, trace3], layout)
The plot
function has a number of other methods that try to make it a bit easier to construct simple plots
methods(plot)
Let's see a few of them in action
x = linspace(-5, 5, 50)
y1 = sin(x)
y2 = cos(x)
plot(x, y1)
# x optional and set attributes with keywords
plot(y1, marker_color="red", mode="markers")
# columns become traces
plot(x, [y1 y2], kind="bar")
# can pass layout
plot(x, [y1 y2], Layout(title="My sinusoids"))
# can plot functions
plot([sin, _ -> cos(_)], -6, 6, Layout(title="My sinusoids"),
marker_symbol="square", mode="markers+lines")
plot
:p1 = plot(...)
p2 = plot(...)
p3 = plot(...)
p4 = plot(...)
(h|v|hv)cat
syntax:[p1 p2] # 1 row 2 cols
[p1 p2 p3] # 1 row 3 cols
[p1, p2] # 2 rows 1 col
[p1 p2; p3 p4] # 2 rows 2 cols
rand_plot(n) = plot(scatter(x=1:n, y=randn(n)))
p1, p2, p3, p4 = [rand_plot(i) for i in [10, 20, 30, 40]]
[p1 p2]
[p1 p2 p3]
[p1, p2]
p = [p1 p2; p3 p4]
Interact.jl
to tie arbitrary widgets to plot updatesimport Plots # import, not using to avoid clash
Plots.plotlyjs()
npts = 125
d = randn(npts)
x = linspace(0, 2*π, npts)
y = copy(x)
sinx = sin(x)
cosx = cos(x)
z = cos(x') .+ sin(y);
WARNING: Method definition show(IO, Base.Multimedia.MIME{:text/plain}, Plots.Plot) in module Plots at /Users/sglyon/.julia/v0.5/Plots/src/output.jl:168 overwritten at /Users/sglyon/.julia/v0.5/Plots/src/output.jl:241.
Plots.plot(x, sinx, leg=false, color=:red, style=:dash)
Plots.contour(x, y, z, fill=true)
Plots.scatter!([2.5], [2.5], color=:black, ms=3, leg=false)
Plots.annotate!([(2.5, 2.5, Plots.text("This is 2.5, 2.5", 12, :white, :left))])
Plots.plot(sin,(x->sin(2x)), 0, 2π, line=4, leg=false, fill=(0, :orange))
x = 1:0.3:20
y = x
f(x,y) = sin(x) + cos(y)
Plots.contour(x, y, f, fill=true)
Styles are instaces of this type:
immutable Style
color_cycle::Vector
layout::Layout
global_trace::PlotlyAttribute
trace::Dict{Symbol,PlotlyAttribute}
end
Create them in 3 ways:
# Filling in fields
ggplot = let
axis = attr(showgrid=true, gridcolor="white", linewidth=1.0,
linecolor="white", titlefont_color="#555555",
titlefont_size=14, ticks="outside",
tickcolor="#555555"
)
layout = Layout(plot_bgcolor="#E5E5E5",
paper_bgcolor="white",
font_size=10,
xaxis=axis,
yaxis=axis,
titlefont_size=14)
gta = attr(marker_line_width=0.5, marker_line_color="#348ABD")
colors = ["#E24A33", "#348ABD", "#988ED5", "#777777", "#FBC15E",
"#8EBA42", "#FFB5B8"]
Style(layout=layout, color_cycle=colors, global_trace=gta)
end
Style with: - color_cycle: String["#E24A33","#348ABD","#988ED5","#777777","#FBC15E","#8EBA42","#FFB5B8"] - layout with fields font, margin, paper_bgcolor, plot_bgcolor, titlefont, xaxis, and yaxis - global_trace: PlotlyAttribute with field marker
# ggplot style, but scatter traces have square markers
square_ggplot = Style(ggplot,
trace=Dict(:scatter => attr(marker_symbol="square")))
Style with: - color_cycle: String["#E24A33","#348ABD","#988ED5","#777777","#FBC15E","#8EBA42","#FFB5B8"] - layout with fields font, margin, paper_bgcolor, plot_bgcolor, titlefont, xaxis, and yaxis - global_trace: PlotlyAttribute with field marker - trace: - scatter: PlotlyAttribute with field marker
# composing
big_title = Style(layout=Layout(titlefont_size=20))
big_ggplot = Style(ggplot, big_title)
Style with: - color_cycle: String["#E24A33","#348ABD","#988ED5","#777777","#FBC15E","#8EBA42","#FFB5B8"] - layout with fields font, margin, paper_bgcolor, plot_bgcolor, titlefont, xaxis, and yaxis - global_trace: PlotlyAttribute with field marker
There are two main ways to use a Style
:
use_style!(::Style)
plot
and Plot
functions accept akeyword argument style::Style
that sets the style for that plot only.
use_style!(ggplot)
plot([sin, cos],-6, 6, Layout(title="My title"))
# notice title for this plot is bigger
p= plot([sin, cos],-6, 6, Layout(title="My title"), style=big_ggplot)
savefig(p, "/Users/sglyon/Desktop/my_ggplot.pdf")
WARNING: Base.writemime is deprecated. likely near In[37]:1 WARNING: Base.writemime is deprecated. likely near In[37]:1 WARNING: Base.writemime is deprecated. likely near In[37]:1 WARNING: Base.writemime is deprecated. likely near In[37]:1 WARNING: Base.writemime is deprecated. likely near In[37]:1 WARNING: Base.writemime is deprecated. likely near In[37]:1 EOFError: read end of file in read at ./iobuffer.jl:88 [inlined] in read(::TCPSocket, ::Type{UInt8}) at ./stream.jl:787 in byteat at /Users/sglyon/.julia/v0.5/JSON/src/Parser.jl:55 [inlined] in current at /Users/sglyon/.julia/v0.5/JSON/src/Parser.jl:66 [inlined] in chomp_space! at /Users/sglyon/.julia/v0.5/JSON/src/Parser.jl:110 [inlined] in parse_value(::JSON.Parser.StreamingParserState{TCPSocket}, ::Type{T}) at /Users/sglyon/.julia/v0.5/JSON/src/Parser.jl:153 in #parse#2(::Type{Dict{String,Any}}, ::Function, ::TCPSocket) at /Users/sglyon/.julia/v0.5/JSON/src/Parser.jl:392 in macro expansion at /Users/sglyon/.julia/v0.5/Lazy/src/macros.jl:259 [inlined] in macro expansion at /Users/sglyon/.julia/v0.5/Blink/src/AtomShell/process.jl:94 [inlined] in (::Blink.AtomShell.##6#7{Blink.AtomShell.Electron})() at ./event.jl:68