Help with Histogram Plot
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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)")
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)
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 个评论
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 Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!