Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ticks after log scale in Seaborn FacetGrid?

I'm trying to plot a FacetGrid consisting of histograms with a log transformation on the y axis. Im unable to set the yticks to a more readable format.

ticks = [0.1, 0.3, 1, 3, 10, 30, 100, 300, 1000, 3000, 10000]
labels = [i for i in ticks]
grid = sns.FacetGrid(data = df, col = 'LoanStatus', col_wrap = 4)
grid.map(plt.hist, 'credit_score_range_average', bins = 20).set(yscale ='log', yticks = (ticks, labels))

I receive a Value Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). enter image description here

enter image description here

Setting yticks = (ticks, labels) is how I achieve this in matplotlib.

while writing this and trying some last options, I went with a sns.distplot and found it to be much better option for my case. I would still appreciate any insight in this issue

like image 340
stephen barter Avatar asked Jan 11 '19 17:01

stephen barter


People also ask

How do you set Xticks in Seaborn?

set_xticks() and Axes. set_yticks() functions in axes module of matplotlib library are used to Set the ticks with a list of ticks on X-axis and Y-axis respectively. Parameters: ticks: This parameter is the list of x-axis/y-axis tick locations.

What does FacetGrid do in Seaborn?

FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is Col_wrap?

col_wrapint. “Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet. share{x,y}bool, 'col', or 'row' optional. If true, the facets will share y axes across columns and/or x axes across rows.


1 Answers

I've finally found this solution to work. Strangely you must call grid.set() again on a new line. Hope this saves someone some time. You can NOT include the yscale argument in this line either or else it will fail.

ticks = [0.1, 0.3, 1, 3, 10, 30, 100, 300, 1000, 3000, 10000, 30000]
labels = [i for i in ticks]
grid = sns.FacetGrid(data = df, col = 'LoanStatus', col_wrap = 4)
grid.map(plt.hist, 'credit_score_range_average').set(yscale = 'log')
grid.set(yticks = ticks, yticklabels = labels)

enter image description here

like image 50
stephen barter Avatar answered Nov 14 '22 21:11

stephen barter