How to put a variable YTICK

Hello, everybody. The doubt I have is the following:
With the code that I attach at the bottom I get the graphic that I also attach. The thing is that in theY-axis I would like to put a label with the number that is really representing. The problem is that I want to do it depending on the value of the line. In other words, I don't want to manually put a fixed value in the middle of the yticks because it will change.
The code is adapted and the original source is this: ORIGINAL CODE
load Z.mat
close all
% Create plotting data and plot
x = 0:254; y = Z(:,:,1);
plot(x,y,'LineWidth',3)
hold on
plot(x,min(y)*ones(length(x)),'r--','LineWidth',2)
% yticks([min(y)])
hold off
xlim([0 255])
ylim([6.5 7])
ylabel('Valor del promedio [\mum]')
xticks(linspace(0,255,16))
% Get current axes object (just plotted on) and its position
ax1 = gca;
axPos = ax1.Position;
% Change the position of ax1 to make room for extra axes
% format is [left bottom width height], so moving up and making shorter here...
ax1.Position = axPos + [0 0.2 -0 -0.2];
% Exactly the same as for plots (above), axes LineWidth can be changed inline or after
ax1.LineWidth = 2;
title('Representación de la evolución del Promedio para Canny con TH_{MIN}=0.4\cdotTH_{MAX}')
grid on
% Add two more axes objects, with small multiplier for height, and offset for bottom
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.1 0 0], 'color', 'none', 'linewidth', 2);
% You can change the limits of the new axes using XLim
ax2.XLim = [0 255*0.4];
ax2.XTick = (linspace(0,255*0.4,18));
% You can label the axes using XLabel.String
ax1.XLabel.String = 'Threshold Máximo';
ax2.XLabel.String = 'Threshold Mínimo';

Respuestas (1)

Image Analyst
Image Analyst el 28 de Nov. de 2019

0 votos

Try
yticks(yourVariable);

9 comentarios

Alejandro Fernández
Alejandro Fernández el 28 de Nov. de 2019
I tried but the old ticks dissapear...
Promedio_Canny_1TH.jpg
Image Analyst
Image Analyst el 28 de Nov. de 2019
Let's see the actual call to yticks, and how you assigned the variable that you passed into it.
The first picture was with the auto ones and the other one with:
yticks(min(y))
And I need to combine the two, but I don't know where the minimum is going to be.
Image Analyst
Image Analyst el 28 de Nov. de 2019
min(y) is just a single number. yticks wants an array - a list of numbers for every tick mark. Are you sure you don't just want to set ylim() manually and let it automatically figure out the tick marks?
You can get the existing tick marks by using yticks() and then you can combine it if you want:
y = 1:20;
plot(y, 'b-', 'LineWidth', 2);
grid on;
oldTicks = yticks()
newTicks = unique(sort([min(y), oldTicks], 'ascend'))
yticks(newTicks);
oldTicks =
0 2 4 6 8 10 12 14 16 18 20
newTicks =
0 1 2 4 6 8 10 12 14 16 18 20
0000 Screenshot.png
Alejandro Fernández
Alejandro Fernández el 28 de Nov. de 2019
What I want to achieve is something similar to the image that I attach but doing it in MatLab.
The important thing is that the red dotted line is not always going to be in that place, it will be able to move, sometimes it will be in the minimum, other times in the maximum others in a point that is 6.7888, etc. And I need to show in the Y axis its value, as well as how you can see the rest of values of the Y axis
Data 1.jpg
Image Analyst
Image Analyst el 28 de Nov. de 2019
Just make a line by itself added on after you plot:
y = rand(1, 20);
plot(y, 'bs-', 'LineWidth', 2);
grid on;
hold on
miny = min(y)
line(xlim, [miny, miny], 'Color', 'r', 'LineStyle', '--', 'LineWidth', 2);
0000 Screenshot.png
Alejandro Fernández
Alejandro Fernández el 28 de Nov. de 2019
But the important thing is to see the min value (that as i told you is not always the min) in the Y axis.
Image Analyst
Image Analyst el 28 de Nov. de 2019
You can use text() to place a red text at the left end of the line.
y = rand(1, 20);
plot(y, 'bs-', 'LineWidth', 2);
grid on;
hold on
miny = min(y)
line(xlim, [miny, miny], 'Color', 'r', 'LineStyle', '--', 'LineWidth', 2);
str = sprintf('Ymin = %f', miny);
xl = xlim
text(xl(1), miny, str, 'FontSize', 14, 'Color', 'r', 'VerticalAlignment', 'bottom');
0000 Screenshot.png
Alejandro Fernández
Alejandro Fernández el 28 de Nov. de 2019
Ok. Thank you.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 28 de Nov. de 2019

Comentada:

el 28 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by