Matlab: Changing line specifications

折月煮酒 提交于 2019-12-10 11:03:30

问题


I would like to automatically create graphs of Hardness H and Young's modulus E of samples as function of load L of indenter.

My goal is to get opaque markers connected with dashed lines. When using set(handle,'linestyle',spec) or line(...,'linestyle',spec) command I got markers or lines, never both of them - MATLAB throws error.
Is there way to get lines and markers without plotting two lines with same data and different specs? I'd like to continue with this to work with legend as described in another question (MATLAB: legend for plotyy with multiple data sets).

Here is my actual MWE code:

%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');

回答1:


I think you may be overcomplicating it?

Try something like this:

% 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]);

It produces the following image of opaque markers connected with dashed lines:

To support plotyy, the process is basically the same, except you have to set some properties on both the parent and child axes. Here's some example code:

% 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);

Which produces the following image:



来源:https://stackoverflow.com/questions/9329087/matlab-changing-line-specifications

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!