Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new in matplotlib. I have data binned data.

Python
Level              Quantity
    0      (199.533, 271.74]  (10.213, 39.4]
    1      (199.533, 271.74]  (10.213, 39.4]
    2      (54.903, 127.327]  (10.213, 39.4]
    3     (127.327, 199.533]  (10.213, 39.4]
    4     (127.327, 199.533]  (10.213, 39.4]
    ...                  ...             ...
    5105   (54.903, 127.327]  (10.213, 39.4]
    5106   (54.903, 127.327]    (39.4, 68.5]
    5107   (54.903, 127.327]  (10.213, 39.4]
    5108  (127.327, 199.533]  (10.213, 39.4]
    5109   (54.903, 127.327]  (10.213, 39.4]

I want to make 2 histogram graph named "Level" and "Quantity" of each bin of "Equal width".I have tried this

Python
import pandas as pd
import matplotlib.pyplot as plt

# Sample binned data for the two variables (Level and Quantity)
data = {
    'Level': ['(199.533, 271.74]', '(199.533, 271.74]', '(54.903, 127.327]', '(127.327, 199.533]', '(127.327, 199.533]'],......
    'Quantity': ['(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]'],......
}

df_binned = pd.DataFrame(data)


plt.hist([df_binned['Level'], df_binned['Quantity']], bins=10, edgecolor='black', alpha=0.7, label=df_binned.columns)
plt.legend()
plt.title('Stacked Histograms of Binned Variables')
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()


What I have tried:

It is not reading the bins in plt.hist() function

Python
df_binned['Level']

df_binned['Quantity']

How i cann plot binned data?
How to prepare the data for making histogram graph?
Posted
Updated 21-Jul-23 5:57am
Comments
Member 8840306 20-Jul-23 0:51am    
"Histogram work on raw data points, not binned data" .Now the question is how should we prepare binned data for histogram ? –

"Binned data" would only chart if you include "counts".

Binned data are numbers converted to text categories; which don't chart in themselvs. e.g. what are the "height ranges" (short, average, tall).

How many of each? (i.e. how many "data points" fell within each range.) That's what you can chart.
 
Share this answer
 
Comments
Member 8840306 21-Jul-23 1:09am    
Kindly provide the code snippet to solve this situation?
[no name] 21-Jul-23 12:34pm    
Your response indicates you still don't understand what "binning" is. How is anybody supposed to know what your numbers represent? Ages? Weights? Heights? You say you have "binned data" ... I'm telling you, you don't.
Here we need data preparation

Python
df_binned = pd.DataFrame(data)
df_binned['Level']   = df_binned['Level'].dtype(str)
df_binned['Quantity']=df_binned['Quantity'].dtype(str)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900