Skip to content

Commit 604b37b

Browse files
committed
fix subplot docs
1 parent 67fc479 commit 604b37b

File tree

6 files changed

+228
-30
lines changed

6 files changed

+228
-30
lines changed

docs/make.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ using Documenter
22
using PlotlyJS
33
using PlotlyBase
44

5+
PlotlyJS.set_default_renderer(PlotlyJS.DOCS)
6+
57
const THIS_DIR = dirname(@__FILE__)
68

79
# used in examples
810
using Distributions, HTTP, DataFrames, RDatasets, Colors, CSV, JSON
911
using Random, Dates, LinearAlgebra, DelimitedFiles
1012

1113
# to override display_dict below
12-
import Documenter.Utilities: display_dict, limitstringmime
13-
using Base64:stringmime
14-
15-
function display_dict(p::PlotlyBase.Plot)
16-
out = Dict{MIME,Any}()
17-
# Always generate text/plain
18-
out[MIME"text/plain"()] = limitstringmime(MIME"text/plain"(), p)
19-
svg_m = MIME"image/svg+xml"()
20-
out[svg_m] = stringmime(svg_m, p)
21-
out
22-
end
14+
# import Documenter.Utilities: display_dict, limitstringmime
15+
# using Base64:stringmime
16+
17+
# function display_dict(p::PlotlyBase.Plot)
18+
# out = Dict{MIME,Any}()
19+
# # Always generate text/plain
20+
# out[MIME"text/plain"()] = limitstringmime(MIME"text/plain"(), p)
21+
# svg_m = MIME"image/svg+xml"()
22+
# out[svg_m] = stringmime(svg_m, p)
23+
# out
24+
# end
2325

24-
display_dict(p::PlotlyJS.SyncPlot) = display_dict(p.plot)
26+
# display_dict(p::PlotlyJS.SyncPlot) = display_dict(p.plot)
2527

