Clear Filters
Clear Filters

Plotting area plots with reference lines

4 views (last 30 days)
Abhilash K S
Abhilash K S on 2 Mar 2015
Answered: Balavignesh on 4 Jul 2024 at 5:25
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

Answers (1)

Balavignesh
Balavignesh on 4 Jul 2024 at 5:25
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

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!