Reputation: 1199
how can the properties of ticklabels be customized inside a FacetGrid
in seaborn? in the example below i wanted to change the fontsize of only the x-axis ticklabels:
import matplotlib.pylab as plt
import seaborn as sns
df1 = pandas.DataFrame({"x": np.random.rand(100),
"y": np.random.rand(100)})
df1["row"] = "A"
df1["col"] = "1"
df2 = pandas.DataFrame({"x": np.random.rand(100),
"y": np.random.rand(100) + 1})
df2["row"] = "A"
df2["col"] = "2"
df = pandas.concat([df1, df2])
g = sns.FacetGrid(df, row="row", col="col", hue="col")
g.map(plt.scatter, "x", "y")
print g.axes
row, col = g.axes.shape
for i in range(row):
for j in range(col):
ax = g.axes[i, j]
print "ax: ", ax.get_xticklabels()
ax.set_xticklabels(ax.get_xticklabels(), fontsize=6)
my solution iterates through the axes
array of the FacetGrid
object which seems unnecessarily complicated. i know fontscale
can be set globally but i only want to change the x-axis ticklabel fonts. is there a better way to do this? and is there also a way to customize the style of labels to be scientific notation?
Upvotes: 1
Views: 5008
Reputation: 426
You can simplify the loop by using the flatten
method. I have also added a way to format in scientific notation. .0e
means scientific notation with 0 decimals.
import matplotlib.ticker as tkr
for ax in g.axes.flatten():
ax.set_xticklabels(ax.get_xticklabels(), fontsize=6)
ax.xaxis.set_major_formatter(
tkr.FuncFormatter(lambda x, p: "{:.0e}".format(x)))
Upvotes: 4