Help with Histogram Plot

13 次查看(过去 30 天)
Mahnoor
Mahnoor 2024-10-9
Hi, I am trying to create a histogram as shown in the picture below (bar length I have drawn out just as an example). I am struggling a bit with getting the 60-100 efficiency range on the y-axis and intervals of 0-1, 1-2, 2-3 on the x-axis. I have also attached the table, with the data, I am using.
I would highly appreciate any help with this (using 2020b). Thankyou.

回答(2 个)

Sean Cupitt
Sean Cupitt 2024-10-9
If it's okay with you, I'm describing how I would generate this bar graph. Others may have a more elegant solution using histogram. I'll start by assuming you have a [23x2] variable called "data", since I don't know how you've imported the data into MATLAB. Otherwise, everything else is included.
%
% filter data to remove Inf
partitionVector = all(isfinite(data),2);
xFinite = data(partitionVector, 1);
yFinite = data(partitionVector, 2);
%
% group and average data
% - create bin edge vector
binEdges = min(floor(xFinite)):1:max(ceil(xFinite));
% - create bin group designations (and edges just for completeness)
[Y, E] = discretize(xFinite,binEdges);
% - average the data that falls into each group
B = groupsummary(yFinite,Y,'mean');
%
% plot data
b = bar(B);
% - create a custom tick string for x axis (in axes containing bar graph)
xTickString = [num2str(E(1:end-1)') num2str(-E(2:end)')];
b.Parent.XTickLabel = {xTickString};
% - truncate lower y axis to 60%
origYLim = ylim;
ylim([60 origYLim(2)]);
grid on ; grid minor
ylabel("Average Efficiency (%)")
xlabel("Average Power (kW)")
  1 个评论
Mahnoor
Mahnoor 2024-10-10
Hello @Sean Cupitt. Thankyou so much for the support. Ive imported the data into MATLAB by using the Import icon and selecting as a table so the data is display directly in Worksapce. Or can use the below code:
fileName = fullfile(pwd, 'Log Analysis.xlsx');
data = readmatrix(fileName);
I have tried running this and changing the variable names accordingly. The table from the Excel sheet displays in the Command Window. But for some reason none of the plots pop up. Not sure what I am doing wrong over here? I have the same issue with the below code.
Thankyou :)

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-10-9
Your picture does not show a histogram. A histogram would have counts (frequency of occurrence) on the vertical axis, not the y value. If you want to plot the data and histogram (as it's actually defined) you can do this:
% Demo by Image Analyst.
% Initialization steps.
clc; % Clear the command window.
%fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Read in data.
fileName = fullfile(pwd, 'Log Analysis.xlsx');
data = readmatrix(fileName)
data = 23×2
0.92 98 2.04 95.64 1.32 97 4.31 94.34 3.26 87.25 0.012 Inf -0.804 104.98 -0.804 104.98 -0.804 104.98 3.7647 91.21
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
avePower = data(:, 1);
aveEfficiency = data(:, 2);
% Plot original data.
subplot(1, 2, 1);
scatter(avePower, aveEfficiency, 'Filled');
grid on;
title('Scatter Plot', 'FontSize', fontSize);
xlabel('Average Power', 'FontSize', fontSize);
ylabel('Average Efficiency', 'FontSize', fontSize);
% Plot histogram
subplot(1, 2, 2);
histogram(avePower, 'BinEdges', -1:4)
grid on;
title('Histogram', 'FontSize', fontSize);
xlabel('Average Power', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
  2 个评论
Mahnoor
Mahnoor 2024-10-10
Hello @Image Analyst, thankyou so much for this. I have tried running this and changing the variable names accordingly. The table from the Excel sheet displays in the Command Window. But for some reason none of the plots pop up. Not sure what I am doing wrong over here?
Image Analyst
Image Analyst 2024-10-10
I don't know. It brings up a figure for me and also here in Answer. Are you sure you don't have a close() call after the code? Can you step through it one line at a time and see when the figure comes up and when it gets closed? It's not just hidden in the background behind all your other windows is it? Are you using Windows? If so, if you hover the mouse over MATLAB icon on the taskbar, do you see multiple little MATLAB pictures, one of them being your plot window?

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by