Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to plot a histogram using Matplotlib in Python with a list of data?

How to plot a histogram using Matplotlib in Python with a list of data?

October 8, 2021 by James Palmer

If you want a histogram, you don’t need to attach any ‘names’ to x-values, as on x-axis you would have data bins:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

np.random.seed(42)
x = np.random.normal(size=1000)

plt.hist(x, density=True, bins=30) # density=False would make counts
plt.ylabel(‘Probability’)
plt.xlabel(‘Data’);

Note, the number of bins=30 was chosen arbitrarily, and there is Freedman–Diaconis rule to be more scientific in choosing the “right” bin width:

, where IQR is Interquartile range and n is total number of datapoints to plot

So, according to this rule one may calculate number of bins as:
q25, q75 = np.percentile(x,[.25,.75])
bin_width = 2*(q75 – q25)*len(x)**(-1/3)
bins = round((x.max() – x.min())/bin_width)
print(“Freedman–Diaconis number of bins:”, bins)
plt.hist(x, bins = bins);

Freedman–Diaconis number of bins: 82

And finally you can make your histogram a bit fancier with PDF line, titles, and legend:
import scipy.stats as st

plt.hist(x, density=True, bins=82, label=”Data”)
mn, mx = plt.xlim()
plt.xlim(mn, mx)
kde_xs = np.linspace(mn, mx, 300)
kde = st.gaussian_kde(x)
plt.plot(kde_xs, kde.pdf(kde_xs), label=”PDF”)
plt.legend(loc=”upper left”)
plt.ylabel(‘Probability’)
plt.xlabel(‘Data’)
plt.title(“Histogram”);

However, if you have limited number of data points, like in OP, a bar plot would make more sense to represent your data. Then you may attach labels to x-axis:
x = np.arange(3)
plt.bar(x, height=[1,2,3])
plt.xticks(x, [‘a’,’b’,’c’])

If you haven’t installed matplotlib yet just try the command.
> pip install matplotlib

Library import
import matplotlib.pyplot as plot

The histogram data:
plot.hist(weightList,density=1, bins=20)
plot.axis([50, 110, 0, 0.06])
#axis([xmin,xmax,ymin,ymax])
plot.xlabel(‘Weight’)
plot.ylabel(‘Probability’)

Display histogram
plot.show()

And the output is like :

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2023 · News Pro Theme on Genesis Framework · WordPress · Log in