Discontinuous y axis and dual y axis combined in one plot

Hello All!
I am hoping to accomplish a discontinuous axis for one plot to display outliers without making all other points look like they have no spread, and then plot another plot on a second y axis due to it being entirely different ranges. Breakaxis wasn't doing a great job accomplishing this, so I decided to create the code from scratch. Attached are the blue and red variables that are used in the code.
figure
hold on
t = tiledlayout(2,1,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [2 1];
ax2 = axes(t);
ax2.Layout.Tile = 1;
plot(ax2,x,Blue, 'b*')
yline(ax2,20,':');
ax2.XAxis.Visible = 'off';
ax2.Box = 'off';
ylim(ax2,[20 30])
ax1 = axes(t);
ax1.Layout.Tile = 2;
Blue = allPC3tau;
Red = allPC3Range;
x = ones(1, length(Blue));
hold on
plot(ax1,x, Blue, 'b*')
%plot(x, median(Blue), 'k*', 'MarkerSize', 5, 'LineWidth', 3) cannot add,
%as this hides previous data
yline(ax1,0.4,':');
ax1.Box = 'off';
ylim(ax1,[0 0.4])
% Link the axes
linkaxes([ax1 ax2], 'x')
ylabel('Tau') %Does not lie within the middle of the two despite axis linked
This creates a semi-correct categorical scatter plot, but tau is not over the entire plot, just the second most recent plot.
t.Ylabel('Tau') also does not work.
Additionally,
(see first screenshot)
Then when trying to add the second y axis on the right below the previous code, things begin to breakdown:
yyaxis right
x = 1.2 * ones(1, length(Red));
hold on
plot(x, Red, 'r+', 'MarkerSize', 3, 'LineWidth', 1);
plot(x, median(Red), 'k+', 'MarkerSize', 5, 'LineWidth', 3)
% Set up axes.
xlim([0.9, 1.3]);
ylabel('Total Fluorescence Intensity Change')
ax = gca;
ax.XTick = [1, 1.2];
ax.XTickLabels = {'Tau','FI Delta'};
xlabel('PC3 Alexa Stats')
hold off
This causes the 1st tile to shift the outliers to the second column, despite this being a [2 1] tiled layout. I effectively need to find a way to merge the tiles for the yyaxis right, or potentially create a terrible mess of a 4x4 tiled layout, where the yyaxis right spans over 2 of the tiles.
(see second screenshot)
That sounds like an absolute headache, so I turn to the community to see if y'all have much smarter solutions (as you always do) :).
Thanks y'all!
Nick

 Risposta accettata

load('Red.mat')
load('Blue.mat')
figure
hold on
t = tiledlayout(2,2,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [2 2];
bgAx.YColor = 'w';
ax2 = axes(t);
ax2.Layout.Tile = 1;
x = ones(1, length(Blue));
plot(ax2,x,Blue, 'bx', 'MarkerSize', 3, 'LineWidth', 0.5)
yline(ax2,22,'--');
ax2.XAxis.Visible = 'off';
ax2.Box = 'off';
ax2.YAxis.Color = 'b';
ylim(ax2,[22 30])
ax3= axes(t);
ax3.Layout.Tile = 2;
ax3.Layout.TileSpan = [2 1];
yyaxis left
yticks([])
ax3.YAxis(1).Color = 'w';
yyaxis right
ax3.YAxis(2).Color = 'r';
hold on
x = 5 * ones(1, length(Red));
plot(x, Red, 'r+', 'MarkerSize', 3, 'LineWidth', 0.5);
plot(x, median(Red), 'rhexagram', 'MarkerSize', 20, 'LineWidth', 2)
ylabel('Total Fluorescence Intensity Change', 'Color', 'r')
xlim([4.9, 5.1]);
ax3.XTick = [5];
ax3.XTickLabels = {'FI Delta'};
ax1 = axes(t);
ax1.Layout.Tile = 3;
hold on
x = ones(1, length(Blue));
plot(ax1,x, Blue, 'bx', 'MarkerSize', 3, 'LineWidth', 0.5)
ax1.YAxis.Color = 'b';
plot(x, median(Blue), 'bhexagram', 'MarkerSize', 20, 'LineWidth', 2)
yline(ax1,0.5,'--');
ax1.Box = 'off';
ylim(ax1,[0 0.5])
% Link the axes
linkaxes([ax1 ax2], 'x')
ylabel(t,'Tau', 'Color', 'b') %Does not lie within the middle of the two despite axis linked
xlim([0.9, 1.1]);
ax = gca;
ax.XTick = [1];
ax.XTickLabels = {'Tau'};
xlabel(t, 'PC3 Alexa Stats')
hold off
fontsize(gcf, 16, "points")
drawnow
exportgraphics(gcf, 'AlexaPC3SpreadTau_FIDelta.png', 'Resolution', 600)

Più risposte (1)

a lazy answer - use yyaxis in a y log scale so you still see the outliers group aside from the main data but no fancy axis break needed (again, a very lazy approach !)
figure(1),
yyaxis left
semilogy(1*ones(size(Blue)),Blue,'*b')
yyaxis right
semilogy(2*ones(size(Red)),Red,'*r')
xlim([0 3])
otherwise , some info's that might help you

8 Commenti

yet another lazy / low tech approach :
you can try to use tidelayout , but as I am very lazy today , I leave it up to you !
load('Red.mat')
load('Blue.mat')
figure(1),
is_out = Blue>1;
Blue_outlier = Blue(is_out);
Blue(is_out) = [];
subplot_tight(2,1,1),
plot(1*ones(size(Blue_outlier)),Blue_outlier,'*b')
xlim([0 3])
set(gca,'XTick',[]);
subplot_tight(2,1,2),
colororder({'k','r'})
yyaxis left
plot(1*ones(size(Blue)),Blue,'*b')
yyaxis right
plot(2*ones(size(Red)),Red,'*r')
xlim([0 3])
While I like the log approach for simplicity I do think it is slightly harder for a reader/reviewer to digest. The commented suggestion is basically what I have sans the second box, I was hoping to extend the yyaxis right to be the full layout.
For now I will not do an accepted answer, but I do sincerly appreciate it! I'll try to adjust the currently laid out code to try to get this to fit what I am needing. I'll love to hear other answers, however!
sorry, it's me again !
wanted to play with your code and finally got this :
load('Red.mat')
load('Blue.mat')
figure
colororder({'r','b'})
t = tiledlayout(2,1,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [2 1];
ax2 = axes(t);
ax2.Layout.Tile = 1;
x = ones(1, length(Blue));
plot(ax2,x,Blue, 'b*')
yline(ax2,20,'--');
ax2.XAxis.Visible = 'off';
ax2.Box = 'off';
ylim(ax2,[20 30])
ax1 = axes(t);
ax1.Layout.Tile = 2;
% Blue = allPC3tau;
% Red = allPC3Range;
plot(ax1,x, Blue, 'b*')
hold on
plot(x, median(Blue), 'bd', 'MarkerSize', 20, 'LineWidth', 3); % cannot add, as this hides previous data
yline(ax1,0.4,':');
ax1.Box = 'off';
ylim(ax1,[0 0.4])
% Link the axes
linkaxes([ax1 ax2], 'x')
ylabel('Tau') %Does not lie within the middle of the two despite axis linked
yyaxis right
x = 1.2 * ones(1, length(Red));
plot(x, Red, 'r+', 'MarkerSize', 3, 'LineWidth', 1);
plot(x, median(Red), 'rd', 'MarkerSize', 20, 'LineWidth', 3)
% Set up axes.
xlim([0.9, 1.3]);
ylabel('Total Fluorescence Intensity Change')
ax = gca;
ax.XTick = [1, 1.2];
ax.XTickLabels = {'Tau','FI Delta'};
xlabel('PC3 Alexa Stats')
love it, I still need to find a way to span the yyaxis right on both tiles. I'm beginning to believe I might need to find a way not using tiled layout. Thank you for workshopping this with me, as it is still not complete for the original request I don't think the as-of-yet-best answer should be marked as accepted, but I do sincerely appreciate the back and forth you have done. If I come up with anything frankensteining yours and some other code together, I will post it here. If I cannot figure anything out within a week I'll mark this most recent attempt as the accepted answer.
Thank you!!!
ok, challenge accepted !
I have other official duties to accomplish so I will patiently wait until end next week !
I got it, it is messy!! It required the thing that I was worried about adding: a 2,2 tiled layout, and took a LOT of coaxing to work. Oddly enough I have encountered a export graphics issue that I don't know how to fix and would love your help, but otherwise it looks great. You inspired me! :)
figure
hold on
t = tiledlayout(2,2,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [2 2];
bgAx.YColor = 'w';
ax2 = axes(t);
ax2.Layout.Tile = 1;
x = ones(1, length(Blue));
plot(ax2,x,Blue, 'b*', 'MarkerSize', 3, 'LineWidth', 0.5)
yline(ax2,20,'--');
ax2.XAxis.Visible = 'off';
ax2.Box = 'off';
ax2.YAxis.Color = 'b';
ylim(ax2,[22 30])
ax3= axes(t);
ax3.Layout.Tile = 2;
ax3.Layout.TileSpan = [2 1];
yyaxis left
yticks([])
ax3.YAxis(1).Color = 'w';
yyaxis right
ax3.YAxis(2).Color = 'r';
hold on
x = 5 * ones(1, length(Red));
plot(x, Red, 'r+', 'MarkerSize', 3, 'LineWidth', 0.5);
plot(x, median(Red), 'rd', 'MarkerSize', 20, 'LineWidth', 3)
ylabel('Total Fluorescence Intensity Change', 'Color', 'r')
xlim([4.9, 5.1]);
ax3.XTick = [5];
ax3.XTickLabels = {'FI Delta'};
ax1 = axes(t);
ax1.Layout.Tile = 3;
hold on
x = ones(1, length(Blue));
plot(ax1,x, Blue, 'b*', 'MarkerSize', 3, 'LineWidth', 0.5)
ax1.YAxis.Color = 'b';
plot(x, median(Blue), 'bd', 'MarkerSize', 20, 'LineWidth', 3)
yline(ax1,0.4,'--');
ax1.Box = 'off';
ylim(ax1,[0 0.4])
% Link the axes
linkaxes([ax1 ax2], 'x')
ylabel(t,'Tau', 'Color', 'b') %Does not lie within the middle of the two despite axis linked
% Set up axes.
xlim([0.9, 1.1]);
ax = gca;
ax.XTick = [1];
ax.XTickLabels = {'Tau'};
xlabel(t, 'PC3 Alexa Stats')
hold off
fontsize(gcf, 16, "points")
drawnow
Quite a few adds and cleanups, but it is all now looking like the attached screenshot.
Oddly though, when doing a:
exportgraphics(gcf, 'pathway.png', 'Resolution', 600)
it cuts off the right-hand diamond median. I have no idea why the visualization is different in the screenshot, vs the gcf .png. Any thoughts?
Thank you! I tried a few of them, wasn't a fan of how export_fig saved things, and instead of spending too much time trying to recreate the figure with the 200 options, I decided to just change the diamond function. I will repost on here to show the finished and accepted answer.
I appreciate all your help, it would've taken me quite some time without your help!! @Mathieu NOE

Accedi per commentare.

Categorie

Prodotti

Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by