[关闭]
@Senl 2018-01-23T03:16:53.000000Z 字数 5944 阅读 5213

Matlab 画图整理

BlackBoxes 美赛建模



0. 学习资料

Matlab图像官方文档
陈浩的博客

1/2. 可视化图形属性编辑

1. plot 函数的简单运用

1.1 自变量和函数的注意事项

1.2 线条属性以及设置

1.2.1 句柄数组

1.2.2 set设置函数

在 matlab 中线条的属性主要有:

- LineStyle: 线型

- LineWidth: 线宽

- Marker: 标记点的形状

- MarkerSize: 标记点大小

- Color: 颜色

- MarkerFaceColor: 标记点填充颜色

- MarkerEdgeColor: 标记点边缘颜色
RGB Vector Short Name Long Name
[ 1 1 0 ] 'y' 'yellow'
[ 1 0 1 ] 'm' 'magenta'
[ 0 1 1 ] 'c' 'cyan'
[1 0 0 ] 'r' 'red'
[ 0 1 0 ] 'g' 'green'
[0 0 1] 'b' 'blue'
[ 1 1 1 ] 'w' 'white
[0 0 0] 'k' 'black'

2. LineStyle

Specifier Line Style
'-' 实线(默认的线型)
'–' 虚线
':' 点线
'-.' 点横线
'none' 没有线

3. LineWidth
- 设置线宽,一般是直接一个数字就可以

  1. Marker
Specifier Marker Symbol
'+' 加号
'o' 圆圈
'*' 星号
'.' 实心点
'x' 叉号
'square' or 's' 正方形
'diamond' or 'd' 钻石形
'^' 上三角形
'v' 下三角形
'>' 右三角形
'<' 左三角形
'pentagram' or 'p' 五角星形
'hexagram' or 'h' 六角星形
'none' 没有标记

5. MarkerSize
设置标记点的大小,一般也是数字

  1. set(h(1),'LineWidth',2)
  2. set(h(2),'Marker','*')
  3. set(h(1),'Color',[0.6 0 1])
  4. set(h(1),'LineStyle',':')
  5. set(h(2),'MarkerFaceColor',[0 0 1],'MarkerEdgeColor',[0.6 0.6 0.6])
  6. set(h(2),'MarkerSize',2)

2. 标签设置

2.1 图的标签设置

2.2 轴的标签设置

2.2.1 设定轴的范围

  1. 默认时,MATLAB可以根据数值的最大值和最小值决定合适的范围,用 axis 命令可以自己定义数值的标尺范围: axis([xmin xmax ymin ymax])

  2. 三维图则用: axis([xmin xmax ymin ymax zmin zmax])

  3. 用命令 axis auto 使MATLAB重新自动选择范围。

2.2.2 设定纵横比

2.2.3 设定轴的可见性

2.2.4 设定网格线

3.在现有图中增加图

3.1 hlod 命令

例子:
peaks函数概念

  1. % 做一个peaks函数
  2. [x,y,z] = peaks;
  3. % 画出来的图是没有色彩的,黑白的
  4. contour(x,y,z,20,'k')
  5. % 添加上颜色
  6. hold on
  7. pcolor(x,y,z)
  8. shading interp
  9. hold off

3.2 Figure窗口

3.3 同一个Figure中作多幅图

4. matlab中对图像和对象(句柄的理解)

参考:陈浩的Blog
matlab 画图(三): 图形句柄
matlab 画图(四): 句柄作图示例

5. matlab画垂直柱状图

5.1 bar(Y)

5.2 bar(x,Y)

5.3 bar( , width)

设置每个柱子的宽度

5.4 bar( ,style)

style可以设置4个值,Grouped ,Stacked ,histc ,hist

5.5 bar( ,bar_color)

5.6 bar( ,name ,value)

name —— value 的取值可以为 :

BarLayout: grouped,stacked,histc,hist

BaseValue: base line location

LineStyle: line style of bar edges

LineWidth: width of bar edges

BarWidth

FaceColor

EdgeColor

5.7 Overlay line 折线图 + 柱状图

