現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to barplot with standard deviation?
87 ビュー (過去 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.
Pinga
2014 年 7 月 14 日
Yes, I've tried it on two other computers with the same error message appearing.
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)