Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: text labels for x values

Subject: text labels for x values

From: Jon

Date: 29 Jun, 2007 23:24:59

Message: 1 of 4

Hi all,

I am trying to create a bar graph that has text for x values.

For a (silly) example, let's say I have 4 values representing how
much of each type of fruit is in my fridge:

fruit = [2 5 6 7]

bar(fruit)

makes the bar plot just fine.

But here's what I want and am having trouble with:

Instead of the default x values being numeric (1 through 4)
I want to have x values be text. something like ['apples',
'oranges', 'strawberries', 'pears'].

Tried this and it doesn't work.

For instance if i do:
mylabels =['apples', 'oranges', 'strawberries', 'pears'];

bar(mylabels, fruit)

??? Error using ==> bar at 49
Must supply Y data or X and Y data as first argument(s).

I opened up bar.m and can't see how to edit it for a workaround,
except maybe seriously hacking into parseparams.m and bar.m. I was
hoping someone had an easy way to do this...maybe just adding text
below the x axis?

Didn't see any examples like this anywhere I searched.
So, if anyone out there knows how to do this, I would really
appreciate it.

thanks. jon

Subject: text labels for x values

From: NZTideMan

Date: 30 Jun, 2007 04:56:44

Message: 2 of 4

On Jun 30, 3:24 pm, Jon <ericksonNOspample...@caltech.edu> wrote:
> Hi all,
>
> I am trying to create a bar graph that has text for x values.
>
> For a (silly) example, let's say I have 4 values representing how
> much of each type of fruit is in my fridge:
>
> fruit = [2 5 6 7]
>
> bar(fruit)
>
> makes the bar plot just fine.
>
> But here's what I want and am having trouble with:
>
> Instead of the default x values being numeric (1 through 4)
> I want to have x values be text. something like ['apples',
> 'oranges', 'strawberries', 'pears'].
>
> Tried this and it doesn't work.
>
> For instance if i do:
> mylabels =['apples', 'oranges', 'strawberries', 'pears'];
>
> bar(mylabels, fruit)
>
> ??? Error using ==> bar at 49
> Must supply Y data or X and Y data as first argument(s).
>
> I opened up bar.m and can't see how to edit it for a workaround,
> except maybe seriously hacking into parseparams.m and bar.m. I was
> hoping someone had an easy way to do this...maybe just adding text
> below the x axis?
>
> Didn't see any examples like this anywhere I searched.
> So, if anyone out there knows how to do this, I would really
> appreciate it.
>
> thanks. jon

Welcome to the wonderful world of handle graphics.
Try this:
bar(fruit)
set(gca,'XTickLabel',{'apples', 'oranges', 'strawberries', 'pears'})

The second line tells Matlab to get the current axis and change the
label on the ticks on the x-axis to what's in the curly brackets.
Notice I used curly brackets, not square brackets - this is a cell
array.

Subject: text labels for x values

From: jon erickson

Date: 30 Jun, 2007 18:03:35

Message: 3 of 4

awesome, works perfectly. thanks.
jon
 NZTideMan wrote:
>
>
> On Jun 30, 3:24 pm, Jon <ericksonNOspample...@caltech.edu>
wrote:
>> Hi all,
>>
>> I am trying to create a bar graph that has text for x values.
>>
>> For a (silly) example, let's say I have 4 values representing
how
>> much of each type of fruit is in my fridge:
>>
>> fruit = [2 5 6 7]
>>
>> bar(fruit)
>>
>> makes the bar plot just fine.
>>
>> But here's what I want and am having trouble with:
>>
>> Instead of the default x values being numeric (1 through 4)
>> I want to have x values be text. something like ['apples',
>> 'oranges', 'strawberries', 'pears'].
>>
>> Tried this and it doesn't work.
>>
>> For instance if i do:
>> mylabels =['apples', 'oranges', 'strawberries', 'pears'];
>>
>> bar(mylabels, fruit)
>>
>> ??? Error using ==> bar at 49
>> Must supply Y data or X and Y data as first argument(s).
>>
>> I opened up bar.m and can't see how to edit it for a
workaround,
>> except maybe seriously hacking into parseparams.m and bar.m. I
> was
>> hoping someone had an easy way to do this...maybe just adding
> text
>> below the x axis?
>>
>> Didn't see any examples like this anywhere I searched.
>> So, if anyone out there knows how to do this, I would really
>> appreciate it.
>>
>> thanks. jon
>
> Welcome to the wonderful world of handle graphics.
> Try this:
> bar(fruit)
> set(gca,'XTickLabel',{'apples', 'oranges', 'strawberries',
> 'pears'})
>
> The second line tells Matlab to get the current axis and change the
> label on the ticks on the x-axis to what's in the curly brackets.
> Notice I used curly brackets, not square brackets - this is a cell
> array.
>
>

Subject: text labels for x values

From: Michael Marszalik

Date: 12 Nov, 2007 05:26:04

Message: 4 of 4

Is it possible to do this, but make the x=axis labes
vertical?

thanks, mike



"jon erickson" <erickson.nospam@caltech.edu> wrote in
message <ef5c22e.1@webcrossing.raydaftYaTP>...
> awesome, works perfectly. thanks.
> jon
> NZTideMan wrote:
> >
> >
> > On Jun 30, 3:24 pm, Jon
<ericksonNOspample...@caltech.edu>
> wrote:
> >> Hi all,
> >>
> >> I am trying to create a bar graph that has text for x
values.
> >>
> >> For a (silly) example, let's say I have 4 values
representing
> how
> >> much of each type of fruit is in my fridge:
> >>
> >> fruit = [2 5 6 7]
> >>
> >> bar(fruit)
> >>
> >> makes the bar plot just fine.
> >>
> >> But here's what I want and am having trouble with:
> >>
> >> Instead of the default x values being numeric (1
through 4)
> >> I want to have x values be text. something like
['apples',
> >> 'oranges', 'strawberries', 'pears'].
> >>
> >> Tried this and it doesn't work.
> >>
> >> For instance if i do:
> >> mylabels =
['apples', 'oranges', 'strawberries', 'pears'];
> >>
> >> bar(mylabels, fruit)
> >>
> >> ??? Error using ==> bar at 49
> >> Must supply Y data or X and Y data as first argument
(s).
> >>
> >> I opened up bar.m and can't see how to edit it for a
> workaround,
> >> except maybe seriously hacking into parseparams.m and
bar.m. I
> > was
> >> hoping someone had an easy way to do this...maybe just
adding
> > text
> >> below the x axis?
> >>
> >> Didn't see any examples like this anywhere I searched.
> >> So, if anyone out there knows how to do this, I would
really
> >> appreciate it.
> >>
> >> thanks. jon
> >
> > Welcome to the wonderful world of handle graphics.
> > Try this:
> > bar(fruit)
> > set(gca,'XTickLabel',
{'apples', 'oranges', 'strawberries',
> > 'pears'})
> >
> > The second line tells Matlab to get the current axis
and change the
> > label on the ticks on the x-axis to what's in the curly
brackets.
> > Notice I used curly brackets, not square brackets -
this is a cell
> > array.
> >
> >

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics