Clear Filters
Clear Filters

How to plot dynamic amount of contour plots within one figure

30 views (last 30 days)
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
  5 Comments
Frederic von Altrock
Frederic von Altrock on 2 Jan 2024
Thank you in advance for your help! I will take a look at it right away.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 3 Jan 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
and the final result (for the time being) looks like this
please double check and get back to me to further refinement
  11 Comments
Frederic von Altrock
Frederic von Altrock on 15 Jan 2024
Edited: Frederic von Altrock on 15 Jan 2024
Hey, sorry I'm only getting back to you now. I'm approaching my exams and haven't had much time these days. I already saw your suggestion last Friday and was surprised how simply and concisely you summarized my code part, simply forgot to reply.... I was able to take a lot from your suggestions :) and yes, my requirements for the program are more than fullfilled since the legend is fixed now. Many thanks again for your help, time and effort!
Best regards, Frederic

Sign in to comment.

More Answers (3)

Dyuman Joshi
Dyuman Joshi on 2 Jan 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])
  1 Comment
Frederic von Altrock
Frederic von Altrock on 2 Jan 2024
Hey, thanks for your help and additional answer, but unfortunately I'm not able to use the min(..) function like you did. I have already explained my problem to @Mathieu NOE in one of the answers below, thanks in advance!

Sign in to comment.


Mathieu NOE
Mathieu NOE on 2 Jan 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 Comments
Frederic von Altrock
Frederic von Altrock on 3 Jan 2024
Edited: Frederic von Altrock on 3 Jan 2024
Put them all in a separate folder if you want to run them.
Within main you can change the toolname, w0x and w0y to simulate random plots.
When you run combine, it will make the combined plot from all the simulated plots you ran in main before.
Thanks in advance!

Sign in to comment.


Shubham
Shubham on 27 Dec 2023
Hi Frederic,
To achieve your goal, you can follow these steps:
  1. Load the saved data tables for each of the individual plots from the folder.
  2. Create a figure and use subplot or tiledlayout and nexttile (for MATLAB R2019b or later) to arrange the plots in a single figure.
  3. Use contour to plot each data table in its respective subplot.
  4. To find the 'lowest' points across all plots, you can combine the data into one matrix and find the minimum values.
  5. Use interpolation if necessary to align the data points from different plots.
  6. 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".
  1 Comment
Frederic von Altrock
Frederic von Altrock on 2 Jan 2024
Hey, as I mentioned in my answer above, I may have misspoken my requirements for the final plot. Thanks for your reply though! I was able to use some of your code and implement it in my script which resulted in these plots:
You can see an image in my answer above that shows how I would like to combine my plots to create my desired result plot. I'm currently working on a way to combine all of these into a single plot and use e.g. interpolation to get all my corresponding points to create the plot I want.
Thanks in advance for your help!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!