【发布时间】:2015-06-11 21:33:59
【问题描述】:
正如标题所说,我很乐意做的是相当直截了当。我有一个 m=3,n=2 子图的网格。它们代表了测量相同参数的 6 个不同实验的图表。我想在六个子图的边界上有一个 x 标签和一个 y 标签。不幸的是,到目前为止,我还没有找到一种简单的方法来做到这一点。 (xlabel 只是在最后一个活动子图下放置一个 xlabel)。有谁知道这是怎么做到的?
哦,我将如何在带有度数符号的标签中显示摄氏度?(小圆圈...)
【问题讨论】:
正如标题所说,我很乐意做的是相当直截了当。我有一个 m=3,n=2 子图的网格。它们代表了测量相同参数的 6 个不同实验的图表。我想在六个子图的边界上有一个 x 标签和一个 y 标签。不幸的是,到目前为止,我还没有找到一种简单的方法来做到这一点。 (xlabel 只是在最后一个活动子图下放置一个 xlabel)。有谁知道这是怎么做到的?
哦,我将如何在带有度数符号的标签中显示摄氏度?(小圆圈...)
【问题讨论】:
您可以使用mtit 在子图周围创建一个不可见的轴。
mtit 返回该轴的句柄,然后您可以为其创建 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 字符串。
在第二个中,为所有 subplos 设置了相同的 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
【讨论】: