Plotting pdf on top of histogram

114 views (last 30 days)
Charith Ranatunga
Charith Ranatunga on 22 Apr 2020
Commented: Tommy on 22 Apr 2020
Hi Im trying to plot a probability density function on top of a histogram I plotted for idle current and idle energy based off a LoRa transmission data. The csv data included time in the first column and current in the second and I found the energy using a given value of 12V. I tried plotting it but got an error, any advice would be great :) .
idle_data = importdata('idlecurrent.csv');
%Lora_t = Lora_0tx(:,1);
idle_time = idle_data(:,1);
idle_current = idle_data(:,2);
idle_timetot = (max(idle_time) - min(idle_time));
idle_energy = idle_current.*12*idle_timetot;
mean_current = mean(idle_current);
stdev_current = std(idle_current);
figure(4)
hist_current = histogram(idle_current);
hold on
current_pdf = histogram(idle_current,'Normalized','pdf');
hold off
figure(5)
hist_energy = histogram(idle_energy);
hold on
energy_pdf = histogram(idle_energy,'Normalized','pdf');
hold off
  1 Comment
Tommy
Tommy on 22 Apr 2020
histogram does not accept 'Normalized' as an argument, but rather 'Normalization':
current_pdf = histogram(idle_current, 'Normalization', 'pdf');
Your code will plot two histograms on top of each other. That may be what you want, but depending on the amount of data the first histogram might tower above the second, making the second essentially invisible. In case you are instead wanting to plot the actual pdf on top of your (normalized) histogram, you can do it if you know the pdf:
A = normrnd(0,1,1000,1);
histogram(A, 'Normalization', 'pdf');
hold on;
ax = gca;
x = linspace(ax.XLim(1), ax.XLim(2), 1000);
plot(x, normpdf(x), 'LineWidth', 2)

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!