Ján Ondruška
Ján Ondruška

Reputation: 23

Linear and Non-linear axis in Matlab

I'm the MatLab newbie and I need some help to create a linear and non-linear axis in one chart. I need to make chart with 2 different X-axes. One X-axis displays 1000/T at the bottom and the second X-axis displays a T at the top of the chart.

Example figure:

Example figure

Do you have any idea how to solve this problem in MatLab? Thanks.

Upvotes: 1

Views: 3191

Answers (2)

hbaderts
hbaderts

Reputation: 14371

This can be done by simply creating a second axes object at the same place as the first. Let's first create some data:

x1 = 1:0.1:3.5;
x2 = 1./x1;
y = (0.5*(x1-2)).^3;

Now we can create a normal plot with the first axes, and get the axes handle:

plot(x1,y,'-r');
ax(1) = gca;

Then we create the second axes object, at the same position as the first, and make the color none so it is transparent and the plot from below is still visible. As this adds a second Y axis too, we simply remove the Y ticks of the second axis.

ax(2) = axes('Position',ax(1).Position,'XAxisLocation','top','Color','none');
set(ax(2),'YTick',[]);

Now lets just format the second X axis as we like. Let's set the limits to the minimum and maximum of the x2 vector, and make it logarithmic:

set(ax(2),'XLim',[min(x2),max(x2)]);
set(ax(2),'XScale','log');

Now we still have the problem that the XTicks of ax(1) are also displayed at the top, and the XTicks of ax(2) are displayed at the bottom. This can be fixed by removing the box around the existing axes and creating a third axis without any ticks but with a box.

box(ax(1),'off');
box(ax(2),'off');
ax(3) = axes('Position',ax(1).Position,'XTick',[],'YTick',[],'Box','on','Color','none');

Now finally we can link the axes to be able to zoom correctly

linkaxes(ax);

And that should be it...

Upvotes: 2

learnvst
learnvst

Reputation: 16193

There is documentation for having a graph with two y-axes on the Mathworks website . .

http://de.mathworks.com/help/matlab/creating_plots/plotting-with-two-y-axes.html

It should be trivial to covert the concepts to the x-axis.

Upvotes: 0

Related Questions