Seaborn jointplot axis on log scale with kind="hex"

Question:

I’d like to show the chart below, but with the x-axis on a log scale.

df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('XY'))
sns.jointplot(data=df,x='X',y='Y',height=3,kind='hex')

enter image description here

To be clear, I don’t want to log X first, rather I want the numbers to stay the same but the distance between the axis ticks to change. In altair, it would look like the following (I can’t do hex in altair, although please correct me if I’m wrong on that):

enter image description here

EDIT: Matt suggested adding xscale="log". That gets me very nearly there. I just need a way to from powers to normal integers.

enter image description here

Asked By: Justin Beresford

||

Answers:

You can use the xscale="log" keyword argument, which gets passed to the Matplotlib hexbin function that is used under-the-hood by seaborn. E.g.,

sns.jointplot(data=df, x='X', y='Y' ,height=3, kind='hex')

As stated in the comments, there are various ways to set the axis tick labels to not be in scientific format. The simplest is to do:

import matplotlib.ticker as mticker

grid = sns.jointplot(data=df, x='X', y='Y', height=3, kind='hex', xscale="log")
grid.ax_joint.xaxis.set_major_formatter(mticker.ScalarFormatter())

If instead you want, e.g., 1000 to be formatted with a comma so that it is 1,000, you could instead do:

grid.ax_joint.xaxis.set_major_formatter(mticker.StrMethodFormatter("{x:,.0f}"))
Answered By: Matt Pitkin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.