How to plot a histogram as a curve?

494 views (last 30 days)
I have a histogram that I want to plot just as a simple line plot. I have tried:
h = histogram(speed,'Normalization','probability')
plot(h)
But then I got an error message: Not enough input arguments.
Does anyone know how I can do it? Many thanks!

Accepted Answer

Xun Jing Toh
Xun Jing Toh on 14 Mar 2019
[N,edges] = histcounts(X, 'Normalization','pdf');
edges = edges(2:end) - (edges(2)-edges(1))/2;
plot(edges, N);
Personally I just do a simple calculation from histcounts

More Answers (3)

Steven Lord
Steven Lord on 30 Oct 2018
The histogram function itself creates a graphics object. The output argument from that call is a handle to that graphics object, not data that you can pass into plot.
If you want it to be a line (just the top of the bars) set the DisplayStyle property of the histogram object to 'stairs'.
h = histogram(speed,'Normalization','probability', 'DisplayStyle', 'stairs');
If you just want to connect the midpoints of the top edges of the bars, there is a way to use the data in the histogram (or returned from the histcounts function) to generate the data to plot. Average adjacent elements in the bin edges vector to identify the bin centers, then plot using those centers with the counts for the corresponding bin.
  2 Comments
Trey Roy
Trey Roy on 28 Oct 2020
Hi,
I am facing a similar issue in switching from hist to histogram to try and upgrade my code. The original code I used for our graph was in the following format:
axes(h_axes(2,2));
set(gca,'FontSize',14)
sessionSummary.ipsiMT = logData.MT(completeTargetIpsi);
sessionSummary.contraMT = logData.MT(completeTargetContra);
sessionSummary.allMT = logData.MT(sessionSummary.complete);
ipsiHist = hist(sessionSummary.ipsiMT, MTbins);
contraHist = hist(sessionSummary.contraMT, MTbins);
allHist = hist(sessionSummary.allMT, MTbins);
plot(MTbins, ipsiHist, 'color', 'm');
hold on
plot(MTbins, contraHist, 'color', 'b');
plot(MTbins, allHist, 'color', 'k');
title('completed MT','fontsize',fontFigure);
h_leg = legend('Ipsi','Contra','All','Location','NorthEast');
set(h_leg, 'fontsize',10);
This chunk of code was effective at creating a histogram in which the center of the bins were plotted as a curve and the actual histogram was not included in the graph. I am attempting to replicate this exact graph using the histogram function instead, but am having trouble. I replaced the middle chunk of code with:
ipsiHist = histogram(sessionSummary.ipsiRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'm');
contraHist = histogram(sessionSummary.contraRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'b');
allHist = histogram(sessionSummary.allRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'k');
This resulted in a graph that traced the top edges of the histogram in a way that appeared like stairs but did not give the curve result I am aiming for. I also attempted to use the 'normalization' and 'probability' functions, however, when I did this it was not generating any graph. Please let me know if you have any ideas or recommendations for achieving my desired result while using the histogram function.
Steven Lord
Steven Lord on 28 Oct 2020
You mean something like this? This sample code will create three figures: one with the histogram, one with the curve, and one with both.
% Sample data
x = randn(1, 1e4);
% Plot the histogram alone
figure
h = histogram(x, 'Normalization', 'probability');
% Plot the curve alone
%
% I could have retrieved values and edges from h (they are stored in the
% Values and BinEdges properties respectively) but I wanted to show how
% to get this information without actually creating a plot
[values, edges] = histcounts(x, 'Normalization', 'probability');
centers = (edges(1:end-1)+edges(2:end))/2;
figure
plot(centers, values, 'k-')
% Plot both, line superimposed on the histogram
figure
h = histogram(x, 'Normalization', 'probability');
hold on
plot(centers, values, 'k-')

Sign in to comment.


KSSV
KSSV on 26 Oct 2018
data = randn(1,1000); %// example data
num_bars = 15; %// specify number of bars
[n, x] = hist(data,num_bars); %// use two-output version of hist to get values
n_normalized = n/numel(data)/(x(2)-x(1)); %// normalize to unit area
bar(x, n_normalized, 1); %// plot histogram (with unit-width bars)
hold on
plot(x, n_normalized, 'r'); %// plot line, in red (or change color)
  2 Comments
KSSV
KSSV on 30 Oct 2018
What bug? What problem you are facing?

Sign in to comment.


TANMOY SINGHA
TANMOY SINGHA on 28 May 2020
data = randn(1,1000); %// example data
num_bars = 15; %// specify number of bars
[n, x] = hist(data,num_bars); %// use two-output version of hist to get values
n_normalized = n/numel(data)/(x(2)-x(1)); %// normalize to unit area
bar(x, n_normalized, 1); %// plot histogram (with unit-width bars)
hold on
plot(x, n_normalized, 'r'); %// plot line, in red (or change color)
  1 Comment
Champago
Champago on 6 Aug 2020
with histfit? with transparency set to 0 and edgecolor to white you will get only the line
figure(1);clf;
h=histfit(data);
h(1).EdgeColor=[1,1,1];
alpha(h,0);

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!