Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help. Need to plot with two x-scales. First is data, second is index

Subject: Help. Need to plot with two x-scales. First is data, second is index

From: Jason

Date: 7 May, 2011 02:39:04

Message: 1 of 12

Hello all, I want to if this is possible with matlab. I have no problems with plotting. I'm plotting my data that starts, say, x = 405 : 509 and the corresponding y = whatever. The x-scale on the plot would start at 405 and end at 509. So no problem with plotting.

However, I want to add the index number to the x-scale as well. So on the x-scale, it would look like, 405, 406, 407, and so on... but right below it, (or above it), it would also have, 1, 2, 3, and so on.

Can you help me with this?

Thanks.

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 7 May, 2011 02:41:35

Message: 2 of 12

On 5/6/2011 9:39 PM, Jason wrote:
> Hello all, I want to if this is possible with matlab. I have no problems
> with plotting. I'm plotting my data that starts, say, x = 405 : 509 and
> the corresponding y = whatever. The x-scale on the plot would start at
> 405 and end at 509. So no problem with plotting.
>
> However, I want to add the index number to the x-scale as well. So on
> the x-scale, it would look like, 405, 406, 407, and so on... but right
> below it, (or above it), it would also have, 1, 2, 3, and so on.
> Can you help me with this?

The Matlab documentation can -- there's an example of multiple axes in
the Graphics section...it's even called "using multiple axes" or similar.

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: Jason

Date: 7 May, 2011 15:50:20

Message: 3 of 12

dpb <none@non.net> wrote in message <iq2bgu$ric$1@speranza.aioe.org>...
> On 5/6/2011 9:39 PM, Jason wrote:
> > Hello all, I want to if this is possible with matlab. I have no problems
> > with plotting. I'm plotting my data that starts, say, x = 405 : 509 and
> > the corresponding y = whatever. The x-scale on the plot would start at
> > 405 and end at 509. So no problem with plotting.
> >
> > However, I want to add the index number to the x-scale as well. So on
> > the x-scale, it would look like, 405, 406, 407, and so on... but right
> > below it, (or above it), it would also have, 1, 2, 3, and so on.
> > Can you help me with this?
>
> The Matlab documentation can -- there's an example of multiple axes in
> the Graphics section...it's even called "using multiple axes" or similar.
>
> --

Actually, I don't think it does what I want. I'm not looking for double axes or anything like that. I'm looking for on the same x-axis, put 405, 406, 407,... Then right below it, put the index numbers, 1, 2, 3, ... Then right below that is where the xlabel would go: "This is the x-axis". So I suppose you can call it that there are three separate items.

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 7 May, 2011 17:34:06

Message: 4 of 12

On 5/7/2011 10:50 AM, Jason wrote:
...

> Actually, I don't think it does what I want. I'm not looking for double
> axes or anything like that. I'm looking for on the same x-axis, put 405,
> 406, 407,... Then right below it, put the index numbers, 1, 2, 3, ...
> Then right below that is where the xlabel would go: "This is the
> x-axis". So I suppose you can call it that there are three separate items.


"Salt to suit..."

You don't have to duplicate the example, it just shows how to deal with
multiple axes.

AFIAK there's no way to make two sets of tick labels on a single axis so
you're going to have to improvise.

Two ways I see are:
   a) another axis to get another tick label property, or
   b) write the additional text w/ text()

The latter might be more easier given the amplified request.

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 7 May, 2011 20:33:41

Message: 5 of 12

On 5/7/2011 12:34 PM, dpb wrote:
> On 5/7/2011 10:50 AM, Jason wrote:
> ...
>
>> Actually, I don't think it does what I want. I'm not looking for double
>> axes or anything like that. I'm looking for on the same x-axis, put 405,
>> 406, 407,... Then right below it, put the index numbers, 1, 2, 3, ...
>> Then right below that is where the xlabel would go: "This is the
>> x-axis". So I suppose you can call it that there are three separate
>> items.
>
>
> "Salt to suit..."
>
> You don't have to duplicate the example, it just shows how to deal with
> multiple axes.
>
> AFIAK there's no way to make two sets of tick labels on a single axis so
> you're going to have to improvise.
>
> Two ways I see are:
> a) another axis to get another tick label property, or
> b) write the additional text w/ text()
>
> The latter might be more easier given the amplified request.

