0

我正在尝试做一个像这样的 Hertzsprung-Russell 图:http://en.wikipedia.org/wiki/File: HRDiagram.png 我有 4 种不同类型的数据用于同一点(我们称之为 x1,y1 , x2, y2) 并且我想将它们放在同一个图中,显示 4 个轴(其中 2 个轴像往常一样位于底部和左侧,另外 2 个位于顶部和右侧)。我的代码:

filename = 'Hipparcos_stars2.xlsx';
sheet1 = 1;

xHRcolor = 'G2:G19920'; 
yHRmag = 'I2:I19920'; 
xHRtemp = 'J2:J19920';
yHRlum = 'K2:K19920';
HRx_c = xlsread(filename, sheet1, xHRcolor); % x1
HRy_m = xlsread(filename, sheet1, yHRmag); % y1
HRx_t = xlsread(filename, sheet1, xHRtemp); % x2
HRy_l = xlsread(filename, sheet1, yHRlum); % y2

figure(1)
ax1 = gca;
line(HRx_c,HRy_m, 'LineStyle', 'o', 'Color', 'k', 'MarkerSize', 1);
set(ax1,'ydir','reverse');

ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','top',...
           'YAxisLocation','right');
line(HRx_t,log10(HRy_l),'LineStyle', 'o','Color','k','MarkerSize', 1,'Parent',ax2);
set(ax2,'xdir','reverse');

xlabel(ax1,'B-V color')
ylabel(ax1,'V Magnitude')
xlabel(ax2,'Temperature [ºK]')
ylabel(ax2,'Luminosity [log(L/Ls)]')

像这样,我最终得到一个带有 4 轴的图形,但关于第二组轴的信息错误。问题是,使用此代码,它不会使第二组轴适应数据,并且对于我选择的任何点,它呈现 x2 和 y2 的正确值,但 x1 和 y1 的值错误。知道如何在维基百科中做类似的事情吗?提前致谢

对于 Hipparcos_stars2.xlsx:http ://www1.datafilehost.com/d/7d652c04

4

2 回答 2

1

Here is your code with slight modifications to make it work:

%# some random data
x1 = linspace(0,1,100);
y1 = exp(x1) .* cos(x1);
x2 = linspace(10,100,100);
y2 = cumsum(rand(size(x2))-0.5);

%# plot
figure
hAx(1) = axes('YDir','reverse', 'XColor','b', 'YColor','b');
hAx(2) = axes('Position',get(hAx(1),'Position'), 'XDir','reverse', ...
    'XAxisLocation','top', 'YAxisLocation','right', 'Color','none', ...
    'XColor','r', 'YColor','r');
linkprop(hAx, 'Position');
line(x1,y1, 'Color','b', 'LineStyle','none', 'Marker','o', 'MarkerSize',2, ...
    'Parent',hAx(1));
line(x2,y2, 'Color','r', 'LineStyle','none', 'Marker','.', 'MarkerSize',5, ...
    'Parent',hAx(2));
xlabel(hAx(1), 'B-V color')
ylabel(hAx(1), 'V Magnitude')
xlabel(hAx(2), 'Temperature [ºK]')
ylabel(hAx(2), 'Luminosity [log(L/Ls)]')

screenshot

Note that the second axis is made transparent by setting its color property to 'none'. Also the correct way of drawing a scatter of points is to set the line property to 'none' while specifying the marker using 'Marker' property.

Also note the use of linkprop to link the position property of both axes, that way if one is changed, the other follows.

于 2013-04-29T12:40:29.307 回答
1

您可以使用plotyy()然后自定义外观:

h = plotyy(HRx_c,HRy_m,HRx_t,HRy_l,'plot','semilogy')
set(h(1),'ydir','reverse');
set(h(2),'xdir','reverse');
于 2013-04-29T12:34:35.677 回答