How do I get MATLAB to use custom labels for data in plots?

3k Views Asked by At

By this I mean, I have data that I would like to plot but each data corresponds to a different drug (aripiprazole, olanzapine, quetiapine and risperidone) and I would like the name of the drug to be displayed on the x axis under the data. For instance, in the following figure I would like the numbers 1, 2, 3 and 4 to be replaced with aripiprazole, olanzapine, quetiapine and risperidone respectively. But I would like these names to be going downwards on the page so that they fit on the figure.

enter image description here

1

There are 1 best solutions below

11
On BEST ANSWER

You can set the property manually:

Xlabels = get(gca,'XTickLabel');
new_X_labels = ['aripiprazole'; 'olanzapine  '; 'quetiapine  '; 'risperidone '];
set(gca,'XTickLabel',new_X_labels);

To rotate them, the easiest way might be to use XTICKLABEL_ROTATE, available at FileExchange: http://www.mathworks.com/matlabcentral/fileexchange/3486.

Note, the XTickLabel must be a character array. I added spaces to make sure each label had the same dimensionality. There are better ways to do this.


Edit: Also note that you'll want to change the XTicks themsevles. You have four categories, so the 0.5 values mean nothing. If you look at the MATLAB documentation, it should all be clear how to do this.

To get an (incomplete) list of properties that you can modify, simply type get(gca).


Edit 2: Minimal working example

data = rand(4,1);
plot(data);

xlim([0 5])
my_labels = ['aripiprazole'; 'olanzapine  '; 'quetiapine  '; 'risperidone '];
set(gca,'XTick',[0 1 2 3 4]);
set(gca,'XTickLabel',my_labels);
xticklabel_rotate