Skip to content

Commit 87bc388

Browse files
plot title, misc text position changes
1 parent 2e7c8f3 commit 87bc388

6 files changed

+224
-4
lines changed

plotly/convertFigure.asv

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
function [data, layout, title] = convertFigure(f)
2+
% convertFigure - converts a matlab figure object into data and layout
3+
% plotly structs.
4+
% [data, layout] = convertFigure(f)
5+
% f - root figure object in the form of a struct. Use f = get(gcf); to
6+
% get the current figure struct.
7+
% data - a cell containing plotly data structs
8+
% layout - a plotly layout struct
9+
% title - a string with the
10+
%
11+
% For full documentation and examples, see https://plot.ly/api
12+
13+
14+
axis_num = numel(f.Children);
15+
16+
if ~strcmp('figure', f.Type)
17+
error('Input object is not a figure')
18+
end
19+
20+
if axis_num==0
21+
error('Input figure object is empty!')
22+
end
23+
24+
% placeholders
25+
data = {};
26+
data_counter = 1;
27+
annotations = {};
28+
annot_counter = 1;
29+
bar_counter = 0;
30+
layout = {};
31+
legend={};
32+
x_axis={};
33+
y_axis={};
34+
35+
% copy general layout fields
36+
layout = extractLayoutGeneral(f, layout);
37+
38+
% For each axes
39+
%TEMP: reverse order of children
40+
for i=axis_num:-1:1
41+
m_axis = get(f.Children(i));
42+
%TODO:do something about this add/replace thing...
43+
if strcmp('legend',m_axis.Tag)
44+
legend = extractLegend(m_axis);
45+
else
46+
%TODO:do something about this add/replace thing...
47+
if strcmp('axes',m_axis.Type) %%&& (strcmp('replace',m_axis.NextPlot) || strcmp('new',m_axis.NextPlot))
48+
[xid, yid, x_axis y_axis] = extractAxes(m_axis, layout, x_axis, y_axis);
49+
m_title = get(m_axis.Title);
50+
annot_tmp = extractTitle(m_title, x_axis{xid}, y_axis{yid});
51+
if numel(annot_tmp)>0
52+
annotations{annot_counter} = annot_tmp;
53+
annot_counter = annot_counter+1;
54+
end
55+
data_num = numel(m_axis.Children);
56+
if data_num>0
57+
% For each data object in a given axes
58+
for j=1:data_num
59+
m_data = get(m_axis.Children(j));
60+
%display(['Data child ' num2str(j) ' is of type ' m_data.Type])
61+
62+
if strcmp('line',m_data.Type)
63+
%line scatter plot
64+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
65+
data_counter = data_counter+1;
66+
end
67+
if strcmp('text',m_data.Type)
68+
%annotation
69+
annot_tmp = extractDataAnnotation(m_data, xid, yid);
70+
if numel(annot_tmp)>0
71+
annotations{annot_counter} = annot_tmp;
72+
annot_counter = annot_counter+1;
73+
end
74+
75+
end
76+
77+
if strcmp('patch',m_data.Type)
78+
%area plot
79+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
80+
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap);
81+
data_counter = data_counter+1;
82+
end
83+
if strcmp('hggroup',m_data.Type)
84+
85+
%TODO: improve condition to differentiate between
86+
%scatter and bar chart
87+
if isfield(m_data, 'BarLayout')
88+
%bar plot
89+
[data{data_counter} layout] = extractDataBar(m_data, layout, xid, yid, m_axis.CLim, f.Colormap);
90+
data_counter = data_counter+1;
91+
% copy in bar gaps
92+
layout.bargap = 1-m_data.BarWidth;
93+
layout.barmode = m_data.BarLayout(1:end-2);
94+
bar_counter = bar_counter+1;
95+
else
96+
if isfield(m_data, 'Marker') && numel(m_data.Marker)>0
97+
%scatter plot
98+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
99+
data_counter = data_counter+1;
100+
end
101+
if isfield(m_data, 'EdgeColor') && isfield(m_data, 'FaceColor')
102+
%area plot
103+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
104+
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap);
105+
data_counter = data_counter+1;
106+
end
107+
end
108+
109+
end
110+
111+
112+
113+
114+
end
115+
end
116+
117+
118+
end
119+
end
120+
end
121+
122+
% BAR MODIFY
123+
if bar_counter>1 && strcmp(layout.barmode, 'group')
124+
layout.bargroupgap = layout.bargap;
125+
layout.bargap = 0.3;
126+
end
127+
128+
% ANNOTATIONS
129+
layout.annotations = annotations;
130+
131+
132+
% LEGEND
133+
if numel(legend)==0
134+
layout.showlegend = false;
135+
else
136+
layout.legend = legend;
137+
layout.showlegend = true;
138+
end
139+
140+
141+
% Assemble axis
142+
for i = 1:numel(x_axis)
143+
if i==1
144+
eval('layout.xaxis=x_axis{1};')
145+
else
146+
eval(['layout.xaxis' num2str(i) '=x_axis{' num2str(i) '};'])
147+
end
148+
end
149+
150+
for i = 1:numel(y_axis)
151+
if i==1
152+
eval('layout.yaxis=y_axis{1};')
153+
else
154+
eval(['layout.yaxis' num2str(i) '=y_axis{' num2str(i) '};'])
155+
end
156+
end
157+
158+
159+
end

