两个y轴具有相同的x轴 [英] Two y axis with the same x-axis

查看:128
本文介绍了两个y轴具有相同的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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);

因此,您可以看到它们具有相同的x轴.现在我想要以下情节:

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

一个x轴,其极限为0到9(这些极限也应该是刻度),其中N个刻度(我希望自己可以定义N个),因此介于N-2个刻度之间,因为0和9本身已经存在滴答声.我希望y1和y2指向相同的y轴,它在左侧显示为0和max([y1,y2])的刻度,中间还有M个刻度. 而不是我想在右侧有另一个轴,其中y3表示...

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和y3在同一图例框中应具有条目... 到目前为止,谢谢!

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

argh刚发现以下内容:

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!

编辑:使用对数x轴时该怎么办?!

What when using logarithmic x-axis?!

推荐答案

请参见

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);

糟糕,错过了一个保持命令.现在应该工作.另外,要删除顶部的第二个x轴,只需在axes命令中添加'XTickLabel',[].

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

顺便说一句,您真的不应该将arrayfun用作y1=arrayfun(@(x) x^2,x);.而是使用.^运算符:y1=x.^2;.这是更好的样式,而且更快.

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.

这篇关于两个y轴具有相同的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