McClean: Making Nice Bar Plots: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
Line 37: Line 37:
</pre>
</pre>
[[Image:Fig3_BarChartEx.png‎]]
[[Image:Fig3_BarChartEx.png‎]]
%Now let's use better colors by changing the color map and set the bar widths, line widths, axis fonts etc to something prettier
<pre>
barwitherr(cat(3,zeros(4,2),[Strain1_std' Strain2_std']), [1 2 3 4],[Strain1_Mean' Strain2_Mean'],'LineWidth',2,'BarWidth',0.9)
legend('Strain 1','Strain 2')
</pre>
set the axis properties
<pre>
ax=gca;
set(ax, 'FontSize',12)
</pre>
[[Image:Fig4_BarChartEx.png‎]]


Don't like the colors? You can change them by modifying the colormap:
Don't like the colors? You can change them by modifying the colormap:
Line 60: Line 47:
</pre>
</pre>


[[Image:Fig5_BarChartEx.png‎]]
[[Image:Fig4_BarChartEx.png‎]]


It isn't very useful to have our experimental conditions labelled 1,2,3,4 so can we change these to words? Yes:
It isn't very useful to have our experimental conditions labelled 1,2,3,4 so can we change these to words? Yes:
Line 68: Line 55:
</pre>
</pre>


[[Image:Fig5_BarChartEx.png‎]]
But this isn't perfect, maybe we want more information on the axis.  To have actual labels rotate them using the handy xticklabel_rotate function:
<pre>
set(ax, 'FontSize',12,'XTick',[1 2 3 4],'XTickLabel',{'Condition A','Condition B','Condition C','Condition D' });
xticklabel_rotate([1 2 3 4],45,{'Condition A','Condition B','Condition C','Condition D' })
pause
</pre>
[[Image:Fig6_BarChartEx.png‎]]
[[Image:Fig6_BarChartEx.png‎]]




 
If you are going to use this figure in a presentation or paper you can save it in various forms (including as a file for adobe illustrator). Recall that h is our figure handle:
 
[[Image:Ex1_TwoErrorBar.png ]]
 
 
''Example 2: Plotting one errorbar plot and a non-errorbar plot on the same x-axis with different y axis''
<pre>
<pre>
M1=randn(100,10);
saveas(h, 'ExampleBar.fig','fig')
x=linspace(1,10,10);
saveas(h, 'ExampleBar.png','png')
y=2*x;
saveas(h, 'ExampleBar.ai','ai')
[a,h1,h2]=plotyy(x,M1,x,y,'EBplotyy','plot')
close all;
set(get(a(1),'YLabel'),'String','Errorbar Plot')
</pre>
set(get(a(2),'YLabel'),'String','Some Other Plot')
title('Errorbar Plot and some other Random Plot')
set(get(a(1),'XLabel'),'String','Points')
</pre>
 
[[Image:ErrorbarwOther.png]]


=Code=
=Code=

Revision as of 15:47, 8 June 2012

Summary

This explains the basics of making pretty bar plots in Matlab. The Matlab "bar" command is used, along with some nice scripts discovered on the Matlab file exchange.

Example

Suppose you have some experimental data from two strains (Strain 1 and Strain 2) under four different experiment conditions (Condition A,B,C,D). The means and standard deviations of your measurement of interest look like:

Strain1_Mean=[0.5137    3.2830    1.5887    5.9188];
Strain2_Mean=[0.4042    2.9884    0.5709    2.7766];
Strain1_std=[1.1393    2.8108    2.2203    3.5233];
Strain2_std=[0.8762    2.8478    0.9878    2.2197];

Use Matlab's bar command to plot this data (without error bars) as a bar chart:

bar([1 2 3 4],[Strain1_Mean' Strain2_Mean'])
legend('Strain 1','Strain 2')
pause; close all;

This looks ok, but we would really like some error bars, so we use a handy function from the file exchange:

h=figure; hold;
barwitherr([Strain1_std' Strain2_std'], [1 2 3 4],[Strain1_Mean' Strain2_Mean'])
legend('Strain 1','Strain 2')
pause; close all;

This is ok, but we'd rather only have one-sided error bars. To do this, you will send barwitherr zeros for the lower error and keep the upper error as is by sending in the matrix cat(3,zeros(4,2),[Strain1_std' Strain2_std']) for the error

barwitherr(cat(3,zeros(4,2),[Strain1_std' Strain2_std']), [1 2 3 4],[Strain1_Mean' Strain2_Mean'])
legend('Strain 1','Strain 2')
pause; close all;

Don't like the colors? You can change them by modifying the colormap:

barmap=[0.7 0.7 0.7; 0.05 .45 0.1]; %[0.7 0.7 0.7] is grey, [ 0.05 .45 0.1] is a green
colormap(barmap);
ylabel('Data','FontSize',14)
title('Title of Experiment','FontSize',14)
pause; 

It isn't very useful to have our experimental conditions labelled 1,2,3,4 so can we change these to words? Yes:

set(ax, 'XTick',[1 2 3 4],'XTickLabel',{'A','B','C','D' });
pause;

But this isn't perfect, maybe we want more information on the axis. To have actual labels rotate them using the handy xticklabel_rotate function:

set(ax, 'FontSize',12,'XTick',[1 2 3 4],'XTickLabel',{'Condition A','Condition B','Condition C','Condition D' });
xticklabel_rotate([1 2 3 4],45,{'Condition A','Condition B','Condition C','Condition D' })
pause


If you are going to use this figure in a presentation or paper you can save it in various forms (including as a file for adobe illustrator). Recall that h is our figure handle:

saveas(h, 'ExampleBar.fig','fig')
saveas(h, 'ExampleBar.png','png')
saveas(h, 'ExampleBar.ai','ai')
close all;
 

Code

function h=EBplotyy(x,y)
%INPUT:
%x-independent variable (often Time)
%y-dependent variable
%OUTPUT:
%h-handle of the errorbar graphics object
%
%Save this code in an m-file named EBplotyy.m

s=nanstd(y);
h=errorbar(x,nanmedian(y),nanstd(y));  %NOTE: You might want to change nanmedian to nanmean, nanstd to standard error, etc depending on what you want to plot

Notes

Please feel free to post comments, questions, or improvements to this protocol. Happy to have your input!

  • Megan N McClean 17:27, 30 January 2012 (EDT): Obviously this is nothing fancy, but the code has come in handy for me a number of times, so I thought I would stick it on the wiki in case it is useful to anyone else in the lab.

References

Mathworks Online Help: plotyy

Matlab Newsreader: Plotyy with errorbar

Contact

or instead, discuss this protocol.