Had another idea, but it can't help...unless newer versions allow for
the Tex interpreter in the tick labels, wouldn't be able to use it to
modify the tick labels. So, I think your best bet is to write the
indices as text at the desired locations.

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: Jason

Date: 7 May, 2011 22:37:04

Message: 6 of 12

dpb <none@non.net> wrote in message <iq2bgu$ric$1@speranza.aioe.org>...
> On 5/6/2011 9:39 PM, Jason wrote:
> > Hello all, I want to if this is possible with matlab. I have no problems
> > with plotting. I'm plotting my data that starts, say, x = 405 : 509 and
> > the corresponding y = whatever. The x-scale on the plot would start at
> > 405 and end at 509. So no problem with plotting.
> >
> > However, I want to add the index number to the x-scale as well. So on
> > the x-scale, it would look like, 405, 406, 407, and so on... but right
> > below it, (or above it), it would also have, 1, 2, 3, and so on.
> > Can you help me with this?
>
> The Matlab documentation can -- there's an example of multiple axes in
> the Graphics section...it's even called "using multiple axes" or similar.
>
> --

Actually, I don't think it does what I want. I'm not looking for double axes or anything like that. I'm looking for on the same x-axis, put 405, 406, 407,... Then right below it, put the index numbers, 1, 2, 3, ... Then right below that is where the xlabel would go: "This is the x-axis". So I suppose you can call it that there are three separate items.

Subject: Help. Need to plot with two x-scales. First is data, second is

From: TideMan

Date: 7 May, 2011 22:54:10

Message: 7 of 12

On May 8, 10:37 am, "Jason " <core_a...@hotmail.com> wrote:
> dpb <n...@non.net> wrote in message <iq2bgu$ri...@speranza.aioe.org>...
> > On 5/6/2011 9:39 PM, Jason wrote:
> > > Hello all, I want to if this is possible with matlab. I have no problems
> > > with plotting. I'm plotting my data that starts, say, x = 405 : 509 and
> > > the corresponding y = whatever. The x-scale on the plot would start at
> > > 405 and end at 509. So no problem with plotting.
>
> > > However, I want to add the index number to the x-scale as well. So on
> > > the x-scale, it would look like, 405, 406, 407, and so on... but right
> > > below it, (or above it), it would also have, 1, 2, 3, and so on.
> > > Can you help me with this?
>
> > The Matlab documentation can -- there's an example of multiple axes in
> > the Graphics section...it's even called "using multiple axes" or similar.
>
> > --
>
> Actually, I don't think it does what I want. I'm not looking for double axes or anything like that.  I'm looking for on the same x-axis, put 405, 406, 407,... Then right below it, put the index numbers, 1, 2, 3, ... Then right below that is where the xlabel would go: "This is the x-axis". So I suppose you can call it that there are three separate items.

So actually what you want to do is simply change the labels of the
ticks.
405 becomes 1, 406 becomes 2, etc., correct?

