How can I set the histogram width

I want same distance between each bars. Now the distance between 1 and 10 is closer than that between 10 and 50 or 50 and 100.
How can I edit the graph?

Risposte (1)

Cris LaPierre
Cris LaPierre il 27 Nov 2023
Modificato: Cris LaPierre il 27 Nov 2023
You will need to turn your System Size values into categoricals. You haven't shared your data, so here's a simple example.
% What you currently have
systemSize = ([ones(4200,1);10*ones(500,1);50*ones(100,1);100*ones(50,1)]);
histogram(systemSize)
xticks([1 10 50 100])
% With x data type as categoricals
catSystemSize = categorical(systemSize);
histogram(catSystemSize)

5 Commenti

Thank you!
What if there are three items, how can I set?
Provide a sample data set. I suspect the result will requite using histcounts and bar
num_simulations = 10000;
system_sizes = [1, 10, 50, 100]; % System sizes in MW
% Define SOEC parameters
capex_system_SOEC_min = 2800; % Minimum Capex for the system in euro/kW
capex_system_SOEC_max = 5600; % Maximum Capex for the system in euro/kW
capex_stack_SOEC_percentage = 0.5; % Percentage of Capex system for the stack
% Generate SOEC Capex values
capex_system_SOEC_values = linspace(capex_system_SOEC_min, capex_system_SOEC_max, numel(system_sizes));
capex_stack_SOEC_values = capex_stack_SOEC_percentage * capex_system_SOEC_values;
capex_SOEC_total_values = (capex_system_SOEC_values + capex_stack_SOEC_values) ./ system_sizes;
% Define PEM parameters
capex_system_PEM_min = 1100; % Minimum Capex for the system in euro/kW
capex_system_PEM_max = 1800; % Maximum Capex for the system in euro/kW
capex_stack_PEM_percentage = 0.35; % Percentage of Capex system for the stack
% Generate PEM Capex values
capex_system_PEM_values = linspace(capex_system_PEM_min, capex_system_PEM_max, numel(system_sizes));
capex_stack_PEM_values = capex_stack_PEM_percentage * capex_system_PEM_values;
capex_PEM_total_values = (capex_system_PEM_values + capex_stack_PEM_values) ./ system_sizes;
% Define AEC parameters
capex_system_AEC_min = 500; % Minimum Capex for the system in euro/kW
capex_system_AEC_max = 1400; % Maximum Capex for the system in euro/kW
capex_stack_AEC_percentage = 0.35; % Percentage of Capex system for the stack
% Generate AEC Capex values
capex_system_AEC_values = linspace(capex_system_AEC_min, capex_system_AEC_max, numel(system_sizes));
capex_stack_AEC_values = capex_stack_AEC_percentage * capex_system_AEC_values;
capex_AEC_total_values = (capex_system_AEC_values + capex_stack_AEC_values) ./ system_sizes;
% Combine all Capex values for each technology
all_capex_values = [capex_SOEC_total_values; capex_PEM_total_values; capex_AEC_total_values];
% Create a grouped bar graph
figure;
bar(system_sizes, all_capex_values, 'grouped');
xlabel('System Size (MW)');
ylabel('Average Capex (euro/MW)');
title('Comparison of Capex for Different Technologies and System Sizes');
legend({'SOEC', 'PEM', 'AEC'}, 'Location', 'Northwest');
grid on;
It's my data set.
Your code is fine, except that you still need to convert your X values to categoricals. This allows the x position to be set by group number instead of numeric value.
This is the only line of code that needs to change.
bar(categorical(system_sizes), all_capex_values, 'grouped');
Thank you so much!

Accedi per commentare.

Prodotti

Release

R2021a

Tag

Richiesto:

il 27 Nov 2023

Commentato:

il 27 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by