0% found this document useful (0 votes)
4 views

Boxplot,Histogram codes with explanations

Uploaded by

shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Boxplot,Histogram codes with explanations

Uploaded by

shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Boxplot

import matplotlib.pyplot as plt

from matplotlib.pyplot import boxplot, show # Required libraries for boxplot

values = [2, 3, 4, 1, -3.04, 5, 4, 6, 7, 2, 4, 6, 8, 6 , 9, 12, 14, 11, 5, 16] # Data points (unordered)

plt.boxplot(values, vert=True, showfliers=True) # Create a simple boxplot

plt.show() # Display the boxplot

plt.boxplot(values, showfliers=False, vert=False) # Boxplot without outliers and in horizontal view

plt.show() # Display the boxplot

plt.boxplot(values, whis="range", vert=False) # Boxplot considering all data points within range

plt.show() # Display the boxplot

plt.boxplot(values, meanline=True, showmeans=True, vert=True) # Display mean line in the boxplot

plt.show() # Display the boxplot

# To plot multiple boxplots on one plane

import numpy as np

collectn_1 = np.random.normal(100, 10, 200) # Generate random data

collectn_2 = np.random.normal(80, 30, 200)

collectn_3 = np.random.normal(90, 20, 200)

values = [collectn_1, collectn_2, collectn_3] # List of data sets

plt.boxplot(values, showmeans=True, meanline=True) # Display multiple boxplots

plt.show() # Display the boxplots

Histogram Notebook
import matplotlib.pyplot as plt

import numpy as np

x = np.random.normal(170, 10, 250) # Generate random data with normal distribution

plt.hist(x) # Plot histogram

plt.show() # Display the histogram


# Detailed explanation of histogram syntax and parameters

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False,


histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None,
stacked=False, *, data=None, **kwargs)

import numpy as np

import matplotlib.pyplot as plt

x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]

n_bins = 10 # Number of bins for histogram

bin_heights, bins, patches = plt.hist(x, bins=10, facecolor='cyan') # Plot histogram with cyan color

plt.show() # Display the histogram

bin_heights # Check the bin heights

Histogram Notebook Commands

1. import matplotlib.pyplot as plt


o Syntax: import matplotlib.pyplot as plt
2. plt.hist()
o Syntax: plt.hist(data, bins=10, range=None, density=False,
facecolor='cyan')
o Parameters:
 data: Data for which the histogram is plotted.
 bins: Number of bins (divisions of the x-axis).
 range: Lower and upper range of the bins.
 density: Whether to normalize the histogram (True/False).
 facecolor: Color of the bars.
3. np.random.normal()
o Syntax: np.random.normal(loc=170, scale=10, size=250)
o Explanation: Generates random numbers following a normal distribution with
specified mean and standard deviation.
4. plt.show()
o Syntax: p lt.show()
o Explanation: Displays the current plot on the screen.
5. Accessing Bin Heights
o Syntax: bin_heights, bins, patches = plt.hist(data, bins=10)
o Explanation: Captures information about the histogram, such as the heights of
the bins and their boundaries.

You might also like