图例外的图例不适用于 Octave 中的 plotyy [英] Legend outside plot does not work with plotyy in Octave
问题描述
我正在尝试使用 plotyy
在 Octave 中创建一个图(在 Windows 上使用 v4.4.1)并将图例放在图外(因为数据覆盖了图内的所有可用空间).以下 MVCE 应该可以很好地重现该问题:
I am trying to create a plot in Octave (using v4.4.1 on Windows) using plotyy
and putting the legend outside the plot (because the data covers all the usable space inside the graph). The following MVCE should reproduce the issue fairly well:
% Generate some random data to reproduce the issue
data = rand(1000,10);
data(:,1:8) = data(:,1:8)-0.5;
data(:,9:10) = data(:,9:10)+30;
timedate = linspace(737310,737313,size(data,1));
data_labels={'1';'2';'3';'4';'5';'6';'7';'8';'9';'10'};
% Plot the data
figure('Name','MVCE','Position',[300 200 1000 600])
[ax,h1,h2] = plotyy(timedate,data(:,1:8),timedate,data(:,9:10));
set(h2,'Visible','on');
datetick(ax(1),'x','HH:MM:SS')
datetick(ax(2),'x','HH:MM:SS')
ylim(ax(1),[-1 1])
ylim(ax(2),[20 50])
xlabel('Date & time')
ylabel(ax(1),'Something')
ylabel(ax(2),'Something else')
title('plotyy graph with legend problem')
[hl,hlo] = legend([h1;h2],data_labels,'location','eastoutside');
grid on
这是使用 gnuplot
图形工具包的代码输出:
This the output of the code using the gnuplot
graphics toolkit:
如您所见,图例并未超出绘图范围,并且第二个 y 轴不可见(看起来绘图的一部分实际上被截断了).
As you can see, the legend does not go outside the plot, and the second y axis is not visible (it looks like part of the plot is actually truncated).
我曾尝试使用 qt
和 fltk
图形工具包,它们各自有问题:
I have tried using the qt
and fltk
graphics toolkits, which give issues of their own:
- 使用
qt
图形工具包
- With
qt
graphics toolkit
- 使用
fltk
图形工具包
- With
fltk
graphics toolkit
有人可以建议修复或至少解决方法吗?同样的问题是否也发生在 MATLAB 中,还是 Octave 特定的?
Can anoybody suggest a fix or at least workaround? Does the same issue also happen in MATLAB or is it Octave-specific?
编辑使用 Tasos 回答中的建议,我几乎设法让它与 gnuplot
一起使用:
EDIT
Using the suggestion in Tasos' answer, I managed to almost make it work with gnuplot
:
% Plot the data
figure('Name','MVCE','Position',[300 200 1000 600])
[ax,h1,h2] = plotyy(timedate,data(:,1:8),timedate,data(:,9:10));
set(h2,'Visible','on');
datetick(ax(1),'x','HH:MM:SS')
datetick(ax(2),'x','HH:MM:SS')
ylim(ax(1),[-1 1])
ylim(ax(2),[20 50])
ax1Pos = get(ax(1), 'position');
ax2Pos = get(ax(2), 'position');
ax1Pos(3) = ax1Pos(3) * 0.73;
ax2Pos(3) = ax2Pos(3) * 0.73;
set(ax(1), 'position', ax2Pos);
set(ax(2), 'position', ax2Pos);
xlabel('Date & time')
ylabel(ax(1),'Something')
ylabel(ax(2),'Something else')
title('plotyy graph with legend problem')
[hl,hlo] = legend([h1;h2],data_labels,'location','eastoutside');
pos = get(hl,'Position');
pos(1) = 0.9;
set(hl,'Position',pos)
grid on
产生:
除了图例与第二个 y 轴标签重叠(它不在我的屏幕上,仅在打印到 jpg
时)之外,问题在于 Octave 似乎绘制了两个由于某种原因,图例彼此重叠:一个将第一组数据附加到第一组轴上,另一个带有完整的数据集,两个轴都位于第一个图例的正上方.这显然是错误的,尝试将 hl
的 Visible
属性设置为 off
会删除两个图例,而不仅仅是一个.
Apart from the fact that the legend overlays with the second y axis label (which it doesn't on my screen, only when printing to jpg
), the problem is that Octave appears to plot two legends on top of each other for some reason: one with the first set of data attached to the first set of axes, and one with the complete set of data, for both axes right on top of the first legend. This is obviously wrong, and trying to set the Visible
property of hl
to off
deletes both legends, not just the one.
推荐答案
UPDATED:处理图例放置和 OpenGL 精度影响图.
UPDATED: deals with both legend placement and OpenGL precision affecting graph.
关于图例没有准确出现在您想要的位置的问题,您可以手动调整图形中涉及的所有轴的位置,将它们准确放置在您想要的位置.
Regarding the problem of the legend not appearing exactly in the position you want it to, you can manually adjust the position of all axes involved in a figure, to place them exactly where you want.
针对OpenGL无法处理小数到大数相加精度的问题,只画小数的图形,然后简单的调整xticklabels对应你想要的数字.
Regarding the problem of OpenGL being unable to deal with the precision involved when adding small numbers to a large number, plot the graph with only the small numbers involved, and then simply adjust the xticklabels to correspond to the numbers you desire.
完整代码如下:
% Generate some random data to reproduce the issue
data = rand(1000,10);
data(:,1:8) = data(:,1:8)-0.5;
data(:,9:10) = data(:,9:10)+30;
t_offset = 737310;
timedate = linspace(0,3,size(data,1));
data_labels={'1';'2';'3';'4';'5';'6';'7';'8';'9';'10'};
% Plot the data
figure('Name','MVCE','Position',[300 200 1000 600])
[ax,h1,h2] = plotyy(timedate,data(:,1:8),timedate,data(:,9:10));
set(h2,'Visible','on');
ylim(ax(1),[-1 1])
ylim(ax(2),[20 50])
ylabel(ax(1),'Something')
ylabel(ax(2),'Something else')
title('plotyy graph with legend problem')
[hl,hlo] = legend([h1;h2],data_labels,'location','eastoutside');
set(hl, 'position', get(hl, 'position') .* [0.975, 1, 0.975, 1] )
grid on
ax1Pos = get(ax(1), 'position'); ax2Pos = get(ax(2), 'position');
ax1Pos(3) = ax1Pos(3) * 0.95; ax2Pos(3) = ax2Pos(3) * 0.95;
set(ax(1), 'position', ax2Pos); set(ax(2), 'position', ax2Pos);
XTicks = get(ax(1), 'xtick');
set(ax(1), 'xticklabel', datestr(XTicks + t_offset, 'HH:MM:SS'))
xlabel('Date & time')
set(ax(2), 'xtick', []);
输出:
这篇关于图例外的图例不适用于 Octave 中的 plotyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!