How to plot dynamic amount of contour plots within one figure
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Frederic von Altrock
il 15 Dic 2023
Commentato: Mathieu NOE
il 15 Gen 2024
Hello,
I have a Problem and I need your help.
I have a Matlab Script that calculates three data tables that I need for plotting with contour().
Now I've managed to find a way that Matlab saves each of those 3 Datatables within a folder so that I am able to regain controll over those Informations after I've already plottet other plots with different input arguments.
My goal is that, e.g. I have plotted 5 Plots from which I've also have its data saved for each individual plot. Now I would be able to plot these again but I want to combine all of these Plots into one single Figure.
In the end I just want all 5 Plots within 1 Figure because I need to find out a way to create one new Plot possibly containing informations from all other plots combined before, therefore I wanted to use interpolation. From those 5 Plots that I've put over each other within one Figure I need to find out all of the 'lowest' points within this combined figure in order to create a new final plot containing all the relevant information that I wanted.
Thanks in advance
Risposta accettata
Mathieu NOE
il 3 Gen 2024
I modified a bit your combine function (see attachment)
I am not sure I generated the best scenario here so there maybe still some work to get exactly what you want
so I get these 3 original curves
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1581451/image.png)
and the final result (for the time being) looks like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1581456/image.png)
please double check and get back to me to further refinement
11 Commenti
Più risposte (3)
Dyuman Joshi
il 2 Gen 2024
In that case, concatenate the extracted points and use min() -
%Random data
x = 0:0.01:5;
y1 = sin(x);
y2 = cos(x);
y3 = abs(2.5-x)-1;
%plot the lines
plot(x, y1, 'g--', 'LineWidth', 2.5)
hold on
plot(x, y2, 'c-', 'LineWidth', 2.5)
plot(x, y3, 'b-.', 'LineWidth', 2.5)
%Concatenate and find the minimum
out = min([y1; y2; y3], [], 1);
%Plot the minimum line
plot(x, out, 'r', 'LineWidth', 2)
ylim([-2 3])
Mathieu NOE
il 2 Gen 2024
here's a small demo code for you
adapt to your data (horizontal or vertical concatenation depending if your data is row or column oriented)
% create some dummay data
x = (1:100)';
y1 = 0.5*exp(-(x-20).^2/100);
y2 = exp(-(x-40).^2/50);
y3 = 1.3*exp(-(x-60).^2/50);
% pick min values of all data
y_all = [y1 y2 y3]; % concat data
ymin = min(y_all,[],2); % pick the min of them
%plot
semilogy(x,y1,x,y2,x,y3)
hold on
semilogy(x,ymin,'r-*','linewidth',2)
4 Commenti
Shubham
il 27 Dic 2023
Hi Frederic,
To achieve your goal, you can follow these steps:
- Load the saved data tables for each of the individual plots from the folder.
- Create a figure and use subplot or tiledlayout and nexttile (for MATLAB R2019b or later) to arrange the plots in a single figure.
- Use contour to plot each data table in its respective subplot.
- To find the 'lowest' points across all plots, you can combine the data into one matrix and find the minimum values.
- Use interpolation if necessary to align the data points from different plots.
- Create a new plot with the combined information.
Here's an example MATLAB script that outlines this process:
% Step 1: Simulate loading the saved data tables
% For this example, I'll create some synthetic data and save it as if they were your actual data files
for i = 1:5
[X, Y] = meshgrid(linspace(-3, 3, 100), linspace(-3, 3, 100));
Z = peaks(X, Y) + i; % Slightly different peaks for each dataset
filename = sprintf('data%d.mat', i);
save(filename, 'X', 'Y', 'Z');
end
% Step 2: Create a figure and arrange subplots
figure;
t = tiledlayout(3, 2); % We have 5 individual plots and 1 combined plot
% Step 3: Plot each data table in its respective subplot
% Initialize a matrix to store the combined lowest points
combinedZ = inf(size(Z)); % Initialize with infinities for finding minimum
for i = 1:5
filename = sprintf('data%d.mat', i);
loadedData = load(filename);
nexttile; % Create a new tile for each individual plot
contour(loadedData.X, loadedData.Y, loadedData.Z);
title(sprintf('Plot %d', i));
% Step 4: Combine the data to find the 'lowest' points
combinedZ = min(combinedZ, loadedData.Z); % Element-wise minimum
end
% Step 6: Create a new plot with the combined information
% Span the new plot across two tiles at the bottom of the layout
nexttile(5, [1, 2]); % This will place the combined plot in the last row, spanning two columns
contour(loadedData.X, loadedData.Y, combinedZ); % Use the last loaded X and Y for plotting
title('Combined Plot with Lowest Points');
% Optionally, clean up by deleting the synthetic data files
for i = 1:5
filename = sprintf('data%d.mat', i);
delete(filename);
end
In this script, we first simulate the situation by creating 5 synthetic datasets and saving them as .mat files. We then create a figure with a tiled layout to accommodate all the individual plots and one combined plot. Each individual plot is created in a loop where we also keep track of the lowest points across all datasets. Finally, we create a combined contour plot that shows the lowest points.
Please note that in the combined plot, combinedZ contains the minimum value from each corresponding point across all datasets. This is visualized in the final contour plot titled "Combined Plot with Lowest Points".
Vedere anche
Categorie
Scopri di più su Axis Labels in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!