I'm trying to plot a set of histograms for a dataframe with 25 columns named "Feature_1","Feature_2",...."Feature_25"
. When I use df.hist()
it sorts individual histograms by their names so they are plotted in the following order: "Feature_1",""Feature_10","Feature_11"..."Feature_2","Feature_20",...
which is not what I need.
How do I change the sorting order? Passing columns parameter doesn't change anything.
I solved the problem by using matplotlib directly but that's not what I would prefer to do each time I need to plot several histograms.
In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.
Bins are the buckets that your histogram will be grouped by. On the back end, Pandas will group your data into bins, or buckets. Then pandas will count how many values fell into that bucket, and plot the result.
Make two dataframes, df1 and df2, of two-dimensional, size-mutable, potentially heterogeneous tabular data. Create a figure and a set of subplots. Make a histogram of the DataFrame's, df1 and df2. To display the figure, use show() method.
You could instead make repeated calls to hist with individual columns. Not sure if that fits all of your needs.
import pandas as pd
df = pd.DataFrame({'a':[1,1,1,1,3],
'b':[1,1,2,1,3],
'c':[2,2,2,1,3],
})
df[['c']].hist()
df[['a']].hist()
df[['b']].hist()
Just iterate through the list of the columns in the order you would like
my_list = ['c','a','b']
for each in my_list:
df[[each]].hist()
Try making the columns index categorical, then plotting with sort_columns True.
import pandas as pd
df = pd.DataFrame({'a':[1,2], 'b':[30,10]})
df.columns = pd.CategoricalIndex(['b', 'a'], ordered=True)
df.plot.hist(sort_columns=True, subplots=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With