How to put a variable YTICK
Mostrar comentarios más antiguos
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
el 28 de Nov. de 2019
Try
yticks(yourVariable);
9 comentarios
Alejandro Fernández
el 28 de Nov. de 2019
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.
Alejandro Fernández
el 28 de Nov. de 2019
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

Alejandro Fernández
el 28 de Nov. de 2019
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);

Alejandro Fernández
el 28 de Nov. de 2019
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');

Alejandro Fernández
el 28 de Nov. de 2019
Categorías
Más información sobre Grid Lines, Tick Values, and Labels en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

