How can I rescale a patternCustom plot?
9 Comments
@Scott,
One more thing I wanted to share — @dpb was genuinely trying to help you out. There are so many talented contributors on this platform, like @Stephen23, @ImageAnalyst, @Star Strider, @John D Errico, @Walter Robertson, @Torsten — and @dpb is certainly among them.
I’ve always respected his thoughts and opinions. We're all here to learn, grow, and make a difference — not just in our own work, but in the lives of students, scholars, and fellow contributors.
I won’t be on this platform much longer, but I’m grateful for the memories and the chance to make a positive impact. I hope you understand where I’m coming from.
Answers (3)
4 Comments
@dpb - You nailed it with the colormap mapping insight. Turns out `clim()` does the trick for forcing uniform color limits across the 3D spherical plots, and `PatternPlotOptions` with `MagnitudeScale` also works as an alternative. Appreciate you pointing in the right direction - saved @Scott from having to rebuild with primitives!
Hi @Scott,
Thanks for the additional information! It sounds like you're trying to plot multiple `patternCustom` plots in a tiled layout while ensuring that all the plots share the same magnitude scaling. I totally get that `patternCustom` can be a bit tricky when it comes to scaling the magnitude consistently across multiple plots.
In your case, since you're not using the default `'CoordinateSystem', 'rectangular'` or `'polar'`, you're likely working with *spherical coordinates* (phi and theta). And since `patternCustom` doesn't offer a straightforward way to rescale the magnitude across multiple plots, let me offer you two approaches: one without any toolbox (using basic MATLAB functions) and the other with the *Antenna Toolbox* if you still want to stick to `patternCustom`.
Solution Without Toolbox
If you're looking for a toolbox-free approach, here's a simple solution using *basic MATLAB functions* that rescale the magnitude and visualize it in a tiled layout. The code uses `surf`, `meshgrid`, and `normalize` to ensure the magnitude is consistent across subplots.
Example Code (No Toolbox)
% Example data (replace with actual data) az = linspace(0, 360, 100); el = linspace(-90, 90, 100); [Az, El] = meshgrid(az, el); MagE = sin(Az) .* cos(El); % Example E-field pattern data
% Normalize the magnitude to [0, 1] MagE_normalized = normalize(MagE, 'range');
% Create a tiled layout figure; tiledLayout = tiledlayout(2, 2); % Create a 2x2 layout for 4 plots
% Loop through each subplot for iax = 1:4 nexttile; % Move to the next subplot
% Select pattern data for each subplot if iax == 1 surf(Az, El, MagE); % Plot original data title('Original Magnitude'); elseif iax == 2 surf(Az, El, MagE_normalized); % Plot normalized data title('Normalized Magnitude'); elseif iax == 3 surf(Az, El, MagE * 0.1); % Scaled data title('Scaled by 0.1'); else surf(Az, El, MagE * 0.01); % Another scaling factor title('Scaled by 0.01'); end
% Ensure the axes are consistent across plots axis([-180 180 -90 90 0 1]); % Set common axis limits colormap jet; colorbar; % Show color scale end
See attached results without using toolbox.
This approach doesn't require any special toolboxes and should give you the consistent scaling across subplots that you're looking for. If this works for you, it could be a very clean solution.
Solution With Antenna Toolbox (Using `patternCustom`)
If you're still relying on the Antenna Toolbox and want to use `patternCustom`, here’s how you can manage multiple radiation pattern plots with consistent scaling.
Rescale the Data: You can normalize the magnitude before passing it to `patternCustom`, just like we did in the example above. This ensures that each plot has the same scaling.
Adjust Plot Settings: Use the CoordinateSystem, Slice and SliceValue options to control the plot’s appearance.
Multiple Plots in a Single Figure: patternCustom allows plotting multiple radiation patterns in the same figure by passing in different data sets.
Here’s an example of how you could use `patternCustom` with normalized data for consistent scaling:
Antenna Toolbox (Using `patternCustom`)
% Assuming you already have MagE, theta, and phi MagE_normalized = normalize(MagE, 'range'); % Normalize the magnitude
% Create a tiled layout for 4 plots figure; tiledLayout = tiledlayout(2, 2); % Create a 2x2 layout
% Plotting the patterns for iax = 1:4 nexttile; % Move to the next subplot if iax == 1 patternCustom(MagE, theta, phi); % Original data title('Original Magnitude'); elseif iax == 2 patternCustom(MagE_normalized, theta, phi); % Normalized data title('Normalized Magnitude'); elseif iax == 3 patternCustom(MagE * 0.1, theta, phi); % Scaled data title('Scaled by 0.1'); else patternCustom(MagE * 0.01, theta, phi); % Another scaling factor title('Scaled by 0.01'); end end
I just went through the Relevant Documentation for Toolbox Functions to find solution to your problem, here are the references:
patternCustom: Pattern Custom Documentation https://www.mathworks.com/help/antenna/ref/patterncustom.html
normalize: Normalize Documentation https://www.mathworks.com/help/matlab/ref/normalize.html
tiledlayout: Tiled Layout Documentation https://www.mathworks.com/help/matlab/ref/tiledlayout.html
If you're comfortable using basic MATLAB functions (like `surf` and `normalize`), you can achieve the same visual results without needing the Antenna Toolbox. However, if you still want to use `patternCustom`, you can normalize your data before passing it into the function to maintain consistent scaling across plots.
Let me know if you need further clarification on any part of this or if you’d like to explore other methods!
3 Comments
Hi @Scott,
I owe you an apology - You were right to be frustrated. So, I do completely understand about what actually happened after going through your posted comments. So, let me first address the caxis approach: You nailed it - this only fixed the colormap, not the physical size of the patterns. The 0.5-scale sphere still filled the entire plot when it should've been half the size. Now, the PatternPlotOptions disaster: I called it a "confirmed solution" but you systematically proved it doesn't work:
- The PatternOptions=po syntax failed (your MATLAB version)
- Passing po directly failed (wrong argument type)
- Using 'MagnitudeScale' failed (not a valid parameter)
But after looking closer at the docs and your plots, I was curious about what I was missing and the key thing was MagnitudeScale only works with the pattern() method from antenna objects, not patternCustom. Even if syntax worked, it would've been ignored. You were absolutely right about patternCustom being inflexible. Since you mentioned being open to other approaches, here's what actually works. And good news - you don't need to refactor much. The code calculates global limits first, then plots everything with consistent scaling:
clear; clc; close all;
%% Setup num_plots = 2; num_azimuth_idxs = 100; num_zenith_idxs = 100; scales = [1, 0.5];
phi = linspace(-180, 180, num_azimuth_idxs); theta = linspace(0, 180, num_zenith_idxs); [PHI, THETA] = meshgrid(phi, theta);
%% Generate data and find global max magE_all = cell(num_plots, 1); max_mag = -Inf;
for ii = 1:num_plots magE = abs(scales(ii) * cos(deg2rad(PHI + THETA))); magE_all{ii} = magE; max_mag = max(max_mag, max(magE(:))); end
%% Plot figure('Position', [100, 100, 1200, 500]); tiled_plot = tiledlayout(1, num_plots, 'TileSpacing', 'compact');
for ii = 1:num_plots nexttile; magE = magE_all{ii};
% KEY: Use magnitude as radius - this is what makes it work theta_rad = deg2rad(THETA); phi_rad = deg2rad(PHI); r = magE; % Smaller magnitude = smaller sphere
x = r .* sin(theta_rad) .* cos(phi_rad); y = r .* sin(theta_rad) .* sin(phi_rad); z = r .* cos(theta_rad);
surf(x, y, z, magE, 'EdgeColor', 'none', 'FaceColor', 'interp'); caxis([0 max_mag]); % Uniform color scale
axis equal; axis vis3d; colormap jet; colorbar; view(3);
% Add coordinate axes hold on; plot3([0 max_mag*1.2], [0 0], [0 0], 'r-', 'LineWidth', 2); plot3([0 0], [0 max_mag*1.2], [0 0], 'g-', 'LineWidth', 2); plot3([0 0], [0 0], [0 max_mag*1.2], 'b-', 'LineWidth', 2); hold off;
xlabel('X'); ylabel('Y'); zlabel('Z'); title(sprintf('Pattern %d (Scale: %.1f)', ii, scales(ii)));
axis_limit = max_mag * 1.3; xlim([-axis_limit axis_limit]); ylim([-axis_limit axis_limit]); zlim([-axis_limit axis_limit]);
lighting gouraud; camlight('headlight'); material dull; end
title(tiled_plot, 'Spherical 3D Radiation Patterns with Uniform Magnitude Scaling');
Results: please see attached.
So, I executed the code and it worked because of r = magEl By using magnitude directly as the radius:
- The 0.5-scale pattern is physically half the size (extends to ±0.5 instead of ±1)
- With uniform caxis([0 max_mag]), it uses middle-range colors (blues/cyans/greens) instead of the full spectrum
This solves exactly what you showed in your image - where both spheres were the same size but shouldn't have been. Now the right plot will be visibly smaller and use middle colors like you wanted.
No toolbox needed, just standard MATLAB functions.
Hope this finally works for you!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!