- fill function: https://www.mathworks.com/help/matlab/ref/fill.html
- mean function: https://www.mathworks.com/help/matlab/ref/mean.html
Plotting area plots with reference lines
2 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147417/image.jpeg)
Above figure is a sample of my actual figure , the line is the average(mean) of the data . I need to plot an area plot with different colors before and after the average line. Is it possible in matlab. Am not much familiar with programming in matlab. Pls help
0 个评论
回答(1 个)
Balavignesh
2024-7-4
Hi Abhilash,
It is possible in MATLAB to plot an area plot with different colors before and after the average line. I am using my own sample data to demonstrate this. You could use the fill function to create the shaded areas. I plotted the mean line using plot function with a dashed line style. You can change the FaceColor and FaceAlpha properties to customize the colours and transparency of the shaded areas.
Here is an example code that might help you understand this concept better:
% Step 1: Generate Sample Data
x = linspace(0, 10, 100); % X-axis values
y = sin(x) + 0.5 * randn(1, 100); % Sample data with some noise
% Step 2: Calculate the Mean
mean_x = mean(x); % Calculate the mean of x
% Step 3: Create the Area Plot
figure;
hold on;
% Plot the area to the left of the mean with one color
fill([x(x <= mean_x), fliplr(x(x <= mean_x))], [y(x <= mean_x), zeros(size(y(x <= mean_x)))], 'r', 'FaceAlpha', 0.5, 'EdgeColor', 'none');
% Plot the area to the right of the mean with another color
fill([x(x > mean_x), fliplr(x(x > mean_x))], [y(x > mean_x), zeros(size(y(x > mean_x)))], 'b', 'FaceAlpha', 0.5, 'EdgeColor', 'none');
% Plot the mean line
plot([mean_x, mean_x], ylim, 'k--', 'LineWidth', 2);
% Add labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('Area Plot with Different Colors Before and After Vertical Mean Line');
legend('Left of Mean', 'Right of Mean', 'Mean Line');
hold off;
Kindly have a look at the following documentation links to have more information on:
Hope that helps!
Balavignesh
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!