MATLAB Answers

How do I change the appearance of a tick label over a number of iterative values through a pre-exisitng tick label?

2 views (last 30 days)
Thomas Dixon
Thomas Dixon on 5 Apr 2020
Commented: Thomas Dixon on 5 Apr 2020
I have a plot which updates to create a video file. It plots a sweeping signal from 1-11GHz. I want to add the label of the signal to the yaxis (surface plot) at each point so i use
set(gca, 'YTick', unique([Sweep(i).Sig_Freq, get(gca, 'YTick')]));
Sweep(i) is each point in the sweep and its Sig_Freq field is the frequency of the signal. As you can imagine Sweep(i).data is anothe field which contains the corresponding data and I plot with:
s=surf(sx,sy,Sweep(i).data(7).WRspice(1:621,:).*1e6)
I then updata the ticks to give me the signal uniquely with
set(gca, 'YTick', unique([Sweep(i).Sig_Freq, get(gca, 'YTick')]));
I first would like to be ablet to adjust the signal label for example smaller font size and maybe red in colour so for all frequencies 1-10 maybe i can set yticklabel(2) to do that. But then how do i update that when yticklabel(2) is the 10GHz tick and the signal has swept over 10-11. I understand I could achieve this semi-manually by just uting a for sig_Freq<10 ....... then something else but i may want to change this for multiple different purposes and zoom in on plot thus changing the yticklabels etc..
An idea I had for what I would like is a perhaps some clever way of labelling this specific label so that at the point I add it to the ytick labels it has a label then I can always adress this partricular tick bsaed on its label or someting like that. Is this possible? and if so how?
What I want to achieve is a way to programtically add a specific ytick and yticklabel to a plot, then move this tick and label with subsequent updated plots through a pre-existing static ylabel and format this individual added yticklabel appearance for each plot without prior knoweldge of exactly where the static ylabel is. My current code looks like this and I am trying to load the Sweep structure but it is too large and I will look to make a reasonable MWE in the near future.

  1 Comment

Thomas Dixon
Thomas Dixon on 5 Apr 2020
I have answered part of this question myself. I can find where the number is in the lsit of Yticks by simply using
fs_i = find(ax.YTick==Sweep(i).Sig_Freq);
ax.YTickLabel(fs_i) = {[]};
This finds the signal frequnecy in the list of frequency labels on my plot and deletes it.
Unfortunately i cant do much else with it. really I would like to extend the tick marker for it make the tick marker red and the font and change the font style or something similar.

Sign in to comment.

Accepted Answer

dpb
dpb on 5 Apr 2020
Edited: dpb on 5 Apr 2020
Think you can do what you want or at least a reasonable facsimile thereof w/ basic 'TeX' interpreter -- 'LaTeX' is such a pain to deal with I'd not mess with it unless run into a real show-stopper without.
But, as far as the size, color, etc., see the document for TeX Markup in the documentation for at the link for 'Interpreter' in text function documentation. All that works for tick labels on a tick-by-tick value in the X/YTickLabel array
Just as a trivial example:
figure
plot(rand(10,1))
hAx=gca;
hAx.XTickLabel(3)={'\color{red}\fontsize{8}\bf 3'};
produces
where one sees the third tick label red and smaller fontsize and bolded in comparison to the default for the other. This is w/ defatul TeX interpreter.
As far as the thing about keeping some static label, I don't fully comprehend the intent or effect, but I'd guess what you may want there would be a text object with its specific handle to refer to as the static object instead of a tick label (which is just an element of an array on a 1:1 correspondence to the ticks for a given axis). But, as the above shows, you can do all kinds of effects thingies on those individually by setting the desired properties as interpreted text strings for any element of the array--and as the way it is effected above shows, that's dynamically adjustable simply by writing the desired array location.
BTW, retrieve and then use the specific axis handle instead of sprinkling calls to gca all over the code -- that ensures you're always programmatically addressing the axis which is the intended target instead of one the user may have brought into focus.

  4 Comments

Show 1 older comment
dpb
dpb on 5 Apr 2020
"...can [I] then apply the ax.XTickRotation to just this point and extend the tick line?"
No. MATLAB implementation of TeX subset can set '\it' or '\sl' for italic or oblique font (generally the same thing) but not a character/string rotation on character basis.
Not sure about whether LaTeX does that or not.
The axis properties set via set or the dot notation are global and unless are array values, are unique to the axis, not the elements of the pieces of the axis. So, ticks and tick labels are arrays and so have separate data, but the other properties associated with them are one size fits all.
dpb
dpb on 5 Apr 2020
"...then after that I will want to adress the XTickLabel(4) and then for arbitrary positioning on the xaxis I wouldn't know which one to adress. However I think I have got round this by adding the ticklabel to the aray then refinding where it is"
I don't see that you have any choice but to somewhere keep track of which tick/tick label you want to address by some lookup or keeping a running pointer to where it is you want to address/modify the label.
It seems you're not modifying the label value from what the tick value is, however, just changing its appearance so you don't need to keep the tick label and search through an arbitrary string to find the numeric value; use the ticks themselves for that. Given the kind of range of values you described above, you might find |ismembertol| useful to find the tick within some value of the wanted value or you could use interp1 to return the nearest, first before, first after, ... depending on how you are choosing which tick it is you want based on the given value you have to set the picking by/with.
Thomas Dixon
Thomas Dixon on 5 Apr 2020
Thank you both for the very comprehensive answers. I have accepted dpb's answer however note that ImageAnalyst gives a lot of detail in and fully worked example for many plot modifications.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Apr 2020
See attached demo that shows you how to control appearance of lots of things on an axes control.
% Demo to make a black graph with red Y axis, green X axis, and yellow grid.
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Create sample data.
X = 1 : 20;
Y = rand(1, 20);
plot(X, Y, 'gs-', 'LineWidth', 2, 'MarkerSize', 10);
grid on;
title('Y vs. X', 'FontSize', 12);
% Make labels for the two axes.
xlabel('X Axis');
ylabel('Y axis');
% Get handle to current axes.
ax = gca
% This sets background color to black.
ax.Color = 'k'
ax.YColor = 'r';
% Make the x axis dark green.
darkGreen = [0, 0.6, 0];
ax.XColor = darkGreen;
% Make the grid color yellow.
ax.GridColor = 'y';
ax.GridAlpha = 0.9; % Set's transparency of the grid.
% Set x and y font sizes.
ax.XAxis.FontSize = 15;
ax.YAxis.FontSize = 24;
% The below would set everything: title, x axis, y axis, and tick mark label font sizes.
% ax.FontSize = 34;
% Bold all labels.
ax.FontWeight = 'bold';
hold off

  3 Comments

Thomas Dixon
Thomas Dixon on 5 Apr 2020
Thankyou for this compehensive response, ImageAnalyst. Lets now say that the point at x=9 is special to me and I want to highlight it. So i want to add a tick marker for it which is a different colour to the rest of the x-axis how would I do that?
Then suppose i Interpolated from x=9 to x= 11 at a spacing of 0.1 and added those points to the plot and then iteratively produced a series of graphs where i added and updated my 'special' xtick label to the new point added e.g figure(1) specialtick at x=9, figure(2) specialtick at x=9.1 and i did this through the tick x=10 which stays.
Thomas Dixon
Thomas Dixon on 5 Apr 2020
I don't know why in the first paragraph i asked that question I already know how to do that. I do that like this:
set(gca, 'XTick', unique([9, get(gca, 'XTick')]));
The rest still applies

Sign in to comment.

Sign in to answer this question.


Translated by