Skip to content

fixing issues #387, #389 and other ones #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

gilbertogalvis
Copy link
Contributor

This PR fix several issues. In particular:

issue #389

The most complicated was the issue Create Subplots with Polar Axes. This is because the axes used are polar and the previous matlab_plotly code was not robust enough with polar axes. This worked when using only one axes, but not with multiple axes (subplots). To solve this I had to recode several matlab_plotly functions. Specifically, to handle MATLAB polar plots, Plotly scatterpolar is used. The initial philosophy of matlab_plotly sets/creates Cartesian axes, therefore each function in the original code of matlab_plotly linked the traces used to Cartesian axes. The challenge lies, in this case, in linking scatterpolar with polar axes. To do this, the polar axes must be created using layout.polar and then linked to Plotly's scatterpolar using its subplot method.
Another challenge is handling empty axes. For this create a logic, which identifies the empty axes and labels them as 'nothing'. So when updateData.m receives a 'nothing' class it executes a special function that I coded (called updateOnlyAxes.m). This function only uses a trace with empty data so that matlab_plotly can display the respective empty axes.
I share below screenshots of results for all the examples in the code flow of the previous link.

Upper and Lower Subplots

Create a figure with two stacked subplots. Plot a sine wave in each one.

subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)

subplot(2,1,2); 
y2 = sin(5*x);
plot(x,y2)

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 15 15 AM

Quadrant of Subplots

Create a figure divided into four subplots. Plot a sine wave in each one and title each subplot.

subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')

subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')

subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')

subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 16 48 AM

Subplots with Different Sizes

Create a figure containing with three subplots. Create two subplots across the upper half of the figure and a third subplot that spans the lower half of the figure. Add titles to each subplot.

subplot(2,2,1);
x = linspace(-3.8,3.8);
y_cos = cos(x);
plot(x,y_cos);
title('Subplot 1: Cosine')

subplot(2,2,2);
y_poly = 1 - x.^2./2 + x.^4./24;
plot(x,y_poly,'g');
title('Subplot 2: Polynomial')

subplot(2,2,[3,4]);
plot(x,y_cos,'b',x,y_poly,'g');
title('Subplot 3 and 4: Both')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 17 58 AM

Replace Subplot with Empty Axes

Create a figure with four stem plots of random data. Then replace the second subplot with empty axes.

for k = 1:4
    data = rand(1,10);
    subplot(2,2,k)
    stem(data)
end

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 19 51 AM

% Replace Subplot with Empty Axes
for k = 1:4
    data = rand(1,10);
    subplot(2,2,k)
    stem(data)
end

subplot(2,2,2,'replace')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 20 08 AM

Subplots at Custom Positions

Create a figure with two subplots that are not aligned with grid positions. Specify a custom position for each subplot.

pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
y = magic(4);
plot(y)
title('First Subplot')

pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
bar(y)
title('Second Subplot')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 22 16 AM

Create Subplots with Polar Axes

Create a figure with two polar axes. Create a polar line chart in the upper subplot and a polar scatter chart in the lower subplot.

figure
ax1 = subplot(2,1,1,polaraxes);
theta = linspace(0,2*pi,50);
rho = sin(theta).*cos(theta);
polarplot(ax1,theta,rho)

ax2 = subplot(2,1,2,polaraxes);
polarscatter(ax2,theta,rho)

fig2plotly(f, 'offline', 1, 'TreatAs', {'scatterpolar', 'polarplot'});

Screen Shot 2021-10-12 at 9 23 20 AM

Modify Axes Properties After Creation

Create a figure with two subplots. Assign the Axes objects to the variables ax1 and ax2. Specify the Axes objects as inputs to the plotting functions to ensure that the functions plot into a specific subplot.

ax1 = subplot(2,1,1);
Z = peaks;
plot(ax1,Z(1:20,:))

ax2 = subplot(2,1,2);  
plot(ax2,Z)

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 24 42 AM

Make Subplot the Current Axes

Create a figure with multiple subplots. Store the Axes objects in vector ax. Then make the second subplot the current axes. Create a line chart and change the axis limits for the second subplot. By default, graphics functions target the current axes.

for k = 1:4
    ax(k) = subplot(2,2,k);
end

subplot(ax(2))
x = linspace(1,50);
y = sin(x);
plot(x,y,'Color',[0.1, 0.5, 0.1])
title('Second Subplot')
axis([0 50 -1 1])

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 26 24 AM

Convert Existing Axes to Subplot

Create a line chart. Then convert the axes so that it is the lower subplot of the figure. The subplot function uses the figure in which the original axes existed.

x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 27 33 AM

% Convert Existing Axes to Subplot
x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')

ax = gca;
subplot(2,1,2,ax)

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 28 32 AM

Convert Axes in Separate Figures to Subplots

Combine axes that exist in separate figures in a single figure with subplots.
Create two plots in two different figures. Assign the Axes objects to the variables ax1 and ax2. Assign the Legend object to the variable lgd.

figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 29 36 AM

figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')

ax1 = gca;

figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 31 31 AM

ax2 = gca;

Create copies of the two Axes objects using copyobj. Specify the parents of the copied axes as a new figure. Since legends and colorbars do not get copied with the associated axes, copy the legend with the axes.

fnew = figure;
ax1_copy = copyobj(ax1,fnew);
subplot(2,1,1,ax1_copy)

copies = copyobj([ax2,lgd],fnew);
ax2_copy = copies(1);
subplot(2,1,2,ax2_copy)

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 32 09 AM

Issue #387

I share below screenshots of results for all the examples in the code flow of the previous link.

Position Multiple Axes in Figure

Position two Axes objects in a figure and add a plot to each one.
Specify the position of the first Axes object so that it has a lower left corner at the point (0.1 0.1) with a width and height of 0.7. Specify the position of the second Axes object so that it has a lower left corner at the point (0.65 0.65) with a width and height of 0.28. By default, the values are normalized to the figure. Return the Axes objects as ax1 and ax2.

figure
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
ax2 = axes('Position',[0.65 0.65 0.28 0.28]);

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 40 15 AM

Add a plot to each Axes object.

Specify the axes by passing it as the first input argument to the graphics function. Most graphics functions reset some axes properties, such as the tick values and labels. However, they do not reset the axes position.

contour(ax1,peaks(20))
surf(ax2,peaks(20))

fig2plotly(gcf, 'offline', 1, 'treatAs', 'surf');

Screen Shot 2021-10-12 at 9 41 40 AM

Make Axes the Current Axes

Create two overlayed Axes objects. Then, specify the current axes and add a plot.
First create two Axes objects and specify the positions. Display the box outline around each axes. Return the Axes objects as ax1 and ax2.

figure
ax1 = axes('Position',[0.1 0.1 .6 .6],'Box','on');
ax2 = axes('Position',[.35 .35 .6 .6],'Box','on');

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 43 08 AM

Make ax1 the current axes. This action brings the axes to the front of the display and makes it the target for subsequent graphics functions. Add a line plot to the axes.

axes(ax1)
x = linspace(0,10);
y = sin(x);
plot(x,y)

fig2plotly(gcf, 'offline', 1);

Screen Shot 2021-10-12 at 9 44 33 AM

@gilbertogalvis gilbertogalvis merged commit 5c5c3c6 into master Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants