String X-axis for bar plus line plot
44 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mattia Pasquali
el 15 de Mzo. de 2018
Comentada: Mattia Pasquali
el 16 de Mzo. de 2018
Hello everyone, I'm having a hard time trying to setup my string-based X-axis for a plot which consists of a bar plot which refers to the left Y axis and a line plot which refers to the right one. Furthermore, I'm trying to generate this kind of plot in a script so I need my code to be "as general as possible". Here an example of the kind of plot I'm looking for

Concerning the X-axis, I have 9 elements to which one bar and one line point correspond to. These elements are nothing but the filename of files that I have previously imported via importdata. These are store in a 1x9 structure and can be access via "data_file(ii).name". I managed to store them as well in a 1x9 cell if needed (i.e. name={'tom','john'...}). Both the bar data (bar_data) and line data (line_data) are store in two different 1x9 double vectors. So far this is the closest I got toward what I need:
hold on;
yyaxis left
bar(bar_data)
yyaxis right
plot(line_data)
set(gca, 'XTickLabel',{'Tom','John','a','b','c','d','e','f','g'})
hold off;
This is what I got:

Regardless the outlook of the graph, I need to correct the following features:
- 1st, the X labeling should start with "Tom" laying under the first blue bar and finishing with "g" laying under the last blue bar
- 2nd, whenever I try to set the X-axis labels in a "more general way" (i.e. not writing the label one by one but rather using a for cycle "for ii=1:9") the whole plot got screwed. As mention previously, this plotting operation is part of a script so I need to generate such a plot with a "general code".
I need to fix these things... Thanks in advance for you help.
Mattia
PS: the bar_data values as well as line_data change every time the script is run along with the number of element and the "Xlabels".
PPS: is there a way to tilt the label in case this later are too long?
0 comentarios
Respuesta aceptada
Rik
el 15 de Mzo. de 2018
You need to explicitly set the XTick property as well. To rotate the labels, you can use the XTickLabelRotation property.
5 comentarios
Rik
el 15 de Mzo. de 2018
You can end the loop just after the assignment to a, the outcome will be identical, without the need to call bar 9 times.
And you're welcome of course.
Más respuestas (0)
Ver también
Categorías
Más información sobre Discrete Data Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!