Absolute bar widths

6 visualizaciones (últimos 30 días)
John Joseph Valletta
John Joseph Valletta el 10 de Mzo. de 2011
Respondida: Jacob Mathew el 25 de Sept. de 2024
How can I specify absolute bar widths when overlaying a bar plot on top of a signal. I've got a simple time series on top of which I want to show discrete event in time. I want to show this by plotting a bar at the point in time where the event occurs, its height proportional to the 'size of the event'. I have two different events that I want to show in different colours and one having half the width of the other. I'm doing this at the moment.
%%%MATLAB CODE
subplot(3,1,2);
plot(Time,Signal1,'k');
hold on;
K=1;
bar1=bar(TimeEvent1,Event1,'b');
set(bar1,'BarWidth',K);
bar2=bar(TimeEvent2,Event2,'g');
set(bar2,'BarWidth',K/2);
datetick('x',15);
hold off
%%%Similar thing for the other subplots
When I do this I get different widths within the same subplot and also between the subplots. I want to specify an absolute width to use in all of my plots to be consistent.
Thank You : )
p.s The Time vectors are actual times i.e the number which represent the date '01-Jan-2010' for example.

Respuestas (1)

Jacob Mathew
Jacob Mathew el 25 de Sept. de 2024
Hey John,
You can set the barwidth property when calling the bar function itself. This will ensure that you can set the width to an aboslute value that you specify for any bar graph you want to plot.
The following example shows how you can plot two bar graphs behind a plot, with each bar graph having different barwidth values:
% Parameters for the sine wave
frequency = 1; % Frequency in Hz
amplitude = 1; % Amplitude of the sine wave
phase = 0; % Phase shift
fs = 100; % Sampling frequency
t = 0:1/fs:2*pi; % Time vector
% Generate the sine wave
sine_wave = amplitude * sin(2 * pi * frequency * t + phase);
% Calculate the absolute value of the amplitude
abs_amplitude = abs(sine_wave);
%% We calculate the amplitude value in discrete time segment to create bar graph
% Determine the number of samples per quarter cycle
samples_per_cycle = fs / frequency;
samples_per_quarter = samples_per_cycle / 4;
% Calculate average absolute amplitude for each quarter cycle
quarter_averages = [];
for i = 1:samples_per_quarter:length(abs_amplitude)
% Get the segment of the current quarter
segment = abs_amplitude(i:min(i+samples_per_quarter-1, end));
% Calculate the average of the segment
quarter_averages(end+1) = mean(segment);
end
% creating another array for 2nd bar graph with values that are 20% higher
quarter_averages_2 = quarter_averages * 1.2;
% Create a time vector for the discrete quarters
quarter_times = linspace(0, 2*pi, length(quarter_averages));
%% Plotting the sine wave and the bar graphs
figure;
% Plot the bar graph of the average absolute amplitude for each quarter
bar(quarter_times, quarter_averages, 'BarWidth', 1, 'FaceColor', [1 0 0], 'EdgeColor', 'black');
hold on;
bar(quarter_times, quarter_averages_2, 'BarWidth', 0.5, 'FaceColor', [0 1 0], 'EdgeColor', 'black');
% Plot the sine wave
plot(t, sine_wave, 'b', 'LineWidth', 2);
% Set plot labels and title
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave with Discrete Average Absolute Amplitude Bar Graph');
% Adjust plot limits
xlim([0 2*pi]);
ylim([-1.5 1.5]);
% Display grid
grid on;
% Hold off to stop adding to the current plot
hold off;
In the graph, you can see that the red bar graph width is wider than the green bar graph width.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by