[Reddit Cross Post] Problems with a contour diagram for a caogulation diagram
26 views (last 30 days)
Show older comments
I need someting simila to this

But I only can get this

I do not know how to use the program, so all is from AI
Can someone help me?
my code
% Diagrama de coagulación: parte 1
% Log[Al] vs pH con especies de aluminio
clear; clc; close all;
% ==== DATOS ====
pH_contour = [7.9,7.6,7.4,7.1,6.8,6.3,6.8,5.7,4.9,4.4,4.3,4.2,4.2];
log_Al = [-3.3,-3.0,-2.9,-2.6,-2.5,-2.3,-2.2,-2.0,-1.9,-1.9,-1.8,-1.7,-1.7];
Al2SO4_3 = [0.00001,0.00003,0.00004,0.00006,0.00009,0.00013,0.00019,0.00025,0.00031,0.00038,0.00044,0.00050,0.00056];
Z = [6.4,79.5,97.8,98.9,98.1,99.1,99.1,96.6,94.2,93.5,94.9,97.1,92.8];
% ==== MALLA REGULAR PARA INTERPOLACIÓN ====
xi = linspace(min(pH_contour), max(pH_contour), 50);
yi = linspace(min(log_Al), max(log_Al), 50);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(pH_contour, log_Al, Z, XI, YI, 'cubic');
% ==== CREAR FIGURA ÚNICA ====
figure('Position', [100, 100, 1200, 800]);
hold on; grid on; box on;
% ==== CONTORNO ====
contourf(XI, YI, ZI, 15, 'LineWidth', 0.5); % sin clabel
colormap(jet);
c = colorbar;
caxis([0 100]);
ylabel(c, 'Porcentaje de Remoción (%)', 'FontSize', 12);
c = colorbar;
caxis([0 100]);
ylabel(c, 'Porcentaje de Remoción (%)', 'FontSize', 12);
% === MOVER LA BARRA DE COLOR A LA DERECHA ===
c.Position(1) = 0.8; % mueve horizontalmente (más a la derecha)
c.Position(3) = 0.02; % ajusta el ancho de la barra
ylabel(c, 'Porcentaje de Remoción (%)', 'FontSize', 12, 'Rotation', 270, ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% ==== LÍNEAS DE ESPECIES ====
pH = linspace(0, 14, 200);
y_Al3 = -3.5294 .* pH + 13.176; % Al3+
y_AlOH2 = -2.3077 .* pH + 6.0769; % Al(OH)2+
y_AlOH4 = pH - 12.4; % Al(OH)4-
plot(pH, y_Al3, 'r', 'LineWidth', 1.8, 'DisplayName', 'Al^{3+}');
plot(pH, y_AlOH2, 'b', 'LineWidth', 1.8, 'DisplayName', 'Al(OH)^{2+}');
plot(pH, y_AlOH4, 'g', 'LineWidth', 1.8, 'DisplayName', 'Al(OH)_4^-');
% ==== CONFIGURAR EJES ====
xlabel('pH', 'FontSize', 14, 'FontWeight', 'bold');
ylabel('log [Al]', 'FontSize', 14, 'FontWeight', 'bold');
xlim([0 14]);
ylim([min(log_Al)-0.2, max(log_Al)+0.2]);
set(gca, 'FontSize', 12, 'LineWidth', 1.2);
% ==== SEGUNDO EJE (DERECHO) PARA [Al2(SO4)3] ====
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'), ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', 'none', ...
'YColor', 'r');
set(ax2, 'YLim', [min(Al2SO4_3) max(Al2SO4_3)], ...
'YTick', linspace(min(Al2SO4_3), max(Al2SO4_3), 6), ...
'YTickLabel', arrayfun(@(x) sprintf('%.5f', x), ...
linspace(min(Al2SO4_3), max(Al2SO4_3), 6), 'UniformOutput', false), ...
'FontSize', 12, 'LineWidth', 1.2);
ylabel(ax2, 'Concentración Al_2(SO_4)_3 (M)', ...
'FontSize', 14, 'FontWeight', 'bold', 'Color', 'r');
% ==== TÍTULO Y LEYENDA ====
title('Diagrama de Coagulación - Contorno de Remoción', ...
'FontSize', 16, 'FontWeight', 'bold');
legend('Location', 'southwest', 'FontSize', 12, ...
'EdgeColor', 'black', 'Color', 'white');
% ==== GRID Y ESTILO ====
grid on;
hold off;
2 Comments
Cris LaPierre
on 22 Oct 2025 at 14:30
You need a 2D dataset for Z. This approach is trying to turn vector inputs for X,Y, and Z into matrices.
Mathieu NOE
on 22 Oct 2025 at 16:48
the x,y,z data represent a narrow band - not interpolation method is able to create data from nothing.
You need to add more points to cover the area you want.
pH_contour = [7.9,7.6,7.4,7.1,6.8,6.3,6.8,5.7,4.9,4.4,4.3,4.2,4.2];
log_Al = [-3.3,-3.0,-2.9,-2.6,-2.5,-2.3,-2.2,-2.0,-1.9,-1.9,-1.8,-1.7,-1.7];
Al2SO4_3 = [0.00001,0.00003,0.00004,0.00006,0.00009,0.00013,0.00019,0.00025,0.00031,0.00038,0.00044,0.00050,0.00056];
Z = [6.4,79.5,97.8,98.9,98.1,99.1,99.1,96.6,94.2,93.5,94.9,97.1,92.8];
colormap('jet')
scatter3(pH_contour, log_Al, Z,50,Z,'filled')
colorbar
view(2)
hold on
plot(pH_contour, log_Al,'-')
xlabel('pH')
ylabel('log Al')
Answers (1)
Cris LaPierre
on 22 Oct 2025 at 18:23
Edited: Cris LaPierre
on 23 Oct 2025 at 1:59
Assuming you had the matrix of data used to create the contourf plot, somethink like this might work.
% Axes 1 - contour plot
f = figure;
ax1 = axes(f, 'NextPlot', 'add', 'YAxisLocation', 'right', 'Box', 'on');
x = linspace(4.1,8.2,20);
startValue = 1.7; % Starting value for logspace
endValue = 171.3; % Ending value for logspace
y = logspace(log10(startValue), log10(endValue));
[X,Y] = meshgrid(x,y);
ZI = rand(size(X))*120-10; % a matrix of random numbers
contourf(x,y,ZI)
% format axes 1
xlim([3 10])
xticks([])
ylim([1.7 171.3])
ylabel('concentration AI_2(SO_4),14 H_2O')
set(ax1, 'YScale', 'log');
yticks([1.7 17.1 171.3])
% Set colors and colorbar
C = hsv(25);
colormap(ax1,[0 0 0; C(1:9,:)])
clim([-11.2, 110.8])
c = colorbar(ax1);
c.Ticks = -11.2:12.2:110.8;
c.TickLabels = ["" 1:12.2:110.8];
% Axes 2 - line plots (drawn on top of contourf plot)
ax2 = axes(f, 'NextPlot', 'add', 'Color', 'none');
linkprop([ax1,ax2],{"Position"});
pH = linspace(0, 14, 200);
y_Al3 = -3.5294 .* pH + 13.176; % Al3+
y_AlOH2 = -2.3077 .* pH + 6.0769; % Al(OH)2+
y_AlOH4 = pH - 12.4; % Al(OH)4-
plot(pH, y_Al3, 'g-', 'LineWidth', 1.8, 'DisplayName', 'Al^{3+}');
hold on
plot(pH, y_AlOH2, 'b-', 'LineWidth', 1.8, 'DisplayName', 'Al(OH)^{2+}');
plot(pH, y_AlOH4, 'm-', 'LineWidth', 1.8, 'DisplayName', 'Al(OH)_4^-');
hold off
% Format axes2
xlabel('pH');
xlim([3 10])
ylabel('log [Al]');
ylim([-5 -3]);
legend(ax2,'Orientation','horizontal','Location','northoutside')
I would have used yyaxis left/right, but there appears to be a bug with that. Or at least with uistack with yyaxis axes as input.
0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

