两个 y 轴与相同的 x 轴

发布于 2024-11-18 10:19:19 字数 843 浏览 4 评论 0原文

可能的重复:
在单个图中绘制 4 条曲线,其中 3 条y 轴

假设我在 Matlab 中有以下数据集作为示例:

x = linspace(0, 9, 10);
y1=arrayfun(@(x) x^2,x);
y2=arrayfun(@(x) 2*x^2,x);
y3=arrayfun(@(x) x^4,x);

因此您可以看到它们具有相同的 x 轴。现在我想要下面的图:

一个 x 轴的限制为 0 到 9(这些限制也应该是刻度),有 N 个刻度(我希望能够自己定义 N),因此中间有 N-2 个刻度,因为 0 和9 本身已经是刻度。我希望 y1 和 y2 引用相同的 y 轴,该轴显示在左侧,并带有 0 和 max([y1, y2]) 的刻度以及中间的 M 个刻度。 比我想在右侧有另一个轴,其中 y3 指的是...

y1、y2 和 y3 应该在同一个图例框中有条目... 到目前为止谢谢!

编辑:argh刚刚发现这个:绘制4条曲线在一个图中,有 3 个 y 轴 也许我可以自己构建它......我现在就尝试!

编辑:使用对数 x 轴时会怎样?!

Possible Duplicate:
Plotting 4 curves in a single plot, with 3 y-axes

assuming I have the following dataset as an example here in Matlab:

x = linspace(0, 9, 10);
y1=arrayfun(@(x) x^2,x);
y2=arrayfun(@(x) 2*x^2,x);
y3=arrayfun(@(x) x^4,x);

thus you can see they have the SAME x-axis. Now I want the following plot:

one x-axis with the limits 0 to 9 (those limits should also be ticks) with N ticks (I want to be able to define N myself), thus having N-2 ticks inbetween because 0 and 9 itself are already ticks. I want y1 and y2 to refer to the same y-axis, which is being displayed on the left with ticks for 0 and max([y1, y2]) and M more ticks inbetween.
than I want to have another axis on the right, where y3 refers to...

y1, y2 and y3 should have entries in the same legend box...
thanks so far!

edit: argh just found this: Plotting 4 curves in a single plot, with 3 y-axes perhaps I can bould it up myself... I will try just right now!

EDIT: What when using logarithmic x-axis?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-11-25 10:19:19

请参阅有关使用多个 X 轴和 Y 轴的文档。像这样的事情应该可以解决问题:

figure
ax1 = gca;
hold on
plot(x,y1)
plot(x,y2)
ax2 = axes('Position',get(ax1,'Position'),...
       'XAxisLocation','top',...
       'YAxisLocation','right',...
       'Color','none',...
       'XColor','k','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
plot(x,y3,'Parent',ax2);

编辑:哎呀,错过了保持命令。现在应该可以工作了。此外,要删除顶部的第二个 x 轴,只需将 'XTickLabel',[] 添加到 axes 命令即可。

顺便说一句,您确实不应该将 arrayfun 用于 y1=arrayfun(@(x) x^2,x);。相反,请使用 .^ 运算符:y1=x.^2;。它的风格更好,速度也更快。

See this documentation on Using Multiple X- and Y-Axes. Something like this should do the trick:

figure
ax1 = gca;
hold on
plot(x,y1)
plot(x,y2)
ax2 = axes('Position',get(ax1,'Position'),...
       'XAxisLocation','top',...
       'YAxisLocation','right',...
       'Color','none',...
       'XColor','k','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
plot(x,y3,'Parent',ax2);

Edit: whoops, missed a hold command. Should work now. Also, to remove the second x-axis on top, simply add 'XTickLabel',[] to the axes command.

As an aside, you really shouldn't use arrayfun for y1=arrayfun(@(x) x^2,x);. Instead, use the .^ operator: y1=x.^2;. It's much better style and is much quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文