Matlab中legend位置

转自:http://blog.sina.com.cn/s/blog_7db803c10102weyk.html

Matlab中legend默认的位置在NorthEast,如图所示:

%Matlab中legend的位置设置

clc
clear
close all
Npoint = 100;
x = linspace(0,4*pi,Npoint);
y1 = sin(x);
y2 = cos(x);
H = plot(x,y1,x,y2);
legend('sin(x)','cos(x)');

然而,我们却可以通过Location对legend的位置进行改变,变为North,如图所示

%Matlab中legend的位置设置

clc
clear
close all
Npoint = 100;
x = linspace(0,4*pi,Npoint);
y1 = sin(x);
y2 = cos(x);
H = plot(x,y1,x,y2);
legend('sin(x)','cos(x)','Location','North');

Matlab位置选择

设置 位置
‘North‘ inside plot box near top
‘South‘ inside bottom
‘East‘ inside right
‘West‘ inside left
‘NorthEast‘ inside top right (default for 2-D plots)
‘NorthWest‘ inside top left
‘SouthEast‘ inside bottom right
‘SouthWest‘ inside bottom left
‘NorthOutside‘ outside plot box near top
‘SouthOutside‘ outside bottom
‘EastOutside‘ outside right
‘WestOutside‘ outside left
‘NorthEastOutside‘ outside top right (default for 3-D plots)
‘NorthWestOutside‘ outside top left
‘SouthEastOutside‘ outside bottom right
‘SouthWestOutside‘ outside bottom left
‘Best‘ least conflict with data in plot
‘BestOutside‘ least unused space outside plot

Matlab中还可以选择某条曲线legend的指定显示

%Matlab中legend的选择

clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
legend('First','Second','Third','Fourth','Fifth','Location','NorthEastOutside')

如果只想显示第1、3、5条,也很简单

%Matlab中legend的选择

clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','NorthEastOutside')

还可以使用Orientation对legend进行横向排列

%Matlab中legend的横排,注意,Location位置改变为North

clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon')

不显示方框:

clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon','Box','off')

原文地址:https://www.cnblogs.com/xym4869/p/12243271.html

时间: 2024-08-03 14:23:07

Matlab中legend位置的相关文章

matlab中legend的任意标注

通过对句柄的组装,利用索引可以实现只标注自己想要的点或线. legend在画图中经常用到,但是如果直接legend('图形1‘,‘图形2','图形3');最终所得的标注是按照画图的先后顺序,也就是说假定我plot了3个点,那么legend就会一次标注这些点 但是,经常有这样的需求,画n个点,再把这些点连成折线,再画n个点,再连成线,最后比较这两条线的差异,这样我标注的就只想是点或者是线,但是按照顺序就会点标完了把连的线也标注了. 解决方法如下: x=[1,2,3]; y=[0.714,0.755

【Matlab】Matlab中的plot函数及legend函数解析 持续更新...

Matlab中plot函数全功能解析 功能 二维曲线绘图 语法 plot(Y) plot(X1,Y1,...) plot(X1,Y1,LineSpec,...) plot(...,'PropertyName',PropertyValue,...) plot(axes_handle,...) h = plot(...) hlines = plot('v6',...) 描述 plot(Y)如果Y是m×n的数组,以1:m为X横坐标,Y中的每一列元素为Y坐标,绘制n条曲线:如果Y是n×1或者1×n的向量

Matlab中给figure添加图例(legend),标题(title)和颜色(color)

在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(figure)中设置曲线颜色.添加图例(legend)和标题(title)的方法. 在Matlab中,给曲线设定颜色可以采用plot函数实现.如下所示的语句中: plot(x, y, 'r'); 是以 x 变量为横坐标,y 变量为纵坐标绘制红色曲线.其中,颜色控制由 ‘r’实现.在Matlab中,预先留

Matlab设置Legend横排、分块

高级用法1:指定legend显示的位置: legend({'str1','str2','strn'},'Location','SouthEast'); 比较鸡肋,画好图后树手动拖动就好了 高级用法2:指定显示某几条曲线的legend: 例如你有25条曲线,想显示其中1,6,11,16,21的legend H = plot(data); legend(H([1 6 11 16 21],'1,'6','11’,'16','21'); 高级用法3:legend横排 hl = legend(H([1 6

matlab中hold指令、figure指令及subplot指令的使用

一.hold指令使用 正常情况下,plot指令显示figure时,以前的数据丢失了.使用hold on指令后,此后添加的一系列plot曲线将叠加在前一个图上当使用hold off后,恢复为默认状况,plot后将取代旧的figure 代码: % 提示 disp ('该功能练习hold功能'); %初始化快捷式数组 x=-2*pi:pi/20:2*pi; y1=sin(x); y2=cos(x); plot(x,y1,'b-'); title('sin(x)和cos(x)图形'); %该语句必须在p

Matlab中配置VLFeat

在VLFeat官网上是这么介绍VLFeat的:VLFeat开源库实现了很多著名的机器视觉算法,如HOG, SIFT, MSER, k-means, hierarchical k-means, agglomerative information bottleneck, SLIC superpixels, 和 quick shift.VLFeat开源库是用C语言写的,以确保其效率和兼容性,同时VLFeat还提供了MATLAB接口和详细的文档.它可以在windows, Mac, 和Linux上使用.

matlab中的xcorr 自相关函数

转载自 http://blog.163.com/to_be_myself/blog/static/176060227201101762159227/ Matlab中用于计算自相关函数的指令是xcorr.比如矩阵A=[1 2 3]; xcorr(A)=3.0000 8.0000 14.0000 8.0000 3.0000 自相关函数是信号间隔的函数,间隔有正负间隔,所以n个长度的信号,有2n-1个自相关函数值,分别描述的是不同信号间隔的相似程度. 比如,上面的矩阵,最后得到5个结果,其中第三个是自

MATLAB中文件的读写和数据的导入导出

http://blog.163.com/tawney_daylily/blog/static/13614643620111117853933/ 在编写一个程序时,经常需要从外部读入数据,或者将程序运行的结果保存为文件.MATLAB使用多种格式打开和保存数据.本章将要介绍 MATLAB中文件的读写和数据的导入导出. 13.1 数据基本操作 本节介绍基本的数据操作,包括工作区的保存.导入和文件打开.13.1.1 文件的存储 MATLAB支持工作区的保存.用户可以将工作区或工作区中的变量以文件的形式保

Matlab中使用LaTeX

Matlab作为数据计算和处理的数学语言(软件),而LaTex作为出版界的重要排版语言(软件),尤其是对数学公式的排版功能特别强.在Matlab中有两种方法使用LaTeX:1)对Matlab生成的图形标注时,2)Matlab的计算结果转化成LaTeX格式. 1)  对Matlab生成的图形标注 Matlab图形中title.xlabel.ylabel.zlabel.textbox和legend等的Interpreter属性有三个属性:latex .tex.none.默认为tex.(注:LaTeX