How to barplot with standard deviation?
91 次查看(过去 30 天)
显示 更早的评论
I've got following code so far:
if true
% code
end
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
figure;
hBars = bar(y,1.0); % create bar plot with y;
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold on;
load('standarddev.mat') % standarddev contains all calculated standard deviation and consists of 5 %rows
std = [sd1,sd2,sd3,sd4,sd5]; % define y-axis
errorbar(x,y,std);
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
The plot itself works fine. I'd like to include the errorbars for the standard deviation as well but that unfortunately doesn't work at the moment. I've already had a look at the MATLAB Documentation Center bt without success.
Any help would be appreciated.
采纳的回答
更多回答(2 个)
Pinga
2014-7-10
Thank you, I should have done that. In the meanwhile I did but it somehow still doesn't work. Only about the half of all errorbars are plotted and still not placed in the center top of the bars (see attachment).
Since I'm new to MATLAB, I would thankful for every suggestion on my code.
if true
% code
end
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
load('Stddeviation.mat')
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
e = [std1,std2,std3,std4,std5,std6]; % define standard deviation
figure;
hBars = bar(y,1.0,'gropued');
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold all;
for i = 1:6;
j = 1:4;
x = -0.5 + i + 1/5 *j;
errorbar(x,y,(j,i),e(j,i),'.'); % plotting errors
end
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
box off;
hold off;
19 个评论
Star Strider
2014-7-10
I can't run your code to see what the problem is. Could you attach Mean.mat and Stddeviation.mat?
Star Strider
2014-7-10
This seems to work:
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
load('Stddeviation.mat')
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
e = [sd1,sd2,sd3,sd4,sd5,sd6]'; % define standard deviation
figure;
hBars = bar(y,1.0,'grouped');
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold all;
for i = 1:4
hb = get(get(hBars(i),'Children'), 'XData');
midbar = mean(hb);
errorbar(midbar, y(:,i), e(:,i), '.') % plotting errors
end
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
box off;
hold off;
Pinga
2014-7-11
Thank you a lot! That was really helpful - I appreciate it!
One more problem: The code actually runs only after hitting "Run" a second time. The first time I'm running it, it'll only plot the bars without errorbars. Following error appear then: "Error using handle.handle/get. Invalid or deleted object." Indicated line is
if true
% code
end"
hb = get(get(hBars(i),'Children','Xdata');
I've already cleared workspace and deleted the two .mat-files Mean and Stddeviation and created them a second time in MATLAB. I've checked for the correct path in MATLAB and I also tried to run the code on another device - I'm getting the same error message.
Star Strider
2014-7-11
It runs for me without errors, warnings, or other problems. (I’m running R2014a, so there could be version differences.) The code I used is exactly what I posted (tested first). It might be worthwhile to remove the semicolon on the ‘hBar = bar( ... )’ line to be sure the handle is returned and has the correct dimensions.
I usually caution against using ‘i’ and ‘j’ as loop counters because MATLAB uses them for its imaginary operators. Changing the loop counter to ‘k’ might solve the problem, but since I can’t reproduce it, I can’t help you with it.
Pinga
2014-7-11
Thank you for your help. I'm also running R2014a on both devices I've tried... That makes sence, I only used 'i' and 'j', since I saw it on another site when I was researching how to solve this errorbar-problem. So, I've already changed it to k. It's "just" a flaw and therefore not really a problem, if I won't find the solution for it.
Thank you again for your help - I really appreciate your time and effort!
Star Strider
2014-7-11
My pleasure.
I don’t know what could be causing your problem. The hBars variable should exist, and I can’t see that it’s being overshadowed anywhere, at least in my code snippet. The only other suggestion I have is to put:
clear hBars
before the ‘x = time;’ statement to be sure you don’t have another hBars variable somewhere else.
Pinga
2014-7-11
I made sure that none of the variables have been used anywhere before. If I have solved the problem, I'll comment once again.
Thank you and have a great day!
Star Strider
2014-7-11
编辑:Star Strider
2014-7-11
My pleasure! You too!
I wish I could help, but I can’t reproduce your problem. The code I posted runs for me without errors or warnings, and the plot looks as it should.
The only thing I can think of is to break up the one hb line into two separate statements:
hbc = get(hBars(k1),'Children');
hb = get(hbc, 'XData');
and see if that somehow magically solves the problem. At least it might tell you which one of the previously nested commands is throwing the error.
Pinga
2014-7-12
Unfortunately, it doesn't magically solve the problem. I'll just keep trying. You've been really helpful - thanks!
Star Strider
2014-7-12
My pleasure!
Can you reproduce it on other computers? That could provide some insight. I can’t reproduce it on mine.
Star Strider
2014-7-14
编辑:Star Strider
2014-7-14
Which line — hbc or hb — throws the error?
Since I can’t reproduce the error, I submitted a Service Request to TMW to see if they can reproduce it and suggest a solution.
Pinga
2014-7-14
Wow - you're making a great effort - thank you! I'll get the error message on
if true
% code
end
hbc = get(hBars(k),'Children');
I don't know, if this is of any relevance but MATLAB doesn't show any warnings, thus the box on the upper right corner appears grean (I'm using default settings).
Star Strider
2014-7-14
That’s strange, because each of the hBars handles should work without problems. I sent along the URL to this thread in my Service Request, so they’ll see everything here.
I’m mystified by ‘the box on the upper right corner’. Your installation must be configured differently than mine.
Star Strider
2014-7-18
I received a reply from TMW with respect to your problem. The essence of the reply follows. (Note: ‘Feel free to let him know that he can contact us directly if he is still having issues. We would be happy to help him directly and, in that case, taking a closer look at what is happening with screen sharing would be the best option.’)
--------------------------------------------------
I read the conversation on MATLAB Answers and ran the code that you posted. It also works fine on my computer.
The error message is clear: one of the handles in the "hBars" vector, or one of the children of one of these objects, refers to an object that does not exist anymore. This would typically happen if the figure was closed.
Unfortunately, I cannot help him anymore than you already did without having access to his computer. The first suggestion would be to step through the code and look at the handles in "hBars" and their children after calling "bar" and after each "get" call, making sure that they exist.
I would also suggest to check that the "bar" function is not shadowed by something else with
>> which -all bar
It could be possible that there is a function named "bar" on the path that returns a double (assuming he is using MATLAB R2014a or older) without throwing an error for these input arguments. In that case, the output would not a valid handle.
That fact that the error bars in the PDF he attached are off is very strange. That could indicate that there is something wrong with his data (e.g., he changed the value of "y" midway through the script) or with his files. It could be possible that he did not run the script at once (F5) but he executed lines here and there without clearing the workspace and closing all the figures.
I would also take a look at the renderer used to display the figure and ask if he changed any of the defaults properties of the graphics root object 0.
Feel free to let him know that he can contact us directly if he is still having issues. We would be happy to help him directly and, in that case, taking a closer look at what is happening with screen sharing would be the best option.
Again, I apologize that I could not be more helpful. I am now closing this case as unresolved. Please let me know if you have additional questions
--------------------------------------------------
Pinga
2014-7-21
Thank you a lot for posting the reply. I haven't been able to solve the problem in the last few days. Next step is to try the suggestions mentioned above. How do I get in contact with TMW, if I still having issues?
Star Strider
2014-7-21
My pleasure!
To get in touch with TMW, start at the TMW home page, go to the upper right, click on ‘My Account’ then on the ‘MathWorks Account’ page, on the left under ‘My Account’, click on ‘Create Service Request’ and choose the ‘Technical Support’ option. Follow the instructions. Include the URL to this thread in your Service Request. No need to explain it all again when you’ve already explained it here. You will also need to type ver in the Command Window to get the version information for your MATLAB installation.
TMW should be able to solve your problem.
Pinga
2014-7-10
What I've tried is to switch the columns/rows since the last plot only showed four groups of errorbars. But that didn't solve the problem. I left the values for the standard deviation in this switched order.
That would be great, if anyone could help me - I've been trying for some days now to only plot these data.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)