plotyy() 函数

  1. days = 0:5:35;
  2. conc = [515,420,370,250,135,120,60,20]
  3. temp = [29,23,27,25,20,23,23,27]
  4. figure;
  5. % plotyy() 函数
  6. [hAxes,hBar,hLine] = plotyy(days,temp,days,conc,'bar','plot')
  7. % 修改属性
  8. set(hLine,'LineWidth',2,'Color',[0,0.7,0.7],'Marker','o')
  9. title('Trend Chart for Concentration')
  10. xlabel('Day')
  11. ylabel(hAxes(1),'Temperature (^{o}C)')
  12. ylabel(hAxes(2),'Concentration')

折线图加柱状图

  1. x = linspace(0,10);
  2. y1 = 200*exp(-0.05*x).*sin(x);
  3. y2 = 0.8*exp(-0.5*x).*sin(10*x);
  4. y3 = 0.2*exp(-0.5*x).*sin(10*x);
  5. figure
  6. [hAx,hLine1,hLine2] = plotyy(x,y1,[x',x'],[y2',y3']);//用左边的y轴画一组数据,用右边的y轴画两组数据

使用y轴画多一条线

5.8 自定义横坐标

  1. clc,clear
  2. x = [53;74;131;296;543;58;81;168];
  3. % As Cd Cr Cu Hg Ni Pb Zn
  4. y = sort(x);
  5. bar(y);
  6. title('各重金属浓度的变异系数(%)');
  7. %xlabel('x轴');
  8. ylabel('变异系数(%)');
  9. set(gca,'xticklabel',{'As','Ni','Cd','Pb','Cr','Zn','Cu','Hg'});

自定义横纵比实例

5.9 修改柱状图的基线

  1. % 正常的柱状图
  2. Y = [5,4,3,5;3,6,3,1;4,3,5,4];
  3. figure
  4. hBars = bar(Y);
  5. % 修改baseline,会变成上下两部分
  6. set(hBars(1),'BaseValue',2)
  7. % 修改baseline线条的属性
  8. hBaseline = get(hBars(1),'Baseline');
  9. set(hBaseline,'LineStyle',':','Color','red','LineWidth',2);

修改基线后的柱状图

6.横向柱状图

6.1 barh() 函数

  1. % Create the data for the temperatures and months
  2. temperatures = [40.5 48.3 56.2 65.3 73.9 69.9 71.1 59.5 48.7 35.3 31.7 30.0];
  3. months = {'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan'};
  4. % Plot the temperatures on a horizontal bar chart
  5. figure
  6. barh(temperatures)
  7. % Set the axis limits
  8. axis([0 80 0 13])
  9. % Add a title
  10. title('Boston Monthly Average Temperature - 2001')
  11. % Change the Y axis tick labels to use the months
  12. set(gca, 'YTick', 1:12)
  13. set(gca, 'YTickLabel', months)

横向柱状图

7. 扇形图

一个很棒的在线制作饼状图的网站

8. 双坐标画图

9. 倾斜横坐标标签

  1. y = rand(7,1)*20
  2. bar(y);
  3. % 将横坐标(xticklabel)标签设置成你想要显示的字符。
  4. % 如果是纵坐标改yticklabel,二者都用自然也可以
  5. set(gca,'xticklabel',{'hello1','hello2','hello3','hello4','hello5','hello6','hello7'})
  6. xtb = get(gca,'XTickLabel'); % 获取横坐标轴标签句柄
  7. xt = get(gca,'XTick'); % 获取横坐标轴刻度句柄
  8. yt = get(gca,'YTick'); % 获取纵坐标轴刻度句柄
  9. xtextp=xt; %每个标签放置位置的横坐标,这个自然应该和原来的一样了。
  10. % 设置显示标签的位置,写法不唯一,这里其实是在为每个标签找放置位置的纵坐标
  11. ytextp=yt(1)*ones(1,length(xt));
  12. % rotation,正的旋转角度代表逆时针旋转,旋转轴可以由HorizontalAlignment属性来设定,
  13. % 3个属性值:leftrightcenter,这里可以改这三个值,以及rotation后的角度,这里写的是45
  14. % 不同的角度对应不同的旋转位置了,依自己的需求而定了。
  15. % ytextp - 0.5是让标签稍微下一移一点,显得不那么紧凑
  16. text(xtextp,ytextp-0.5,xtb,'HorizontalAlignment','right','rotation',45,'fontsize',10);
  17. set(gca,'xticklabel','');% 将原有的标签隐去

横坐标标签倾斜


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注