pandasnumpymatplotlibboxplotfrequency-table

How to create a box plot from a frequency table


In the table below, I have values and frequencies. I'd like to draw a box-plot using Jupyter Notebook. I googled it but not able to find any answers.

My idea is to create a column, 2,2,2,2,4,4,4,4,4,4,4,... But I think there must be a better way.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

value=np.array([2,4,6,7,10])
freq=np.array([4,7,8,5,2])

# do something here

plt.boxplot(newdata)
plt.show()

enter image description here


Solution

  • use numpy's repeat:

    newdata = np.repeat(value,freq)