Plots in App Designer have very small y-axis
Show older comments
I am using UIAxes in app desiger to display images and graphs as specified by the user. The images work perfectly, using the answer here
https://www.mathworks.com/matlabcentral/answers/360670-imshow-in-app-designer-image-size-doesn-t-fit
However, I am having issues with the graphing aspect. It appears that MATLAB is scaling the figure y-axis based upon the differences in the y-data plotted.

In most cases, I can live with this since the data is still legible. However, changing what the x-axis represents (which is the whole point of graphing) can end up with some very bad results.

This is completely illegible. Furthermore, for an unknown reason, the last two graphs cut out my ytitle (the code is the same line for line, only variables change, and it works elsewhere.
Making the issue weirder, if I graph a single number (which I would assume would change the size of the yaxis to nothing from the above examples) ends up with a very nice looking graph...

Things I have tried:
Increasing the size of the plot in the same way as the images in the answer above
Scaling the data so that x-axis and y-axis will always be the same range (i.e. both will be from 0-80 even if the raw data is from 2-2.5 on the y-axis)
Trying to alter the answer here to work with appdesigner (it errors btw)
app.UIAxesS1.Units = 'normalized';
app.UIAxesS1.Position = [0 0 1 1];
Using pbaspect, which does nothing https://www.mathworks.com/help/matlab/creating_plots/aspect-ratio-for-2-d-axes.html#bvlnj5i-1
Changing the dataAspectRatio and PlotBoxAspectRatio in the axes properties tab and setting them to manual (has no effect)

Using the set position function to change the position of the plot
h = plot(....)
set(h,'Position',[0 0 1 1]) This one says it cannot work on a line property
set(app.UIAxes,'Position',[0 0 1 1]) This one just changes the UIAxes to fill the entire figure and doesn't change the plotted data
I am sure there is a super stupid error that I am making with parent/children things, but I am not sure what is happening. If anyone would like the other code that manipulates it for the images I can post that, but this scaling issue happens even without displaying any images. Thank you to anyone who has made it this far
Code to graph the data (xAxis, yAxis are my data points). Note: The ylim and xlim is the way I got enough room for my data labels
plot(app.UIAxesS2, xAxis, yAxis(2,:), 'k*--'); % to plot the data in xAxis and yAxis variables
title(app.UIAxesS2, []); % Removing title, adding axis labels
xlabel(app.UIAxesS2, xlab);
ylabel(app.UIAxesS2, 'Kt Values');
xticks(app.UIAxesS2, xAxis); % Changing ticks and tick labels for xAxis
app.UIAxesS2.XAxis.TickLabels = xlabs;
text(app.UIAxesS2, xAxis - 0.1, yAxis(2,:) + 0.3, num2cell(round(yAxis(2,:),2))); %Adding data labels
app.UIAxesS2.YDir = 'normal'; %Flipping direction of the y axis
ylim(app.UIAxesS2, [min(yAxis(2,:)) - 0.5, max(yAxis(2,:)) + 0.5]); %Refining the limits of the axis
xlim(app.UIAxesS2, [min(xAxis) - 0.5, max(xAxis) + 0.5]);
8 Comments
Adam Danz
on 25 Jul 2019
Are you using axis(UIaxesS2,'tight')?
Samuel Nelson
on 25 Jul 2019
Adam Danz
on 25 Jul 2019
I wasn't recommending that you use "axis tight"; I was checking if that was the problem and would have recommended removing that.
When you first open the app, the axes appear correctly but after plotting, the y axis shrinks. Is that correct? Could you provide the full code to the function where this happens?
Samuel Nelson
on 25 Jul 2019
Edited: Samuel Nelson
on 25 Jul 2019
This section below is problematic. Presumably one of your cases is the underscore character "_" because that's the only thing the switch-case is looking for and you didn't report any errors so the swich-case must be selecting one of the cases since there are variables later in the code that are dependent on those cases. Please read how to use a swich-case becase this looks like a major flaw.
switch '_' % <------ problem
case ...
case ...
case ...
end
There are other areas of concern but that one stuck out.
From App Designer, select your axes and then select the "Rulers" from the propery inspector dropdown list. Check that your YLimMode is set to "auto" (and x/z lim while you're at it).
I'm assuming you've tried shutting down your app, clearing everything, and then restarting it.
Samuel Nelson
on 25 Jul 2019
Samuel Nelson
on 25 Jul 2019
Adam Danz
on 25 Jul 2019
"The entire function is only called if an underscore is detected within the searchString string "
1) I gather that "searchString" is always one of the follow forms
- _ABC For case 1
- A_BC for case 2
- AB_C For case 3
- ABCDE_F 6
- ABCDEF_G 7
- ABCDEFGHI_J 10
A more readable way of doing that would be to find the index of the underscore
switch strfind(searchString,'_')
case 1
case 2
case 3
...
otherwise
error('Unexpected underscore location.')
end
2) Another thing that makes it difficult to follow your code are the non descriptive variable names that I'm sure mean something to you but "special", "str" and "xAxis" etc wouldn't communicate much to anyone else reading the code (I hope you know this is all constructive feedback). In fact the "xAxis" and "yAxis" are really hard to accept since they don't refer to an axis at all.
3) Looking at the 3rd case (only), the loop can be vectorized but it would probably be easer to read as a loop so I'll leave it.
4) Instead of length(), use numel(). (just for best-practice)
5) Avoid using common Matlab function names as variables.
[kTvalues, ~, ~, error] = retrieveReferenceValues(app, newSearch);
% |-----| I suggest changing this var name
6) see comments below; also I'm assuming there are no "error" values here since that would clear your data. Right?
if error(1) || error(2)
% If something failed, setting the value as NaN
% |--------------------------------------------| Good idea
yAxis(:,i) = []; % }
xlabs(i) = []; % }
xAxis(i) = []; % }
else
% Make them NaNs to avoid error here:
% plot(app.UIAxesS1, xAxis, yAxis(1,:), 'k*--');
7) when the plotting begins, have you verfied that
- xAxis equals [20 30 40 50 60 70 80] ?
- yAxis is not all 0s ?
8) is this really needed? app.UIAxesS1.YDir = 'normal';
I haven't seen anything in your code that would collapse the y axis. The screen shots of the axis parameter values also look OK so this is puzzling. If you can create a super easy way to run the app with a push of a button, along with loaded data that recreates the problem, I could step through it.
Accepted Answer
More Answers (1)
Damien Engeler
on 15 Jun 2021
0 votes
Hello,
If it can help, I solved my almost same issue by changing in propreties section "viewing angle" the setting "CameraViewAngleMode". I set it to "auto"
Good luck <3
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

