Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change order of plots in pandas hist command

Tags:

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.

like image 417
Dennis Sakva Avatar asked Nov 03 '15 17:11

Dennis Sakva


People also ask

How do you plot a histogram in pandas?

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.

What is bins in pandas histogram?

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.

How do you plot multiple histograms in Python?

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.


Video Answer


2 Answers

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()
like image 81
James Natale Avatar answered Sep 30 '22 20:09

James Natale


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)
like image 29
abevieiramota Avatar answered Sep 30 '22 20:09

abevieiramota