我想自动创建样品的硬度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');
答案 0 :(得分:5)
我认为你可能过于复杂了?
尝试这样的事情:
% 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);
产生以下图像: