- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在相关图中绘制一组配对数据。这是一项通过两种田间处理和 8 个输入水平来判断植物 react 的研究。我想显示数据,用 8 种不同的颜色表示 8 个输入级别,用 3 个不同的形状表示 3 个不同年份的研究。我正在使用 gscatter。
问题是,当我指定两个变量作为分组依据时,它不知道颜色与一个变量相关而形状与另一个变量相关,而且我只想在输入变化时改变颜色,并且只希望形状与当年份改变时改变。最终结果是,对于每个独特的分组(其中 24 个),它通过每个独特的组契约(Contract)时通过颜色和形状前进。
这是说明输出的两个图表。第一张图显示了所有刚刚按输入分组和着色的数据。它正确地为每个输入级别分配了唯一的颜色。 3 年每年 2 分,每种颜色共计 6 分。
现在我只想改变第 2 年和第 3 年的形状并保持颜色相同,但就是我得到的。您可以在图例中看到它如何同时循环 3 次颜色和 8 次形状。所以不同的颜色被分配到相同的输入级别。我尝试以不同的方式对数据进行排序,但得到的结果完全相同。
我还尝试手动调整每个数据点的颜色,但对象中的顺序必须与图例中列出的顺序不同,因为我得到了奇怪的结果。有些点已正确更改,但其他点未正确更改。
一定有更好的方法来做到这一点。我愿意接受所有建议,无论是让这个方法起作用,还是使用不同的函数。
这是包含较小数据子集的代码:
clear
Treatments = table([{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'}]);
Data = [2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018 2016 2016 2017 2017 2018 2018;...
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2;...
4704.5 4059.5 10891 11440.5 4083.5 2876 11459.66667 11752 11566 12036 11323.5 11118.5 10296.5 10234 13074.5 14166 9062 9669]';
% split by treatment
t1Response = Data(strcmp(Treatments.Var1,'T1'),3);
t2Response = Data(strcmp(Treatments.Var1,'T2'),3);
Inputs = Data(strcmp(Treatments.Var1,'T2'),2); % treatment doesn't matter, just need one set
Years = Data(strcmp(Treatments.Var1,'T2'),1); % treatment doesn't matter
% all points one shape, group colors just by inputs
figure;
colors = lines(8);
colors(8,1)=0.5;
g = gscatter(t1Response,t2Response,Inputs,colors([7,2,3],:),'.',20,'on');
% group by input and year
figure;
g2 = gscatter(t1Response,t2Response,{Inputs,Years},colors([7,2,3],:),'.s^',20,'on'); g2(1).MarkerFaceColor = colors(7,:);
% g2(1).MarkerFaceColor = colors(7,:);
% g2(2).MarkerFaceColor = colors(7,:);
% g2(3).MarkerFaceColor = colors(7,:);
% g2(4).MarkerFaceColor = colors(2,:);
% g2(5).MarkerFaceColor = colors(2,:);
% g2(6).MarkerFaceColor = colors(2,:);
% g2(7).MarkerFaceColor = colors(3,:);
% g2(8).MarkerFaceColor = colors(3,:);
% g2(9).MarkerFaceColor = colors(3,:);
最佳答案
您可能不得不放弃 gscatter
并自己完成此操作。来自documentation多个分组变量上的项目:
Alternatively, g can be a cell array containing several grouping variables (such as {g1 g2 g3}), in which case observations are in the same group if they have common values of all grouping variables.
即它无法处理具有独立颜色和形状的独立变量。
您可以使用 fingroups
和一些索引来构建组并循环多年以进行绘图。这是一个强大的解决方案,可以处理诸如拥有比颜色/标记更多的组的情况:
% Define colours and markers
colors = lines(8); colors(8,1)=0.5;
markers = {'x','d','o','+','*','s','p''h'};
% Create colour matrix
[gInputs, uInputs] = findgroups(Inputs);
if max(gInputs) > size(colors,1)
warning( 'More inputs than possible colors, colors will be re-used' );
end
colors = colors(mod(gInputs-1,size(colors,1))+1,:); % mod to handle out of range case
% Create marker array
[gYears, uYears] = findgroups(Years);
if max(gYears) > numel(markers)
warning( 'More years than possible markers, markers will be re-used' );
end
markers = markers(mod(gYears-1,numel(markers))+1); % mod to handle out of range case
figure(); hold on
for iYr = 1:max(gYears)
idx = (iYr == gYears);
scatter(t1Response(idx), t2Response(idx), 20, colors(idx,:), 'Marker', markers{iYr}, 'displayname', num2str(uYears(iYr)), 'LineWidth', 2 );
end
hold off
legend('show')
如果您希望图例反射(reflect)年份和输入的组合,那么您将不得不使用双循环
% Setup as above...
figure(); hold on
for iYr = 1:max(gYears)
for iIn = 1:max(gInputs)
idx = (iYr == gYears) & (iIn == gInputs);
scatter(t1Response(idx), t2Response(idx), 20, colors(idx,:), 'Marker', markers{iYr}, 'displayname', sprintf('%d: %d',uYears(iYr),uInputs(iIn)), 'LineWidth', 2 );
end
end
hold off
legend('show')
您可以使用 line
而不是 scatter
使最后一个示例更快,但语法略有不同,因此我保留它以避免复杂化。
要获得更简洁的图例,您必须使用您想要的格式伪造一些行。我对代码进行了一些重组,以展示如何做到这一点:
% Define colours and markers
colors = lines(8); colors(8,1)=0.5;
markers = {'x','d','o','+','*','s','p''h'};
% Create colour matrix
[gInputs, uInputs] = findgroups(Inputs);
if max(gInputs) > size(colors,1)
warning( 'More inputs than possible colors, colors will be re-used' );
end
% Create marker array
[gYears, uYears] = findgroups(Years);
if max(gYears) > numel(markers)
warning( 'More years than possible markers, markers will be re-used' );
end
% handle index out of range
markers = markers(mod((1:max(gYears))-1,numel(markers))+1);
colors = colors(mod((1:max(gInputs))-1,size(colors,1))+1,:);
% Setup as above...
lineProps = {'markersize', 5, 'linestyle', 'none', 'LineWidth', 2};
figure(); hold on
for iYr = 1:max(gYears)
for iIn = 1:max(gInputs)
idx = (iYr == gYears) & (iIn == gInputs);
line(t1Response(idx), t2Response(idx), 20, 'color', colors(iIn,:), 'Marker', markers{iYr}, 'handlevisibility', 'off', lineProps{:} );
end
end
% Spoof markers for the legend
for iYr = 1:max(gYears)
line( NaN, NaN, 'color', 'k', 'Marker', markers{iYr}, 'displayname', num2str(uYears(iYr)), lineProps{:} );
end
for iIn = 1:max(gInputs)
line( NaN, NaN, 'color', colors(iIn,:), 'Marker', 'o', 'markersize', 20, 'displayname', num2str(uInputs(iIn)), lineProps{:} );
end
hold off
legend('show')
关于matlab - 按 2 个变量分组,一个变量具有独特的颜色,另一个变量具有独特的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67011177/
在 Matlab 中,您可以选择创建新的“示例”脚本文件以及脚本、函数、类等。创建它们时,它们会获得一个脚本图标。 它们与其他标准脚本文件的处理方式有何不同? 是否有关于这些示例脚本类型的预期用途的文
我正在运行一个不是我自己编写的大 m 文件,它依赖于某些子函数。我想知道是否在所有嵌套函数的任何地方都使用了特定函数(在我的例子中是函数 eig.m(计算特征值))。有没有快速的方法来做到这一点? 亲
Matlab中有一个函数叫 copulafit .我怎样才能看到这个函数背后的代码?许多 Python 的 numpy 和 scipy 函数在 Github 上很容易开源,但由于某种原因我在 Gith
我定义了一个抽象基类measurementHandler < handle它定义了所有继承类的接口(interface)。这个类的两个子类是a < measurementHandler和 b < me
假设有一个矩阵 A = 1 3 2 4 4 2 5 8 6 1 4 9 例如,我有一个 Vector 包含该矩阵每一列的“类”
我有一个在后台运行的 Matlab 脚本。随着计算的进行,它会不断弹出进度栏窗口。这很烦人。 问题是我没有自己写 Matlab 脚本,这是一段很长很复杂的代码,我不想搞砸。那么如何在不修改 Matla
有没有办法从一个 matlab 程序中检测计算机上正在运行多少个 matlab 进程? 我想要恰好有 n 个 matlab 进程在运行。如果我的数量太少,我想创建它们,如果数量太多,我想杀死一些。您当
我正在测试我们在 Matlab 中开发的一个独立应用程序,当时我注意到它的内存使用量(根据 Windows 任务管理器)达到了 16gb 以上的数倍峰值。我决定在编译版本后面的脚本上使用 profil
我面临着一个相当棘手的问题。在 Matlab 中,命令 S = char(1044) 将俄语字母 д 放入变量 S。但是 disp(S) 返回空白符号,尽管内容实际上是正确的: >> S = char
我在这行 MATLAB 代码中遇到内存不足错误: result = (A(1:xmax,1:ymax,1:zmax) .* B(2:xmax+1,2:ymax+1,2:zmax+1) +
我正在寻找一种在 MATLAB 中比较有限顺序数据与非确定性顺序的方法。基本上,我想要的是一个数组,但不对包含的元素强加顺序。如果我有对象 a = [x y z]; 和 b = [x z y]; 我希
我有一个由 1 和 0 组成的二维矩阵。 mat = [0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1]; 我需
我可以在 Matlab 中用一组 x,y 点绘制回归线。但是,如果我有一组点(如下图),假设我有四组点,我想为它们绘制四条回归线……我该怎么做?所有的点都保存在 x,y 中。没有办法将它们分开并将它们
我正在尝试使用以下代码在 MATLAB 中绘制圆锥体。但是,当 MATLAB 生成绘图时,曲面中有一个间隙,如下图所示。谁能建议关闭它的方法? clearvars; close all; clc; [
我有一个 map称为 res_Map,包含一组不同大小的数组。我想找到用于存储 res_Map 的总内存。 正如您在下面看到的,看起来 res_Map 几乎不占用内存,而 res_Map 中的各个元素
有没有办法在 MATLAB 中组合 2 个向量,这样: mat = zeros(length(C),length(S)); for j=1:length(C) mat(j,:)=C(j)*S;
已结束。此问题不符合 Stack Overflow guidelines 。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答它。 关闭 5 年前
我正在尝试将MatLab中的t copula适配到我的数据,并且我的功能是: u = ksdensity(range_1, range_1,'function','cdf'); v = ksdens
大家好,我目前正在尝试使用论文“多尺度形态学图像简化”中的 SMMT 运算符 Dorini .由于没有订阅无法访问该页面,因此我将相关详细信息发布在这里: 请注意,我将相关文章的部分内容作为图片发布。
我在MATLAB中编写代码,需要使用一个名为modwt的函数,该函数同时存在于两个我同时使用的工具箱(Wavelet和WMTSA)中。问题在于,一个版本仅返回一个输出,而另一个版本则返回三个输出。我应
我是一名优秀的程序员,十分优秀!