Download code

Jump to: navigation, search

Back to Plots_in_Matlab

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

example_of_plot_01.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Plots_in_Matlab?oldid=10997
14 
15 
16 f=@(x)((x+1)./(x.*sin(x)+1))
17 
18 
19 x_bounds     = [-3, 3];
20 x_resolution = .1;
21 x_domain     = (x_bounds(1):x_resolution:x_bounds(2))'
22 
23 y = f(x_domain);
24 
25 figure;
26 plot(x_domain,y)
27 


hijacker
hijacker
hijacker
hijacker

example_of_plot_with_legend_01.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Plots_in_Matlab?oldid=10997
14 
15 
16 data1 = struct('date', (1:100)', 'value', 100*exp(cumsum(randn(100,2))/10), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 1')
17 data2 = struct('date', (1:100)', 'value', 100*cumsum(rand(100,2)*2-1+.1), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 2')
18 %% plot
19 % notice the handle I get back from the plots
20 figure;
21 h1 = plot(data1.date, data1.value, 'linewidth',2)
22 hold on
23 h2 = plot(data2.date, data2.value, ':', 'linewidth',2)
24 hold off
25 %% Legend
26 legend(gca,[h1;h2], cellfun(@(x,y)([x ' ' y]), ...
27         {data1.names{:}, data2.names{:}}', ...
28         cellstr( [repmat(data1.title, length(data1.names),1); ...
29                   repmat(data2.title,length(data2.names),1)]), ...
30         'uniformoutput',false), 'Location',  'NorthWest' )


hijacker
hijacker
hijacker
hijacker