0

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

  • consider using bar graph instead. – Sason Torosean Jul 22 '13 at 17:59
  • These are confidence interval plots and hence that would be difficult. If you have a way of doing these plots in MATLAB with bar graphs please do share http://stats.stackexchange.com/questions/64880/software-to-produce-confidence-interval-error-bars-from-summary-statistics-witho – Josh Pinto Jul 22 '13 at 18:02

1 Answers1

1

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
Emily
  • 35,688
  • 6
  • 93
  • 141
  • Good job at answering this question but I would like to know how to use XTickLabel_rotate, I know it gives some explanation in the MATLAB code but it isn't helpful since it asks me to give it XTick and I haven't the foggiest what XTick is. – Josh Pinto Jul 22 '13 at 18:48
  • @BrentonHorne Every MATLAB plot has an "axis" structure. You can access this by typing get(gca). This structure specifies everything necessary to generate the plot: titles, labels, tick marks, etc. To get the XTick from a plot, just type my_xtick = get(gca,'XTick'). This gets you a numerical vector of the x-axis values where a "tick mark" gets placed. In your plot, it's going to be [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5]. Typing get(gca,'XTickLabel') gets you the character array of what goes under those labels. – Emily Jul 22 '13 at 18:54
  • OK, so XTick=get(gca,'XTicklabel') and then just type xticklabel_rotate(XTick) at the end of the code you have in your answer? If so I have done that and it ain't working. It just gives me 1, 1.5, 2, 2.5, 3, ... on my X axis – Josh Pinto Jul 22 '13 at 19:02
  • Type help xticklabel_rotate and you will find a complete description of all arguments. At minimum you also need an angle of rotation. – Emily Jul 22 '13 at 19:03
  • @BrentonHorne If all you want is to rotate 90 degrees, then after setting your new labels, just type xticklabel_rotate; Please see the minimal working example in my post. – Emily Jul 22 '13 at 19:05
  • In the code it says the default angle is 90 degrees which is what I want so I don't think I need to give the angle of rotation. – Josh Pinto Jul 22 '13 at 19:05
  • @BrentonHorne Then just run the command; no argument. Please see my code in my answer. – Emily Jul 22 '13 at 19:08
  • http://i.stack.imgur.com/FyLEo.png is what I get from your code. (The line is not desirable) – Josh Pinto Jul 22 '13 at 19:10
  • @BrentonHorne yes, that looks right. Now, remove the plot(data) portion and it should apply it just to your plot. Please note that you may have to close the figure and re-plot it. The line was the random data that I had to generate. All you should need is the code from my_labels ... onward. – Emily Jul 22 '13 at 19:11
  • Great! Just one last thing I would like; how do I get the plot such that the sides of the sides of the graph, aripiprazole, and risperidone are on the graph and not on the graph's borders? – Josh Pinto Jul 22 '13 at 19:22
  • See my edit. Add xlim([0 5]) after your plot command. Change 0 and 5 to whatever numerical values give you the most appropriate looking plot. – Emily Jul 22 '13 at 19:26