xtic=get(gca,'XTick'); % Get existing ticks
xticnew=xtic - 404; % New tick labels
xticlab=num2str(xticnew'); % Convert to string
set(gca,...
    'XTickLabel',xticlab)

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 7 May, 2011 22:59:15

Message: 8 of 12

On 5/7/2011 5:54 PM, TideMan wrote:
...
> So actually what you want to do is simply change the labels of the
> ticks.
> 405 becomes 1, 406 becomes 2, etc., correct?
>
> xtic=get(gca,'XTick'); % Get existing ticks
> xticnew=xtic - 404; % New tick labels
> xticlab=num2str(xticnew'); % Convert to string
> set(gca,...
> 'XTickLabel',xticlab)

Except he wants both sets simultaneously...that takes another axis to
populate w/ ticklabels or writing one set (might as well be the indices
since the ticks come for free) w/ text().

Doesn't seem to be much inclination to simply _try_ something here...
it can't be that tough, even for a newbie (and I'm trying to coach here...)

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: TideMan

Date: 8 May, 2011 02:19:12

Message: 9 of 12

On May 8, 10:59 am, dpb <n...@non.net> wrote:
> On 5/7/2011 5:54 PM, TideMan wrote:
> ...
>
> > So actually what you want to do is simply change the labels of the
> > ticks.
> > 405 becomes 1, 406 becomes 2, etc., correct?
>
> > xtic=get(gca,'XTick');  % Get existing ticks
> > xticnew=xtic - 404;    % New tick labels
> > xticlab=num2str(xticnew');   % Convert to string
> > set(gca,...
> >      'XTickLabel',xticlab)
>
> Except he wants both sets simultaneously...that takes another axis to
> populate w/ ticklabels or writing one set (might as well be the indices
> since the ticks come for free) w/ text().
>
> Doesn't seem to be much inclination to simply _try_ something here...
> it can't be that tough, even for a newbie (and I'm trying to coach here...)
>
> --

I see....
When I have to do that, for example if I want a spectrum with both
frequency and period, I simply label the xticks at the top of the plot
with the different numbers, using text.

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 8 May, 2011 02:50:04

Message: 10 of 12

On 5/7/2011 9:19 PM, TideMan wrote:
> On May 8, 10:59 am, dpb<n...@non.net> wrote:
...

>> Except he wants both sets simultaneously...that takes another axis to
>> populate w/ ticklabels or writing one set (might as well be the indices
>> since the ticks come for free) w/ text().
>>
>> Doesn't seem to be much inclination to simply _try_ something here...
>> it can't be that tough, even for a newbie (and I'm trying to coach here...)
...
> I see....
> When I have to do that, for example if I want a spectrum with both
> frequency and period, I simply label the xticks at the top of the plot
> with the different numbers, using text.

Ayup, 'cept he's also said they both are to be on the bottom...of
course, same thing works, just probably will have to raise the bottom
position of the axis a little to gain some room then text()

Or, the other axis w/o any data and at the bottom instead of top as the
multi-axis example shows...

Again, it's "salt to suit" here....iffen OP would just spearmint some
he'd learn his way 'round handle graphics in a few minutes of trial 'n
error.

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 8 May, 2011 18:13:09

Message: 11 of 12

On 5/7/2011 12:34 PM, dpb wrote:
...

> ...there's no way to make two sets of tick labels on a single axis so
> you're going to have to improvise.
>
> Two ways I see are:
> a) another axis to get another tick label property, or
> b) write the additional text w/ text()
>
> The latter might be more easier given the amplified request.
...

Actually, it's about the only way--I was thinking one could control axis
color independently of the ticklabels so setting it to plot background
would hide the axis and ticks but still be able to show the tick labels.
  Doesn't work that way so the second-axis solution doesn't really work
as initially envisioned.

But, it's easy enough to write the desired numbers as text at the
locations desired; as noted you'll probably need to make a little room
by raising the bottom position of the axis from its default position.

--

Subject: Help. Need to plot with two x-scales. First is data, second is

From: dpb

Date: 9 May, 2011 15:34:29

Message: 12 of 12

On 5/7/2011 9:50 PM, dpb wrote:
> On 5/7/2011 9:19 PM, TideMan wrote:
...

> Ayup, 'cept he's also said they both are to be on the bottom...of
> course, same thing works, just probably will have to raise the bottom
> position of the axis a little to gain some room then text()
...

 >> x=405:509;
 >> y=sqrt(x);
 >> plot(x,y)
 >> pos=get(gca,'position');
 >> pos(2)=1.2*pos(2);
 >> set(gca,'position',pos)
 >> xt=get(ax1,'xtick');
 >> axlim=axis;
 >> yt=ones(size(xt))*(axlim(3)-.25); % may want to compute the bias
 >> tlab=num2str(1); ...
    for idx=2:length(xt),tlab=strvcat(tlab,num2str(xt(idx)-xt(1)+1));end
 >> tlab=cellstr(tlab);
 >> text(xt,yt,tlab,'horizontal','center')
 >>

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us