only the first of the three subplots shows eveything including the points, grid, axis labels, and legend. The last two subplots only show the trend line and none of the extra features
1 view (last 30 days)
Show older comments
subplot(3,1,1); plot(mass,quarter,'rx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'),
hold on, plot(mass,half,'bx'), plot(mass,full,'gx'), legend('Quarter Bridge','Half Bridge','Full Bridge'),
line_quarter=polyfit(mass,quarter,1); plot_quarter_trend=polyval(line_quarter,mass); plot(mass,plot_quarter_trend,'r-'),
line_half=polyfit(mass,half,1); plot_half_trend=polyval(line_half,mass); plot(mass,plot_half_trend,'b-'),
line_full=polyfit(mass,full,1); plot_full_trend=polyval(line_full,mass); plot(mass,plot_full_trend,'g-'),
subplot(3,1,2); plot(mass,amp,'mx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'), legend('Amplifier'),
line_amp=polyfit(mass,amp,1); plot_amp_trend=polyval(line_amp,mass); plot(mass,plot_amp_trend,'m-'),
subplot(3,1,3); plot(mass,div,'cx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'), legend('Potential Divider'),
line_div=polyfit(mass,div,1); plot_div_trend=polyval(line_div,mass); plot(mass,plot_div_trend,'c-')
0 Comments
Answers (1)
Aashray
on 21 Aug 2025
In the first subplot, you explicitly did a hold on, so MATLAB kept all the plotted objects (quarter, half, full, and their trend lines). That’s why all markers + lines showed up in the legend.
But in the 2nd and 3rd subplots, there was no hold on.
So, what happened is after plotting the 'mx' markers, the trend line (‘m-‘) line was immediately plotted. And since hold on wasn’t active, the second plot command replaced the first one on that subplot. That’s why the markers and legend entries for “Amplifier” or “Divider” weren’t visible.
To solve this, you can add hold on before plotting the trend line and use DisplayName with each plot instead of hardcoding legend early. Then call legend('show') at the end as shown below:
% Dummy data (since your data wasn’t available, I am generating some synthetic data on my own)
mass = (0:50:500)';
% Create synthetic linear-ish responses with some noise
quarter = 0.002*mass + 0.05*randn(size(mass));
half = 0.003*mass + 0.05*randn(size(mass));
full = 0.004*mass + 0.05*randn(size(mass));
amp = 0.010*mass + 0.1*randn(size(mass));
div = 0.0015*mass + 0.05*randn(size(mass));
subplot(3,1,1);
plot(mass,quarter,'rx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'),
hold on, plot(mass,half,'bx'), plot(mass,full,'gx'),
legend('Quarter Bridge','Half Bridge','Full Bridge'),
line_quarter=polyfit(mass,quarter,1);
plot_quarter_trend=polyval(line_quarter,mass);
plot(mass,plot_quarter_trend,'r-'),
line_half=polyfit(mass,half,1);
plot_half_trend=polyval(line_half,mass);
plot(mass,plot_half_trend,'b-'),
line_full=polyfit(mass,full,1);
plot_full_trend=polyval(line_full,mass);
plot(mass,plot_full_trend,'g-'),
hold off
subplot(3,1,2);
plot(mass,amp,'mx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'),
legend('Amplifier'),
hold on
line_amp=polyfit(mass,amp,1);
plot_amp_trend=polyval(line_amp,mass);
plot(mass,plot_amp_trend,'m-'),
hold off
subplot(3,1,3);
plot(mass,div,'cx'), grid on, xlabel('Load/g'), ylabel('Voltage/v'),
legend('Potential Divider'),
hold on
line_div=polyfit(mass,div,1);
plot_div_trend=polyval(line_div,mass);
plot(mass,plot_div_trend,'c-'),
hold off
You can observe that the legends and markers are visible in 2nd and 3rd plots in the above figure.
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!