【问题标题】:Matlab: Changing line specificationsMatlab:更改线路规格
【发布时间】:2012-02-17 13:27:38
【问题描述】:

我想自动创建样品的硬度 H 和杨氏模量 E 作为压头负载 L 的函数的图表。

我的目标是用虚线连接不透明的标记。使用 set(handle,'linestyle',spec)line(...,'linestyle',spec) 命令时,我得到了标记或线条,但它们都没有 - MATLAB 抛出错误。
有没有办法在不绘制具有相同数据和不同规格的两条线的情况下获得线条和标记?如另一个问题 (MATLAB: legend for plotyy with multiple data sets) 中所述,我想继续处理图例。

这是我的实际 MWE 代码:

%data1 - m x 3 matrix with data for first sample:
[m,n]=size(data1);

%plots 1st sample data:
[ax,h1,h2]=plotyy([data1(1:m,1)],[data1(1:m,2)],[data1(1:m,1)],[data1(1:m,3)]);

set(h1,'linestyle','o')
set(h2,'linestyle','o')

%store colors:
c1=get(h1,'color');c2=get(h2,'color');

%plots 2nd sample hardness:
line('parent',ax(1),'xdata',[data2(1:m,1)],'ydata',[data2(1:m,2)],...
     'color',c1,'linestyle','s');

%plots 2nd sample young's modulus
line('parent',ax(2),'xdata',[data2(1:m,1)],'ydata',[data2(1:m,3)],...
     'color',c2,'linestyle','s');

【问题讨论】:

    标签: matlab plot line


    【解决方案1】:

    我认为你可能过于复杂了?

    试试这样的:

    % MarkerSize determines the size of the markers
    % MarkerEdgeColor determines the color of the markers themselves
    % Color determines the line color connecting them
    data = rand(1,5);
    plot(data, '.--', 'MarkerSize', 50, 'MarkerEdgeColor', [0.1 0.8 0.2], 'Color', [0.9 0.2 .4]);
    

    它会生成以下用虚线连接的不透明标记图像:

    要支持plotyy,过程基本相同,只是需要在父轴和子轴上都设置一些属性。下面是一些示例代码:

    % Generate some data
    datax1 = rand(1,5);
    datay1 = rand(1,5);
    datax2 = rand(1,5);
    datay2 = rand(1,5);
    
    % Plot the data    
    [ax, h1, h2] = plotyy(datax1, datay1, datax2, datay2);
    
    % Different line styles for each child plot
    set(h1, 'LineStyle', '--');
    set(h2, 'LineStyle', '-.');
    
    % Different markers for each child plot
    set(h1, 'Marker', '.');
    set(h2, 'Marker', '+');
    
    % Different marker sizes for each child plot
    set(h1, 'MarkerSize', 50);
    set(h2, 'MarkerSize', 5);
    
    % Generate two colors. We keep a copy so we can set the axes to match.
    color1 = rand(1,3);
    color2 = rand(1,3);
    
    % The face colors are darker versions of the colors.
    set(h1, 'MarkerEdgeColor', color1 * 0.5);
    set(h2, 'MarkerEdgeColor', color2 * 0.5);
    
    % This is the plot line color.
    set(h1, 'Color', color1);
    set(h2, 'Color', color2);
    
    % Set the axis colors to match the plot colors.
    set(ax(1), 'YColor', color1);
    set(ax(2), 'YColor', color2);
    

    生成以下图像:

    【讨论】:

    • 绘图是否支持'parent' 选项?因为我需要一个与左轴连接的图,第二个与右轴连接的图。 plotyy(...), hold on, plotyy(...) 在第二轴上造成混乱:(
    • 好吧,set(h2, 'Marker', '+'); 做了我想要的一切。非常感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2017-01-03
    相关资源
    最近更新 更多