Matlab 面积图:调整框样式和刻度线
Matlab Area Plot: Adjusting box style and tick marks
我正在尝试通过使用 'area' 函数使用 plotyy 创建具有 2 y-axis 的面积图。我不想有任何刻度线、标签或标题,但我只想要外框。我还想将绘图(而不是整个图形 window)保存为 png 文件。
当我关闭 x 和 y-axis 刻度和标签时,盒子的底部和左侧看起来很薄,顶部和右侧看起来很厚。我做错了什么?
此外,如果我尝试使方框 'LineWidth' 变粗,我会在左下角和右下角看到两个刻度线 - 这会破坏情节并且我无法删除。有人可以帮忙吗?
我的测试代码如下:
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
fig_handle = figure('units','inches','position',[1 1 9 3]);
[a,p1,p2] = plotyy(x,y1,x,y2,'area');
c1 = get(p1,'child');
c2 = get(p2,'child');
set(c1,'facea',0.5,'FaceColor','b','EdgeColor',[0 0 0]);
set(c2,'facea',0.5,'FaceColor','r','EdgeColor',[0 0 0]);
set(c1,'Line','None');
set(c2,'Line','None');
set(a,'Layer','top')
set(a,'XTick',[]);
set(a(1),'YTick',[]);
set(a(2),'YTick',[]);
set(a,'TickDir','in')
set(a,'LineWidth',5);
此外,请注意左侧 Y-axis 上有红色区域 ,而右侧 Y-axis 上没有。这是fix-able吗?
如有任何帮助,我们将不胜感激!另外,我是 Whosebug 的新手 - 所以,如果这些问题太多 post,请原谅我,我会 post 将它们分开 requests/questions。
这是左侧 Y 轴上出现红色的解决方法。
由于轴线很粗,靠近它显示的数据被绘制在它上面。为避免这种情况,您可以稍微移动轴的 x 限制以为数据腾出更多空间。通过更改任一轴的 XLim
属性 来实现此目的,因为它们共享相同的 x 限制:
XL = get(a,'Xlim');
xl = XL{1}; %// here XL{1} and XL{2} are the same...[1 100]
set(a(:),'Xlim',[xl(1)-.5 xl(2)+.5])
至于图底部令人讨厌的刻度线,我必须说我不知道如何在保持坐标轴可见的同时删除它们。
作为 plotyy
的替代解决方案,这里有一种方法可以在没有 plotyy
的情况下获得良好的结果(我认为)。诀窍是叠加 2 个轴并使它们都不可见,然后将图形的颜色设置为白色并在绘图周围添加一个黑色矩形。
代码如下:
clear
clc
close all
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
H1 = area(x,y1);
%// Set the figure color to white
set(gcf,'Color','w')
%// Plot data and set different properties for each axes
A1 = gca;
A2 = axes('Position',get(A1,'Position'));
H2 = area(x,y2);
set(A2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(A2,'XLim',get(A1,'XLim'),'XTick',[],'YTick',[]);
set(A1,'XTick',[],'YTick',[]);
%// Make both axes not visible.
set(A2,'Visible','off')
set(A1,'Visible','off')
axes(A1)
%// Get axes limits
XL = get(gca,'XLim');
YL= get(gca,'YLim');
%// Create rectangle as a bounding box
rectangle('Position',[XL(1) YL(1) XL(2)-1 YL(2)],'LineWidth',5)
%//===
%// Change the data color/properties
hP = findobj('Type','patch');
set(hP(1),'FaceColor','b','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
set(hP(2),'FaceColor','r','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
并且输出:
它并不完美,但希望对您有所帮助!
我正在尝试通过使用 'area' 函数使用 plotyy 创建具有 2 y-axis 的面积图。我不想有任何刻度线、标签或标题,但我只想要外框。我还想将绘图(而不是整个图形 window)保存为 png 文件。
当我关闭 x 和 y-axis 刻度和标签时,盒子的底部和左侧看起来很薄,顶部和右侧看起来很厚。我做错了什么?
此外,如果我尝试使方框 'LineWidth' 变粗,我会在左下角和右下角看到两个刻度线 - 这会破坏情节并且我无法删除。有人可以帮忙吗?
我的测试代码如下:
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
fig_handle = figure('units','inches','position',[1 1 9 3]);
[a,p1,p2] = plotyy(x,y1,x,y2,'area');
c1 = get(p1,'child');
c2 = get(p2,'child');
set(c1,'facea',0.5,'FaceColor','b','EdgeColor',[0 0 0]);
set(c2,'facea',0.5,'FaceColor','r','EdgeColor',[0 0 0]);
set(c1,'Line','None');
set(c2,'Line','None');
set(a,'Layer','top')
set(a,'XTick',[]);
set(a(1),'YTick',[]);
set(a(2),'YTick',[]);
set(a,'TickDir','in')
set(a,'LineWidth',5);
此外,请注意左侧 Y-axis 上有红色区域 ,而右侧 Y-axis 上没有。这是fix-able吗?
如有任何帮助,我们将不胜感激!另外,我是 Whosebug 的新手 - 所以,如果这些问题太多 post,请原谅我,我会 post 将它们分开 requests/questions。
这是左侧 Y 轴上出现红色的解决方法。
由于轴线很粗,靠近它显示的数据被绘制在它上面。为避免这种情况,您可以稍微移动轴的 x 限制以为数据腾出更多空间。通过更改任一轴的 XLim
属性 来实现此目的,因为它们共享相同的 x 限制:
XL = get(a,'Xlim');
xl = XL{1}; %// here XL{1} and XL{2} are the same...[1 100]
set(a(:),'Xlim',[xl(1)-.5 xl(2)+.5])
至于图底部令人讨厌的刻度线,我必须说我不知道如何在保持坐标轴可见的同时删除它们。
作为 plotyy
的替代解决方案,这里有一种方法可以在没有 plotyy
的情况下获得良好的结果(我认为)。诀窍是叠加 2 个轴并使它们都不可见,然后将图形的颜色设置为白色并在绘图周围添加一个黑色矩形。
代码如下:
clear
clc
close all
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
H1 = area(x,y1);
%// Set the figure color to white
set(gcf,'Color','w')
%// Plot data and set different properties for each axes
A1 = gca;
A2 = axes('Position',get(A1,'Position'));
H2 = area(x,y2);
set(A2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(A2,'XLim',get(A1,'XLim'),'XTick',[],'YTick',[]);
set(A1,'XTick',[],'YTick',[]);
%// Make both axes not visible.
set(A2,'Visible','off')
set(A1,'Visible','off')
axes(A1)
%// Get axes limits
XL = get(gca,'XLim');
YL= get(gca,'YLim');
%// Create rectangle as a bounding box
rectangle('Position',[XL(1) YL(1) XL(2)-1 YL(2)],'LineWidth',5)
%//===
%// Change the data color/properties
hP = findobj('Type','patch');
set(hP(1),'FaceColor','b','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
set(hP(2),'FaceColor','r','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
并且输出:
它并不完美,但希望对您有所帮助!