Using 3 “y” axes for multiple plot
Hello,
I am working with multiple plots in MATLAB and I would like to use three different "y" axis for the different axes within a single figure. I have no problem using two "y" axes with yyaxis, I can’t include that function in my for loop (this loop reads the data from each column to be plotted from the Excel file). I read about a function called addaxis, but I don’t know how to use it.
Also, I would like the plots to have different colors for each "y" axis, since MATLAB defaults to red for Y1 and blue for Y2. Any ideas on how to achieve this?
Any suggestions would be greatly appreciated!
Best Regards!
Lucas.
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB’, ‘NumberTitle’,’off’, ‘Color’, ‘w’);
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 100
yyaxis right
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘r’);
end
if max(y_i) > 100
yyaxis left
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’;
axis tight;
ax.XAxis.SecondaryLabelFormatMode = ‘manual’;
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]);
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g;
This code is working but It can only plot 2 "y" axes; but if I try the addaxis in order to implement the third "y" axis, my code It´s not working:
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB DQA’, ‘NumberTitle’,’off’, ‘Color’, ‘w’); %Preguntar si NumberTitle on o off
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 20
addaxis(t, y_i, [0 20],’LineWidth’,1.3,’Color’,’b’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, ‘SeriesIndex’, 1, ‘Marker’, ‘.’, ‘MarkerSize’, 1, ‘Color’, ‘green’);
end
if 20 < max(y_i) && max(y_i) <= 100
addaxis(t, y_i, [0 100], ‘LineWidth’,1.3,’Color’,’r’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘red’); %[‘Y_’ num2str(i-1)]
end
if max(y_i) > 100
addaxis(t, y_i, [0 max(y_i)],’LineWidth’,1.3,’Color’,’g’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva DiagnóstiQA’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’; %QUITAR ESTA LINEA DE CODIGO SI PREFERIMOS FORMATO TEMPORAL mm:ss
axis tight; %INSTRUCCIONES: tight, tickaligned o padded %en vez de axis puedo usar xlim o ylim si solo quiero ajustar 1 de los ejes
ax.XAxis.SecondaryLabelFormatMode = ‘manual’; %ESTO SOLUCIONA EL 31 -1!
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]); %El segundo numero es el margen desde el borde inferior de la ventana y el cuarto número es la altura de los ejes
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g;Hello,
I am working with multiple plots in MATLAB and I would like to use three different "y" axis for the different axes within a single figure. I have no problem using two "y" axes with yyaxis, I can’t include that function in my for loop (this loop reads the data from each column to be plotted from the Excel file). I read about a function called addaxis, but I don’t know how to use it.
Also, I would like the plots to have different colors for each "y" axis, since MATLAB defaults to red for Y1 and blue for Y2. Any ideas on how to achieve this?
Any suggestions would be greatly appreciated!
Best Regards!
Lucas.
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB’, ‘NumberTitle’,’off’, ‘Color’, ‘w’);
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 100
yyaxis right
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘r’);
end
if max(y_i) > 100
yyaxis left
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’;
axis tight;
ax.XAxis.SecondaryLabelFormatMode = ‘manual’;
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]);
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g;
This code is working but It can only plot 2 "y" axes; but if I try the addaxis in order to implement the third "y" axis, my code It´s not working:
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB DQA’, ‘NumberTitle’,’off’, ‘Color’, ‘w’); %Preguntar si NumberTitle on o off
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 20
addaxis(t, y_i, [0 20],’LineWidth’,1.3,’Color’,’b’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, ‘SeriesIndex’, 1, ‘Marker’, ‘.’, ‘MarkerSize’, 1, ‘Color’, ‘green’);
end
if 20 < max(y_i) && max(y_i) <= 100
addaxis(t, y_i, [0 100], ‘LineWidth’,1.3,’Color’,’r’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘red’); %[‘Y_’ num2str(i-1)]
end
if max(y_i) > 100
addaxis(t, y_i, [0 max(y_i)],’LineWidth’,1.3,’Color’,’g’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva DiagnóstiQA’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’; %QUITAR ESTA LINEA DE CODIGO SI PREFERIMOS FORMATO TEMPORAL mm:ss
axis tight; %INSTRUCCIONES: tight, tickaligned o padded %en vez de axis puedo usar xlim o ylim si solo quiero ajustar 1 de los ejes
ax.XAxis.SecondaryLabelFormatMode = ‘manual’; %ESTO SOLUCIONA EL 31 -1!
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]); %El segundo numero es el margen desde el borde inferior de la ventana y el cuarto número es la altura de los ejes
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g; Hello,
I am working with multiple plots in MATLAB and I would like to use three different "y" axis for the different axes within a single figure. I have no problem using two "y" axes with yyaxis, I can’t include that function in my for loop (this loop reads the data from each column to be plotted from the Excel file). I read about a function called addaxis, but I don’t know how to use it.
Also, I would like the plots to have different colors for each "y" axis, since MATLAB defaults to red for Y1 and blue for Y2. Any ideas on how to achieve this?
Any suggestions would be greatly appreciated!
Best Regards!
Lucas.
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB’, ‘NumberTitle’,’off’, ‘Color’, ‘w’);
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 100
yyaxis right
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘r’);
end
if max(y_i) > 100
yyaxis left
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’;
axis tight;
ax.XAxis.SecondaryLabelFormatMode = ‘manual’;
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]);
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g;
This code is working but It can only plot 2 "y" axes; but if I try the addaxis in order to implement the third "y" axis, my code It´s not working:
clc;
clear;
nombre_excel = ‘Hoja_fuente.xlsx’;
datos = readtable (nombre_excel);
encabezados = datos.Properties.VariableNames(2:end);
encabezados = strrep(encabezados, ‘_’, ‘ ‘);
t = datetime(datos.Tiempo,’ConvertFrom’, ‘datenum’, ‘Format’, ‘HH:mm:ss’); %Si da probemas: sustituir ‘datenum’ por ‘excel’
numero_columnas=size(datos,2) – 1;
fig = figure(‘Name’,’Representación interactiva MATLAB DQA’, ‘NumberTitle’,’off’, ‘Color’, ‘w’); %Preguntar si NumberTitle on o off
hold on;
grid on;
for i=2:numero_columnas+1
y_i = datos(:,i);
y_i = table2array(y_i);
if max(y_i) <= 20
addaxis(t, y_i, [0 20],’LineWidth’,1.3,’Color’,’b’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, ‘SeriesIndex’, 1, ‘Marker’, ‘.’, ‘MarkerSize’, 1, ‘Color’, ‘green’);
end
if 20 < max(y_i) && max(y_i) <= 100
addaxis(t, y_i, [0 100], ‘LineWidth’,1.3,’Color’,’r’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘red’); %[‘Y_’ num2str(i-1)]
end
if max(y_i) > 100
addaxis(t, y_i, [0 max(y_i)],’LineWidth’,1.3,’Color’,’g’)
plot(t, y_i, ‘LineWidth’, 1.5, ‘DisplayName’, encabezados{i-1}, SeriesIndex = 1, marker=’.’, markersize = 1, color = ‘b’); %[‘Y_’ num2str(i-1)]
end
end
title(‘Gráfica Interactiva DiagnóstiQA’);
legend(‘show’);
xlabel(‘Tiempo( hh:mm:ss )’, ‘FontSize’, 12);
%ylabel(encabezados, ‘Rotation’, 0, ‘Color’, ‘b’);
ax=gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = ‘HH:mm:ss’; %QUITAR ESTA LINEA DE CODIGO SI PREFERIMOS FORMATO TEMPORAL mm:ss
axis tight; %INSTRUCCIONES: tight, tickaligned o padded %en vez de axis puedo usar xlim o ylim si solo quiero ajustar 1 de los ejes
ax.XAxis.SecondaryLabelFormatMode = ‘manual’; %ESTO SOLUCIONA EL 31 -1!
set(gca, ‘Position’, [0.056770833333333,0.157667386609071,0.894270833333333,0.809935205183586]); %El segundo numero es el margen desde el borde inferior de la ventana y el cuarto número es la altura de los ejes
legend(‘Location’, ‘southoutside’, ‘NumColumns’, 6, ‘Orientation’, ‘horizontal’, ‘NumColumnsMode’,’manual’, ‘Interpreter’,’none’, ‘FontSize’, 8, ‘FontWeight’, ‘bold’, ‘FontName’, ‘Arial Narrow’, ‘box’, ‘on’);
set(legend, ‘IconColumnWidth’, 15, ‘Position’, [0.004678527128273,0.041036717062635,0.988802104021113,0.058815920456996]);
format long g; plot, label MATLAB Answers — New Questions