plotly/convertFigure.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
function [data, layout] = convertFigure(f)
1+
function [data, layout, title] = convertFigure(f)
22
% convertFigure - converts a matlab figure object into data and layout
33
% plotly structs.
44
% [data, layout] = convertFigure(f)
55
% f - root figure object in the form of a struct. Use f = get(gcf); to
66
% get the current figure struct.
77
% data - a cell containing plotly data structs
88
% layout - a plotly layout struct
9+
% title - a string with the title of the plot
910
%
1011
% For full documentation and examples, see https://plot.ly/api
1112

@@ -30,6 +31,7 @@
3031
legend={};
3132
x_axis={};
3233
y_axis={};
34+
title = '';
3335

3436
% copy general layout fields
3537
layout = extractLayoutGeneral(f, layout);
@@ -50,6 +52,7 @@
5052
if numel(annot_tmp)>0
5153
annotations{annot_counter} = annot_tmp;
5254
annot_counter = annot_counter+1;
55+
title = annot_tmp.text;
5356
end
5457
data_num = numel(m_axis.Children);
5558
if data_num>0

plotly/fig2plotly.asv

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function [response] = fig2plotly(varargin)
2+
% fig2plotly - plots a matlab figure object with PLOTLY
3+
% [response] = fig2plotly()
4+
% [response] = fig2plotly(gcf)
5+
% [response] = fig2plotly(f)
6+
% [response] = fig2plotly(gcf, plot_name)
7+
% [response] = fig2plotly(f, plot_name)
8+
% gcf - root figure object in the form of a double.
9+
% f - root figure object in the form of a struct. Use f = get(gcf); to
10+
% get the current figure struct.
11+
% plot_name - a string naming the plot
12+
% response - a struct containing the result info of the plot
13+
%
14+
% For full documentation and examples, see https://plot.ly/api
15+
16+
%default input
17+
f = get(gcf);
18+
plot_name = 'untitled';
19+
20+
switch numel(varargin)
21+
case 0
22+
case 1
23+
if isa(varargin{1}, 'double')
24+
f = get(varargin{1});
25+
end
26+
if isa(varargin{1}, 'struct')
27+
f = varargin{1};
28+
end
29+
plot_name = 'untitled';
30+
case 2
31+
if isa(varargin{1}, 'double')
32+
f = get(varargin{1});
33+
end
34+
if isa(varargin{1}, 'struct')
35+
f = varargin{1};
36+
end
37+
plot_name = varargin{2};
38+
otherwise
39+
error('Too many arguments!')
40+
end
41+
42+
43+
%convert figure into data and layout data structures
44+
[data, layout, title] = convertFigure(f);
45+
46+
% send graph request
47+
response = plotly(data, struct('layout', layout, ...
48+
'filename',plot_name, ...
49+
'fileopt', 'overwrite'));
50+
51+
display('Done! Check out your plot at:')
52+
display(response.url)
53+
54+
end

plotly/fig2plotly.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141

4242

4343
%convert figure into data and layout data structures
44-
[data, layout] = convertFigure(f);
44+
[data, layout, title] = convertFigure(f);
45+
46+
if numel(title)>0
47+
plot_name = title;
48+
end
4549

4650
% send graph request
4751
response = plotly(data, struct('layout', layout, ...

plotly/fig2plotly_aux/extractDataAnnotation.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
data.x = d.Extent(1)+d.Extent(3)/2;
3737
data.y = d.Extent(2)+d.Extent(4)/2;
3838
data.align = d.HorizontalAlignment;
39-
data.xanchor = 'middle';
39+
data.xanchor = 'center';
4040
data.yanchor = 'middle';
4141

4242
%ARROW

plotly/fig2plotly_aux/extractTitle.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
data.y = ya.domain(1)+ (d.Extent(2)+d.Extent(4)/2 - ya.range(1))*yd_range / yr_range;
3737

3838
data.align = d.HorizontalAlignment;
39-
data.xanchor = 'middle';
39+
data.xanchor = 'center';
4040
data.yanchor = 'middle';
4141

4242
%ARROW

0 commit comments

Comments
 (0)