How to automatically determine grid size for a dataset?
3 次查看(过去 30 天)
显示 更早的评论
I have a loop that solves an equation using different values of parameters each time, which basically generates a new set of data for each iteration. I want to make a contour plot for each set of data but the problem is, I'd have to change the grid size every time to get the correct contours. Is there a way to automatically adjust the mesh, perhaps based on the maximum and minimum values in each dataset?
0 个评论
回答(1 个)
Nandini
2023-7-9
Yes, you can automatically adjust the mesh for contour plots based on the maximum and minimum values in each dataset. MATLAB provides the `caxis` function that allows you to set the color axis limits for the contour plot dynamically.
Here's an example of how you can adjust the mesh for contour plots based on the dataset's maximum and minimum values:
% Example loop that generates different datasets
for i = 1:numIterations
% Generate data for each iteration
data = generateData(i); % Replace with your own data generation code
% Create contour plot
figure;
contour(data);
% Adjust the mesh based on the maximum and minimum values in the dataset
caxis([min(data(:)), max(data(:))]);
% Add labels, title, etc. to the plot
xlabel('X');
ylabel('Y');
title(['Contour Plot - Iteration ', num2str(i)]);
% Other customizations for the plot
% Save the plot if needed
saveas(gcf, ['contour_plot_iteration_', num2str(i), '.png']);
end
In this example, within the loop, you generate a different dataset for each iteration. Then, you create a contour plot using the `contour` function. After that, you adjust the mesh using the `caxis` function by setting the color axis limits to the minimum and maximum values of the dataset (`min(data(:))` and `max(data(:))`). This ensures that the contour plot is scaled properly based on the dataset's range.
You can customize the plot further by adding labels, titles, legends, or any other desired customizations. Additionally, you can save the plots if needed using the `saveas` function.
Make sure to replace `generateData(i)` with your own code that generates the dataset for each iteration.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!