简体   繁体   English

情节外的图例不适用于 Octave 中的 plotyy

[英]Legend outside plot does not work with plotyy in Octave

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).我正在尝试使用 plotyy 在 Octave 中创建一个图(在 Windows 上使用plotyy )并将图例放在图外(因为数据覆盖了图内的所有可用空间)。 The following MVCE should reproduce the issue fairly well:以下 MVCE 应该可以很好地重现该问题:

% 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

This the output of the code using the gnuplot graphics toolkit:这是使用gnuplot图形工具包的代码输出:

在此处输入图片说明

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).如您所见,图例没有超出图,第二个 y 轴不可见(看起来图的一部分实际上被截断了)。

I have tried using the qt and fltk graphics toolkits, which give issues of their own:我曾尝试使用qtfltk图形工具包,它们给出了自己的问题:

  1. With qt graphics toolkit使用qt图形工具包

在此处输入图片说明

  1. With fltk graphics toolkit使用fltk图形工具包

在此处输入图片说明

Can anoybody suggest a fix or at least workaround?任何人都可以建议修复或至少解决方法吗? Does the same issue also happen in MATLAB or is it Octave-specific?同样的问题是否也发生在 MATLAB 中,还是 Octave 特定的?

EDIT Using the suggestion in Tasos' answer, I managed to almost make it work with gnuplot :编辑使用 Tasos 回答中的建议,我设法几乎让它与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

Which produces:其中产生:

在此处输入图片说明

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.除了图例与第二个 y 轴标签重叠(它不在我的屏幕上,仅在打印到jpg )这一事实之外,问题在于 Octave 似乎出于某种原因将两个图例叠加在一起:一个将第一组数据附加到第一组轴,另一个带有完整的数据集,用于第一个图例顶部的两个轴。 This is obviously wrong, and trying to set the Visible property of hl to off deletes both legends, not just the one.这显然是错误的,尝试将hlVisible属性设置为off会删除两个图例,而不仅仅是一个图例。

UPDATED : deals with both legend placement and OpenGL precision affecting graph.更新:处理图例位置和 OpenGL 精度影响图。

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.关于图例没有准确出现在您想要的位置的问题,您可以手动调整图形中涉及的所有轴的位置,将它们准确放置在您想要的位置。

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.针对OpenGL在小数和大数相加时无法处理精度的问题,只绘制小数的图形,然后简单地调整xticklabels以对应你想要的数字。

Full code below:完整代码如下:

% 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', []);

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM