Test Disappear from Chart

Hello everyone
Kindly I am facing an Issue with Matlab trying to print a Chart but that one when I write a text in Xlabel as a one line it works fine but if I add new line it Disappear
As you can see in this chart its one line but this line is long so I need to add new line and when I add new Line one of these text diappear as in below
You can see when I added new Line the Text on the left diapeared
I am using this for the one line:
x_labels{cat_indices(max_accuracy_index)} = [cat, ' - Method: ', method{cat_indices(max_accuracy_index)}, ' Accuracy: ', num2str(max_accuracy), ' Time: ', num2str(time_period(cat_indices(max_accuracy_index)))];
when I added new line I used this:
x_labels{cat_indices(max_accuracy_index)} = sprintf('%s - Method: %s\nAccuracy: %.2f Time: %.2f', cat, method{cat_indices(max_accuracy_index)}, max_accuracy, time_period(cat_indices(max_accuracy_index)));
This causing one text disapearing, I tried to change the Figure size but it doesn't work
figure('Position', [100, 100, 1000, 600]);
and I even change it to
figure('Position', [100, 100, 2000, 600]);
but still no change
I hope if someone experienced such thing could help me

Answers (1)

Try using '\\newline' instead of '\n' in sprintf:
cat = 'Final Bump Road Obstacle';
method = {'WaveletScattering and SVM Tuned RFE'};
cat_indices = 1;
max_accuracy_index = 1;
max_accuracy = 100;
time_period = 70;
x_labels{cat_indices(max_accuracy_index)} = sprintf('%s - Method: %s\\newlineAccuracy: %.2f Time: %.2f', cat, method{cat_indices(max_accuracy_index)}, max_accuracy, time_period(cat_indices(max_accuracy_index)));
x_labels = 1×1 cell array
{'Final Bump Road Obstacle - Method: WaveletScattering and SVM Tuned RFE\newlineAccuracy: 100.00 Time: 70.00'}
figure('Position',[10 10 1000 500])
plot(1:10)
xticks(1:2:10)
xticklabels(x_labels(ones(1,numel(xticks())))) % repeating the same label 5 times

6 Comments

I have tried but it keeps same issue
When you say, "it keeps same issue", same as what? Same as the version with '\n'?
What version of MATLAB are you using? '\\newline' may not work in older versions, although I've tested it in R2017b and R2022a as well as R2023b (i.e., in this forum) and it works in all of those versions.
Here's another demo to show that it works, at least in 2023b.
cat = 'Final Bump Road Obstacle';
method = {'WaveletScattering and SVM Tuned RFE'};
cat_indices = 1;
max_accuracy_index = 1;
max_accuracy = 100;
time_period = 70;
First, let me show your one-line version:
x_labels{cat_indices(max_accuracy_index)} = [cat, ' - Method: ', method{cat_indices(max_accuracy_index)}, ' Accuracy: ', num2str(max_accuracy), ' Time: ', num2str(time_period(cat_indices(max_accuracy_index)))]
x_labels = 1×1 cell array
{'Final Bump Road Obstacle - Method: WaveletScattering and SVM Tuned RFE Accuracy: 100 Time: 70'}
figure('Position',[10 10 1000 500])
plot(1:10)
xticks(1:2:10)
xticklabels(x_labels(ones(1,numel(xticks())))) % repeating the same label 5 times
And your two-line version with '\n':
x_labels{cat_indices(max_accuracy_index)} = sprintf('%s - Method: %s\nAccuracy: %.2f Time: %.2f', cat, method{cat_indices(max_accuracy_index)}, max_accuracy, time_period(cat_indices(max_accuracy_index)))
x_labels = 1×1 cell array
{'Final Bump Road Obstacle - Method: WaveletScattering and SVM Tuned RFE↵Accuracy: 100.00 Time: 70.00'}
figure('Position',[10 10 1000 500])
plot(1:10)
xticks(1:2:10)
xticklabels(x_labels(ones(1,numel(xticks())))) % repeating the same label 5 times
You can see that neither of those provide the desired result.
Now, my proposed solution ('\\newline'):
x_labels{cat_indices(max_accuracy_index)} = sprintf('%s - Method: %s\\newlineAccuracy: %.2f Time: %.2f', cat, method{cat_indices(max_accuracy_index)}, max_accuracy, time_period(cat_indices(max_accuracy_index)))
x_labels = 1×1 cell array
{'Final Bump Road Obstacle - Method: WaveletScattering and SVM Tuned RFE\newlineAccuracy: 100.00 Time: 70.00'}
figure('Position',[10 10 1000 500])
plot(1:10)
xticks(1:2:10)
xticklabels(x_labels(ones(1,numel(xticks())))) % repeating the same label 5 times
Seems to do the correct thing: each x-tick label is split across two lines of text.
Hello Dear ,
Kindly i mean it kept same. means still the final bump diappearing...
I will provide you with the full code and the data in the attached so you could know what i went throw ,, my Matlab version is 2023b
Below is my code and the attached is my data
% Read the Excel file into a table
data = readtable('firsttest.xlsx');
% Access the data from the table
accuracy = str2double(data.Accuracy);
time_period = str2double(data.TimePeriod);
method = data.Method;
category = data.Category;
% Define categories
categories = unique(category);
% Create a grouped bar chart with a larger figure size
figure('Position', [10, 10, 1000, 500]);
% Define the width of the bars
barWidth = 0.4;
% Create a vector of indices for x-coordinates
x = 1:length(accuracy);
% Initialize custom labels for x-axis
x_labels = cell(1, length(x));
% Define custom colors for highlighting
highlight_color = [0, 0, 0]; % Black color for Accuracy highlight
highlight_color_yellow = [1, 1, 0]; % Yellow color for Time period highlight
% Create a cell array to store the highlighted data points
highlighted_data = cell(1, length(categories));
% Initialize legend entries with custom labels and colors
for i = 1:length(categories)
cat = categories{i}; % Get the current category as a string
cat_indices = find(strcmp(category, cat));
% Find the indices of points with the highest Accuracy for this category
max_accuracy = max(accuracy(cat_indices));
max_accuracy_indices = find(accuracy(cat_indices) == max_accuracy);
% Find the lowest Time period within the highest Accuracy group
if length(max_accuracy_indices) > 1
min_time_period = min(time_period(cat_indices(max_accuracy_indices)));
min_time_period_indices = find(time_period(cat_indices(max_accuracy_indices)) == min_time_period);
max_accuracy_index = max_accuracy_indices(min_time_period_indices);
else
max_accuracy_index = max_accuracy_indices;
end
% Create bars for Accuracy (in blue) for all data points in the category
bar(x(cat_indices), accuracy(cat_indices), barWidth, 'b', 'DisplayName', ['Accuracy: ' num2str(max_accuracy)]);
hold on;
% Create bars for Time period (in red) for all data points in the category with an offset
bar(x(cat_indices) + barWidth, time_period(cat_indices), barWidth, 'r', 'DisplayName', ['Time: ' num2str(time_period(cat_indices(max_accuracy_index)))]);
% Highlight the corresponding bars for all categories by changing their color
bar(x(cat_indices(max_accuracy_index)), accuracy(cat_indices(max_accuracy_index)), barWidth, 'FaceColor', highlight_color);
bar(x(cat_indices(max_accuracy_index)) + barWidth, time_period(cat_indices(max_accuracy_index)), barWidth, 'FaceColor', highlight_color_yellow);
% Create a custom label with HTML formatting for the whole sentence
x_labels{cat_indices(max_accuracy_index)} = [cat, ' - Method: ', method{cat_indices(max_accuracy_index)}, ' Accuracy: ', num2str(max_accuracy), ' Time: ', num2str(time_period(cat_indices(max_accuracy_index)))];
% Store the highlighted data points for this category
highlighted_data{i} = x_labels{cat_indices(max_accuracy_index)};
end
% Set x-tick locations
xticks(x);
% Rotate the x-axis labels to avoid overlap
xtickangle(45);
% Add the custom labels to the x-axis
ax = gca;
ax.XTickLabel = x_labels;
% Set axis labels and title
xlabel('Category - Method, Accuracy, Time Period');
ylabel('Value');
title('Comparison of Accuracy and Time Period for Different Methods in Categories');
% Customize the plot appearance
grid on;
% ...
% Create the legend with custom entries and colors
lgd = legend('Accuracy', 'Time Period', 'Accuracy (Best Method)', 'Time Period (Best Method)');
set(lgd, 'TextColor', 'black');
legend_colors = [0, 0, 1; 1, 0, 0; 0, 0, 0; 1, 1, 0]; % Blue, Red, Black, Yellow
legend_colors(legend_colors == 0) = 1;
set(lgd, 'Location', 'Best', 'AutoUpdate', 'off');
% Print the highlighted data points
for i = 1:numel(categories)
fprintf('Category %s: %s\n', categories{i}, highlighted_data{i});
end
% Optionally, save the chart as an image file
% saveas(gcf, 'grouped_bar_chart.png');
% Hold off to end the plotting
hold off;
Thank you for the data and code. The \\newline solution appears to work in this case as well: each xticklabel has text on two lines. Is this not the desired result?
% Read the Excel file into a table
data = readtable('firsttest.xlsx');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% Access the data from the table
accuracy = str2double(data.Accuracy);
time_period = str2double(data.TimePeriod);
method = data.Method;
category = data.Category;
% Define categories
categories = unique(category);
% Create a grouped bar chart with a larger figure size
figure('Position', [10, 10, 1000, 500]);
% Define the width of the bars
barWidth = 0.4;
% Create a vector of indices for x-coordinates
x = 1:length(accuracy);
% Initialize custom labels for x-axis
x_labels = cell(1, length(x));
% Define custom colors for highlighting
highlight_color = [0, 0, 0]; % Black color for Accuracy highlight
highlight_color_yellow = [1, 1, 0]; % Yellow color for Time period highlight
% Create a cell array to store the highlighted data points
highlighted_data = cell(1, length(categories));
% Initialize legend entries with custom labels and colors
for i = 1:length(categories)
cat = categories{i}; % Get the current category as a string
cat_indices = find(strcmp(category, cat));
% Find the indices of points with the highest Accuracy for this category
max_accuracy = max(accuracy(cat_indices));
max_accuracy_indices = find(accuracy(cat_indices) == max_accuracy);
% Find the lowest Time period within the highest Accuracy group
if length(max_accuracy_indices) > 1
min_time_period = min(time_period(cat_indices(max_accuracy_indices)));
min_time_period_indices = find(time_period(cat_indices(max_accuracy_indices)) == min_time_period);
max_accuracy_index = max_accuracy_indices(min_time_period_indices);
else
max_accuracy_index = max_accuracy_indices;
end
% Create bars for Accuracy (in blue) for all data points in the category
bar(x(cat_indices), accuracy(cat_indices), barWidth, 'b', 'DisplayName', ['Accuracy: ' num2str(max_accuracy)]);
hold on;
% Create bars for Time period (in red) for all data points in the category with an offset
bar(x(cat_indices) + barWidth, time_period(cat_indices), barWidth, 'r', 'DisplayName', ['Time: ' num2str(time_period(cat_indices(max_accuracy_index)))]);
% Highlight the corresponding bars for all categories by changing their color
bar(x(cat_indices(max_accuracy_index)), accuracy(cat_indices(max_accuracy_index)), barWidth, 'FaceColor', highlight_color);
bar(x(cat_indices(max_accuracy_index)) + barWidth, time_period(cat_indices(max_accuracy_index)), barWidth, 'FaceColor', highlight_color_yellow);
% Create a custom label with HTML formatting for the whole sentence
% x_labels{cat_indices(max_accuracy_index)} = [cat, ' - Method: ', method{cat_indices(max_accuracy_index)}, ' Accuracy: ', num2str(max_accuracy), ' Time: ', num2str(time_period(cat_indices(max_accuracy_index)))];
x_labels{cat_indices(max_accuracy_index)} = sprintf('%s - Method: %s\\newlineAccuracy: %.2f Time: %.2f', cat, method{cat_indices(max_accuracy_index)}, max_accuracy, time_period(cat_indices(max_accuracy_index)));
% Store the highlighted data points for this category
highlighted_data{i} = x_labels{cat_indices(max_accuracy_index)};
end
% Set x-tick locations
xticks(x);
% Rotate the x-axis labels to avoid overlap
xtickangle(45);
% Add the custom labels to the x-axis
ax = gca;
ax.XTickLabel = x_labels;
% Set axis labels and title
xlabel('Category - Method, Accuracy, Time Period');
ylabel('Value');
title('Comparison of Accuracy and Time Period for Different Methods in Categories');
% Customize the plot appearance
grid on;
% ...
% Create the legend with custom entries and colors
lgd = legend('Accuracy', 'Time Period', 'Accuracy (Best Method)', 'Time Period (Best Method)');
set(lgd, 'TextColor', 'black');
legend_colors = [0, 0, 1; 1, 0, 0; 0, 0, 0; 1, 1, 0]; % Blue, Red, Black, Yellow
legend_colors(legend_colors == 0) = 1;
set(lgd, 'Location', 'Best', 'AutoUpdate', 'off');
% Print the highlighted data points
for i = 1:numel(categories)
fprintf('Category %s: %s\n', categories{i}, highlighted_data{i});
end
Category Bump Obstacle: Bump Obstacle - Method: Wavelet SVM Tuned (RFE)\newlineAccuracy: 100.00 Time: 79.13 Category Grid Obstacle: Grid Obstacle - Method: Wavelet SVM Tuned (RFE)\newlineAccuracy: 100.00 Time: 72.46 Category Pothole Obstacle: Pothole Obstacle - Method: Wavelet SVM Tuned (RFE)\newlineAccuracy: 100.00 Time: 66.54 Category Sinewave Obstacle: Sinewave Obstacle - Method: Wavelet SVM Tuned (RFE)\newlineAccuracy: 100.00 Time: 69.48
% Optionally, save the chart as an image file
% saveas(gcf, 'grouped_bar_chart.png');
% Hold off to end the plotting
hold off;
Yes dear this is working now
Thank you so much
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Sign in to comment.

Categories

Asked:

on 31 Oct 2023

Commented:

on 2 Nov 2023

Community Treasure Hunt

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

Start Hunting!