String X-axis for bar plus line plot

43 次查看(过去 30 天)
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?

采纳的回答

Rik
Rik 2018-3-15
You need to explicitly set the XTick property as well. To rotate the labels, you can use the XTickLabelRotation property.
  5 个评论
Rik
Rik 2018-3-15
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.
Mattia Pasquali
Mattia Pasquali 2018-3-16
I definitely mess up with that "end" while copy paste

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by