MATLAB:尝试在 3x2 子图中添加共享 xlabel、ylabel
MATLAB: Trying to add shared xlabel,ylabel in 3x2 subplot
正如标题所说,我很乐意做的是相当直截了当的事情。我有一个 m=3,n=2 子图的网格。它们代表测量相同参数的 6 个不同实验的图表。我想在六个子图的边界上有一个 x 标签和一个 y 标签。不幸的是,到目前为止我还没有找到一种简单的方法来做到这一点。 (xlabel 只是将 xlabel 放在最后一个活动子图下)。任何人都知道如何做到这一点?
哦,我如何在带有度数符号的标签中显示摄氏度?(小圆圈...)
您可以使用 mtit 围绕子图创建一个不可见的轴。
mtit
returns 该轴的句柄,然后您可以为其创建 xlabel 和 ylabel。
示例:
% create sample data
my_data = arrayfun(@(x)rand(10, 2) + repmat([x, 0], 10, 1), 1:6, 'UniformOutput', 0);
figure;
clf
ah = gobjects(6, 1); % use zeros if using an old version of MATLAB
% plot data
for ii = 1:6
ah(ii) = subplot(3, 2, ii);
plot(1:10, my_data{ii}(:, 1));
hold on
plot(1:10, my_data{ii}(:, 2));
end
% link axes to have same ranges
max_data = max(cellfun(@(x) max(x(:)), my_data));
min_data = min(cellfun(@(x) min(x(:)), my_data));
linkaxes(ah, 'xy')
ylim([min_data, max_data])
% Create invisible large axes with title (title could be empty)
hh = mtit('Cool experiment');
%set(gcf, 'currentAxes', hh.ah)
% make ylabels
ylh = ylabel(hh.ah, 'Temperature [°C]');
set(ylh, 'Visible', 'On')
xlh = xlabel(hh.ah, 'x label');
set(xlh, 'Visible', 'On')
这将生成如下图:
我不知道你在为每个子图设置 xlabel
和 ylabel
时遇到了什么错误。
另外我不确定我是否理解问题。
以下代码生成 3x2 子图,每个子图都带有 xlabel
和 ylabel
。
在第一种情况下,每个子图对于 xlabel
和 ylabel
都有不同的字符串。
在第二个中,为所有子图设置了相同的 xlabel
和 ylabel
。
要在标签中添加“°”符号,这样定义一个char
变量就足够了:
c='°'
然后使用 sprintf
为 xlabel
和 ylabel
生成字符串。
a=randi(100,6,20)
figure
% Each subplot with its own xlabel and ylabel
for i=1:6
hs(i)=subplot(3,2,i);
plot(a(i,:))
c='°'
str=sprintf('Temp [C%c]',c)
xlabel([str ' ' num2str(i)])
ylabel(['Subplot ' num2str(i)])
grid on
end
figure
% The same xlabel and ylabel for all the subplot
c='°';
x_label_str=sprintf('Temp [C%c]',c)
y_label_str='Same for all'
for i=1:6
hs(i)=subplot(3,2,i);
plot(a(i,:))
xlabel(x_label_str)
ylabel(y_label_str)
grid on
end
图 1:每个子图的不同 xlabel、ylabel
图2:每个子图的相同xlabel、ylabel
希望对您有所帮助。
我认为您可以使用以下来源的超级标签:
http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel
正如标题所说,我很乐意做的是相当直截了当的事情。我有一个 m=3,n=2 子图的网格。它们代表测量相同参数的 6 个不同实验的图表。我想在六个子图的边界上有一个 x 标签和一个 y 标签。不幸的是,到目前为止我还没有找到一种简单的方法来做到这一点。 (xlabel 只是将 xlabel 放在最后一个活动子图下)。任何人都知道如何做到这一点?
哦,我如何在带有度数符号的标签中显示摄氏度?(小圆圈...)
您可以使用 mtit 围绕子图创建一个不可见的轴。
mtit
returns 该轴的句柄,然后您可以为其创建 xlabel 和 ylabel。
示例:
% create sample data
my_data = arrayfun(@(x)rand(10, 2) + repmat([x, 0], 10, 1), 1:6, 'UniformOutput', 0);
figure;
clf
ah = gobjects(6, 1); % use zeros if using an old version of MATLAB
% plot data
for ii = 1:6
ah(ii) = subplot(3, 2, ii);
plot(1:10, my_data{ii}(:, 1));
hold on
plot(1:10, my_data{ii}(:, 2));
end
% link axes to have same ranges
max_data = max(cellfun(@(x) max(x(:)), my_data));
min_data = min(cellfun(@(x) min(x(:)), my_data));
linkaxes(ah, 'xy')
ylim([min_data, max_data])
% Create invisible large axes with title (title could be empty)
hh = mtit('Cool experiment');
%set(gcf, 'currentAxes', hh.ah)
% make ylabels
ylh = ylabel(hh.ah, 'Temperature [°C]');
set(ylh, 'Visible', 'On')
xlh = xlabel(hh.ah, 'x label');
set(xlh, 'Visible', 'On')
这将生成如下图:
我不知道你在为每个子图设置 xlabel
和 ylabel
时遇到了什么错误。
另外我不确定我是否理解问题。
以下代码生成 3x2 子图,每个子图都带有 xlabel
和 ylabel
。
在第一种情况下,每个子图对于 xlabel
和 ylabel
都有不同的字符串。
在第二个中,为所有子图设置了相同的 xlabel
和 ylabel
。
要在标签中添加“°”符号,这样定义一个char
变量就足够了:
c='°'
然后使用 sprintf
为 xlabel
和 ylabel
生成字符串。
a=randi(100,6,20)
figure
% Each subplot with its own xlabel and ylabel
for i=1:6
hs(i)=subplot(3,2,i);
plot(a(i,:))
c='°'
str=sprintf('Temp [C%c]',c)
xlabel([str ' ' num2str(i)])
ylabel(['Subplot ' num2str(i)])
grid on
end
figure
% The same xlabel and ylabel for all the subplot
c='°';
x_label_str=sprintf('Temp [C%c]',c)
y_label_str='Same for all'
for i=1:6
hs(i)=subplot(3,2,i);
plot(a(i,:))
xlabel(x_label_str)
ylabel(y_label_str)
grid on
end
图 1:每个子图的不同 xlabel、ylabel
图2:每个子图的相同xlabel、ylabel
希望对您有所帮助。
我认为您可以使用以下来源的超级标签: