0

With matplotlib-style figures, I'm aware that using:

import matplotlib.pyplot
plt.ticklabel_format()

allows you to set the axis labels into scientific notation or plain style (to suppress scientific notation). This can also be used in some seaborn plots like scatterplot or lineplot:

import numpy as np
import pandas as pd
import seaborn as sns
a = pd.DataFrame({"Time":np.repeat(np.arange(1e4,5.1e4,1e4),100),
                   "Speed": np.random.randn(500)})

plt.ticklabel_format(axis="x", style="sci", scilimits=(4,4))
result = sns.scatterplot(x = "Time",y = 'Speed',data = a)
plt.show()

Matplotlib-Scientific Notation Style

plt.ticklabel_format(axis="x", style="sci", scilimits=(4,4))
result = sns.lineplot(x = "Time",y = 'Speed',data = a)
plt.show()

enter image description here

However, for seaborn plots that generate FacetGrids like catplot, setting plt.ticklabel_format is ignored, especially on the categorial axis:

plt.ticklabel_format(axis="x", style="sci", scilimits=(4,4))
result = sns.catplot(x = "Time",y = 'Speed',data = a)
plt.show()

FacetGrid no Scientific Notation

A temporary workaround to this issue involves displaying each number in the categorical variable (Time) as scientific notation as such:

a = pd.DataFrame({"Time": ["{:.0e}".format(i) for i in np.repeat(np.arange(1e4,5.1e4,1e4),100)],
                    "Speed": np.random.randn(500)})

plt.ticklabel_format(axis="x", style="sci", scilimits=(4,4))
result = sns.catplot(x = "Time",y = 'Speed',data = a)
plt.show()

FacetGrid with Scientific Notation

From my understanding, this is because FacetGrids don't share some attributes with the matplotlib-style plots (like ticklabel_format) or matplotlib doesn't allow scientific notation display on "categorial variables"; likewise, the notation style for this is different from that of the scatter plot. In this case, the exponent is part of each x-value and the base is omitted whereas the scatterplot displays a singular base and exponent for all x-values.

Is there a way to get the FacetGrid from catplot to have the same scientific notation style as the one on the scatter plots?

superasiantomtom95
  • 521
  • 1
  • 7
  • 25
  • 1
    `p = sns.relplot(kind='scatter', x = "Time",y = 'Speed', data = a); for ax in p.axes.flatten(): ax.ticklabel_format(style='scientific', scilimits=(0,0), axis='x')` works where the axis is continuous. – Trenton McKinney Nov 15 '21 at 20:02
  • 1
    However, for plots with discrete (0 indexed) axis ticks (e.g. barplot, stripplot, etc) you must format the labels. For `p = sns.catplot(x = "Time",y = 'Speed', data = a)` see `for ax in p.axes.flatten(): print(ax.get_xticklabels())` which is `[Text(0, 0, '10000.0'), Text(1, 0, '20000.0'), Text(2, 0, '30000.0'), Text(3, 0, '40000.0'), Text(4, 0, '50000.0')]` – Trenton McKinney Nov 15 '21 at 20:08
  • Ok, so I modified each individual label with `for ax in result.axes.flatten(): for i in range(len(ax.get_xticklabels())): ax.get_xticklabels()[i]._text = float(ax.get_xticklabels()[i]._text)/1e4` but it seems that these changes aren't carried over to when the figure actually gets plotted. Am I doing something wrong? – superasiantomtom95 Nov 16 '21 at 20:47
  • 1
    The only thing different here is accessing each `axes` from the FacetGrid subplots (e.g. `for ax in result.axes.flatten():` all other steps are exactly the same as standard matplotlib, since seaborn is just an API for matplotlib. (1) `get_xticklabels`, (2) extract the text and format, (3) `set_xticks`, (4) `set_xticklabels` (5) format the margin offset_value – Trenton McKinney Nov 16 '21 at 21:51
  • 1
    Scratch step (5), `ax.ticklabel_format(useOffset=1e4, axis='x')` only works with a continuous axis, as previously stated. For the xaxis you would have to add something like `'Time(1e4)'` as the xlabel. You can test it against the yaxis with `ax.ticklabel_format(useOffset=1e4, axis='y')`. [See Plot and Code](https://i.stack.imgur.com/nULAU.png) and `ScalarFormatter` doesn't work as one would expect -> see [this code and plot](https://i.stack.imgur.com/zVU4w.png) – Trenton McKinney Nov 16 '21 at 22:40

0 Answers0