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일

0 개 추천

You need to explicitly set the XTick property as well. To rotate the labels, you can use the XTickLabelRotation property.

댓글 수: 5

Mattia Pasquali
Mattia Pasquali 2018년 3월 15일
Thanks Rik for your tip. Unfortunately, I'm already in trouble with XTickLabel when I try to put in string in a "general way", can you please specify with more details how to set Xtick in my case?
Something like this?
hold on;
yyaxis left
bar(bar_data)
yyaxis right
plot(line_data)
set(gca, 'XTickLabel',{'Tom','John','a','b','c','d','e','f','g'})
set(gca, 'XTick',1:9)%unless you specify x-values in plot and bar
set(gca, 'XTickLabelRotation',45)
hold off;
That's exactly what I meant! Thanks a lot for your help.
Actually, by creating the string cell for XTickLabel through a for loop, I was able to get what I wanted.
Just for information:
a=cell(1,9);
for ii=1:9
a{ii}=[data_file(ii).name]
hold on;
yyaxis left
bar(bar_data)
yyaxis right
plot(line_data)
set(gca, 'XTickLabel',a)
set(gca, 'XTick',1:9)%unless you specify x-values in plot and bar
set(gca, 'XTickLabelRotation',45)
hold off;
end
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개)

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

질문:

2018년 3월 15일

댓글:

2018년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by