Why is using Basevalue stretching my stacked bar chart instead of shifting it.
4 ビュー (過去 30 日間)
古いコメントを表示
I am attempting to create a stacked horizontal bar chart, which shows the trend of certain characteristics every minute over one hour (30 minutes either side of a specific event). I have managed to plot this with the x-axis showing 0-60 minutes, but this would be better represented as -30 - +30 minutes, with 0 representing the moment the event occurs.
When using Basevalue in an attempt to shift my entire bar plots to start at -30 instead of 0, the first "section" of the stacked bar plot is stretched to -30 and the rest of the data remains where it was before.
Anyone got any ideas about this? I have been banging my head against the wall for the past few days trying to change this. I attempted to change the data to be negative but ran into issues because bar charts are centred around the 0 point (meaning the first half of the data, whilst being displayed on the negative x-axis would be in reverse order), and as I am demonstrating characteristics over time the order of the data is important.
Thanks :)
This is the relevant bit of code I am using. The DoffColour bit isn't strictly relevant as it is just the way I am assigning colours to each block, but I left it in to show the whole for loop.
figure(1)
for i20=1:Doff
one=barh(i20,DoffValue(:,i20).','Stacked','FaceColor','Flat','Basevalue',-Time);
for i10=1:e
one(i10).CData=DoffColour2{i10,i20};
end
hold on
end
0 件のコメント
回答 (1 件)
albara
2023 年 4 月 26 日
編集済み: albara
2023 年 4 月 26 日
it sounds like you are trying to shift your stacked bar chart to start at -30 minutes using the BaseValue parameter, but the first section of the chart is getting stretched instead of shifted. This is happening because the BaseValue parameter sets the baseline value for each section of the stacked bar chart, which means that it will stretch or compress the bars based on their original position relative to the baseline.
To shift the entire chart to start at -30 minutes, you can modify the x-axis limits of your plot using the xlim function
For example, you can add the following line of code before your for loop to set the x-axis limits:
scss
xlim([-30 30])
This will shift your entire chart to start at -30 minutes and end at +30 minutes.
If you still want to use the BaseValue parameter to adjust the baseline of your stacked bar chart, you can try adjusting the values in your DoffValue matrix to reflect the shift in time. For example, if your DoffValue matrix originally contained values for 0-60 minutes, you can subtract 30 from each value to shift the data to -30 to +30 minutes. Then you can use a BaseValue of 0 to stack the bars on top of each other, with the shifted data already reflecting the desired x-axis range.
% Define the time range
Time = 30;
% Define the data
Doff = 4; % number of stacked bars
e = 6; % number of sections in each stacked bar
DoffValue = rand(e, 91); % randomly generated data for demonstration purposes
DoffColour2 = [1 0 0; % red
0 0 1; % blue
0 1 0; % green
1 0 1; % magenta
1 1 0; % yellow
0 1 1];% cyan
% Shift the data to reflect the new time range
DoffValueShifted = DoffValue(:, 31:91);
% Create the stacked bar chart
figure(1)
h = barh(DoffValueShifted, 'stacked');
colormap(DoffColour2);
xlim([-Time Time]);
% Assign colors to each section of the stacked bars
for i10 = 1:e
for i20 = 1:Doff
h(i20).CData(i10,:) = DoffColour2(i10,:);
end
end
% Add labels to the chart
xlabel('Characteristic Value')
ylabel('Time (minutes)')
title('Trends in Characteristic Values over Time')
legend('Section 1', 'Section 2', 'Section 3', 'Section 4', 'Section 5', 'Section 6', 'Location', 'southoutside', 'Orientation', 'horizontal')
Here, I've first defined the time range as 30 minutes (which is half of your original range of 0-60 minutes). the Doff and e variables to match your original code. I've also generated some randomly generated data for demonstration purposes, and assigned colors to each section of the stacked bars using the DoffColour2 variable.Then, I've shifted the DoffValue matrix to reflect the new time range by selecting only the columns corresponding to -30 to +30 minutes.
Next, I've created the stacked bar chart using the shifted DoffValueShifted matrix, and set the x-axis limits using xlim([-Time Time]) to start at -30 minutes and end at +30 minutes.
Finally, I've assigned colors to each section of the stacked bars using a nested loop that iterates over each color and each section of the stacked bars. This code should generate a stacked bar chart with the new time range and the correct colors assigned to each section of the stacked bars
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
GOODLUCK
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!