2628
## handle examples
2729
# Walk through each example in a file and get the markdown from `single_example`
@@ -101,7 +103,7 @@ makedocs(
101103
sitename="PlotlyJS",
102104
format=Documenter.HTML(
103105
assets=[
104-
asset("https://cdn.plot.ly/plotly-1.54.7.js")
106+
"include_plotlyjs.js"
105107
]
106108
),
107109
modules=[PlotlyJS, PlotlyBase],
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if (typeof require !== "undefined") {
2+
console.log("Trying to load plotly.js via requirejs");
3+
require.undef("plotly");
4+
requirejs.config({
5+
paths: {
6+
plotly: ["https://cdn.plot.ly/plotly-2.3.0.min"],
7+
},
8+
});
9+
require(["plotly"], function (Plotly) {
10+
window._Plotly = Plotly;
11+
});
12+
}

docs/src/examples/subplots.md

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ subplots1()
1919
function subplots2()
2020
p1 = linescatter1()
2121
p2 = linescatter2()
22-
p = [p1, p2]
22+
p = [p1; p2]
2323
p
2424
end
2525
subplots2()
@@ -42,29 +42,127 @@ subplots3()
4242

4343
```@example subplots
4444
function subplots_withcomprehension()
45-
hcat([plot(scatter(x = 1:5, y = rand(5))) for i in 1:3]...)
45+
hcat([plot(scatter(x=1:5, y=rand(5))) for i in 1:3]...)
4646
end
4747
subplots_withcomprehension()
4848
```
4949

5050
```@example subplots
5151
function subplots_withsharedaxes()
5252
data = [
53-
scatter(x=1:3, y=2:4),
54-
scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"),
55-
scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"),
56-
scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
53+
scatter(x=1:3, y=2:4),
54+
scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"),
55+
scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"),
56+
scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
5757
]
5858
layout = Layout(
5959
xaxis_domain=[0, 0.45],
6060
yaxis_domain=[0, 0.45],
6161
xaxis4=attr(domain=[0.55, 1.0], anchor="y4"),
6262
xaxis2_domain=[0.55, 1],
6363
yaxis3_domain=[0.55, 1],
64-
yaxis4=attr(domain=[0.55, 1], anchor="x4")
64+
yaxis4=attr(domain=[0.55, 1], anchor="x4")
6565
)
6666
plot(data, layout)
6767
end
6868
subplots_withsharedaxes()
6969
```
7070

71+
```@example subplots
72+
function with_make_subplots1()
73+
74+
# The `shared_xaxes` argument to `make_subplots` can be used to link the x
75+
# axes of subplots in the resulting figure. The `vertical_spacing` argument
76+
# is used to control the vertical spacing between rows in the subplot grid.
77+
78+
# Here is an example that creates a figure with 3 vertically stacked
79+
# subplots with linked x axes. A small vertical spacing value is used to
80+
# reduce the spacing between subplot rows.
81+
82+
p = make_subplots(rows=3, cols=1, shared_xaxes=true, vertical_spacing=0.02)
83+
add_trace!(p, scatter(x=0:2, y=10:12), row=3, col=1)
84+
add_trace!(p, scatter(x=2:4, y=100:10:120), row=2, col=1)
85+
add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=1, col=1)
86+
relayout!(p, title_text="Stacked Subplots with Shared X-Axes")
87+
p
88+
end
89+
with_make_subplots1()
90+
```
91+
92+
```@example subplots
93+
function with_make_subplots2()
94+
# The `shared_yaxes` argument to `make_subplots` can be used to link the y
95+
# axes of subplots in the resulting figure.
96+
97+
# Here is an example that creates a figure with a 2 x 2 subplot grid, where
98+
# the y axes of each row are linked.
99+
100+
p = make_subplots(rows=3, cols=2, shared_yaxes=true)
101+
add_trace!(p, scatter(x=0:2, y=10:12), row=1, col=1)
102+
add_trace!(p, scatter(x=20:10:40, y=1:3), row=1, col=2)
103+
add_trace!(p, scatter(x=3:5, y=600:100:800), row=2, col=1)
104+
add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=2, col=2)
105+
relayout!(p, title_text="Multiple Subplots with Shared Y-Axes")
106+
p
107+
end
108+
with_make_subplots2()
109+
```
110+
111+
```@example subplots
112+
function with_make_subplots3()
113+
# The `specs` argument to `make_subplots` is used to configure per-subplot
114+
# options. `specs` must be a `Matrix` with dimensions that match those
115+
# provided as the `rows` and `cols` arguments. The elements of `specs` may
116+
# either be `missing`, indicating no subplot should be initialized starting
117+
# with this grid cell, or an instance of `Spec` containing subplot options.
118+
# The `colspan` subplot option specifies the number of grid columns that the
119+
# subplot starting in the given cell should occupy. If unspecified,
120+
# `colspan` defaults to 1.
121+
122+
# Here is an example that creates a 2 by 2 subplot grid containing 3
123+
# subplots. The subplot `specs` element for position (2, 1) has a `colspan`
124+
# value of 2, causing it to span the full figure width. The subplot `specs`
125+
# element f or position (2, 2) is `None` because no subplot begins at this
126+
# location in the grid.
127+
p = make_subplots(
128+
rows=2, cols=2,
129+
specs=[Spec() Spec(); Spec(colspan=2) missing],
130+
subplot_titles=["First Subplot" "Second Subplot"; "Third Subplot" missing]
131+
)
132+
133+
add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=1)
134+
add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=2)
135+
add_trace!(p, scatter(x=[1, 2, 3], y=[2, 1, 2]), row=2, col=1)
136+
137+
relayout!(p, showlegend=false, title_text="Specs with Subplot Title")
138+
p
139+
end
140+
with_make_subplots3()
141+
```
142+
143+
```@example subplots
144+
function with_make_subplots4()
145+
# Here is an example that uses the `rowspan` and `colspan` subplot options
146+
# to create a custom subplot layout with subplots of mixed sizes.
147+
p = make_subplots(
148+
rows=5, cols=2,
149+
specs=[Spec() Spec(rowspan=2)
150+
Spec() missing
151+
Spec(rowspan=2, colspan=2) missing
152+
missing missing
153+
Spec() Spec()]
154+
)
155+
156+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1)
157+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2)
158+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1)
159+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1)
160+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1)
161+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2)
162+
163+
relayout!(p, height=600, width=600, title_text="specs examples")
164+
p
165+
end
166+
with_make_subplots4()
167+
```
168+

docs/src/examples/violin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ violin_side_by_side()
129129
```
130130

131131
```@example violin
132-
function violin_styled()
132+
function violin_style()
133133
y1 = vcat(abs.(20 .* rand(100)), rand(UInt16, 300) .* 500 ./ typemax(UInt16))
134134
y2 = [25.261999999999997, 66.5419, 98.2114, 0.09070629 ]
135135
box = attr(fillcolor="black", line_color="black", width=0.01)
@@ -157,6 +157,6 @@ function violin_styled()
157157
)
158158
plot([trace1, trace2], layout)
159159
end
160-
violin_styled()
160+
violin_style()
161161
```
162162

examples/subplots.jl

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010
function subplots2()
1111
p1 = linescatter1()
1212
p2 = linescatter2()
13-
p = [p1, p2]
13+
p = [p1; p2]
1414
p
1515
end
1616

@@ -29,24 +29,110 @@ end
2929

3030

3131
function subplots_withcomprehension()
32-
hcat([plot(scatter(x = 1:5, y = rand(5))) for i in 1:3]...)
32+
hcat([plot(scatter(x=1:5, y=rand(5))) for i in 1:3]...)
3333
end
3434

3535

3636
function subplots_withsharedaxes()
3737
data = [
38-
scatter(x=1:3, y=2:4),
39-
scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"),
40-
scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"),
41-
scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
38+
scatter(x=1:3, y=2:4),
39+
scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"),
40+
scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"),
41+
scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
4242
]
4343
layout = Layout(
4444
xaxis_domain=[0, 0.45],
4545
yaxis_domain=[0, 0.45],
4646
xaxis4=attr(domain=[0.55, 1.0], anchor="y4"),
4747
xaxis2_domain=[0.55, 1],
4848
yaxis3_domain=[0.55, 1],
49-
yaxis4=attr(domain=[0.55, 1], anchor="x4")
49+
yaxis4=attr(domain=[0.55, 1], anchor="x4")
5050
)
5151
plot(data, layout)
5252
end
53+
54+
function with_make_subplots1()
55+
56+
# The `shared_xaxes` argument to `make_subplots` can be used to link the x
57+
# axes of subplots in the resulting figure. The `vertical_spacing` argument
58+
# is used to control the vertical spacing between rows in the subplot grid.
59+
60+
# Here is an example that creates a figure with 3 vertically stacked
61+
# subplots with linked x axes. A small vertical spacing value is used to
62+
# reduce the spacing between subplot rows.
63+
64+
p = make_subplots(rows=3, cols=1, shared_xaxes=true, vertical_spacing=0.02)
65+
add_trace!(p, scatter(x=0:2, y=10:12), row=3, col=1)
66+
add_trace!(p, scatter(x=2:4, y=100:10:120), row=2, col=1)
67+
add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=1, col=1)
68+
relayout!(p, title_text="Stacked Subplots with Shared X-Axes")
69+
p
70+
end
71+
72+
function with_make_subplots2()
73+
# The `shared_yaxes` argument to `make_subplots` can be used to link the y
74+
# axes of subplots in the resulting figure.
75+
76+
# Here is an example that creates a figure with a 2 x 2 subplot grid, where
77+
# the y axes of each row are linked.
78+
79+
p = make_subplots(rows=3, cols=2, shared_yaxes=true)
80+
add_trace!(p, scatter(x=0:2, y=10:12), row=1, col=1)
81+
add_trace!(p, scatter(x=20:10:40, y=1:3), row=1, col=2)
82+
add_trace!(p, scatter(x=3:5, y=600:100:800), row=2, col=1)
83+
add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=2, col=2)
84+
relayout!(p, title_text="Multiple Subplots with Shared Y-Axes")
85+
p
86+
end
87+
88+
function with_make_subplots3()
89+
# The `specs` argument to `make_subplots` is used to configure per-subplot
90+
# options. `specs` must be a `Matrix` with dimensions that match those
91+
# provided as the `rows` and `cols` arguments. The elements of `specs` may
92+
# either be `missing`, indicating no subplot should be initialized starting
93+
# with this grid cell, or an instance of `Spec` containing subplot options.
94+
# The `colspan` subplot option specifies the number of grid columns that the
95+
# subplot starting in the given cell should occupy. If unspecified,
96+
# `colspan` defaults to 1.
97+
98+
# Here is an example that creates a 2 by 2 subplot grid containing 3
99+
# subplots. The subplot `specs` element for position (2, 1) has a `colspan`
100+
# value of 2, causing it to span the full figure width. The subplot `specs`
101+
# element f or position (2, 2) is `None` because no subplot begins at this
102+
# location in the grid.
103+
p = make_subplots(
104+
rows=2, cols=2,
105+
specs=[Spec() Spec(); Spec(colspan=2) missing],
106+
subplot_titles=["First Subplot" "Second Subplot"; "Third Subplot" missing]
107+
)
108+
109+
add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=1)
110+
add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=2)
111+
add_trace!(p, scatter(x=[1, 2, 3], y=[2, 1, 2]), row=2, col=1)
112+
113+
relayout!(p, showlegend=false, title_text="Specs with Subplot Title")
114+
p
115+
end
116+
117+
function with_make_subplots4()
118+
# Here is an example that uses the `rowspan` and `colspan` subplot options
119+
# to create a custom subplot layout with subplots of mixed sizes.
120+
p = make_subplots(
121+
rows=5, cols=2,
122+
specs=[Spec() Spec(rowspan=2)
123+
Spec() missing
124+
Spec(rowspan=2, colspan=2) missing
125+
missing missing
126+
Spec() Spec()]
127+
)
128+
129+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1)
130+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2)
131+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1)
132+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1)
133+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1)
134+
add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2)
135+
136+
relayout!(p, height=600, width=600, title_text="specs examples")
137+
p
138+
end

src/display.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616
function Base.show(io::IO, mm::MIME"text/html", p::SyncPlot)
1717
# if we are rendering docs -- short circuit and display html
1818
if get_renderer() == DOCS
19-
return show(io, mm, p.plot, full_html=false, include_plotlyjs="require")
19+
return show(io, mm, p.plot, full_html=false, include_plotlyjs="require-loaded")
2020
end
2121
show(io, mm, p.scope)
2222
end

0 commit comments

Comments
 (0)