How to put a variable YTICK

4 views (last 30 days)
Alejandro Fernández
Alejandro Fernández on 28 Nov 2019
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';

Answers (1)

Image Analyst
Image Analyst on 28 Nov 2019
Try
yticks(yourVariable);
  9 Comments
Image Analyst
Image Analyst on 28 Nov 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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!