【问题标题】:customizing facetgrid xticks and labels in seaborn在 seaborn 中自定义 facetgrid xticks 和标签
【发布时间】:2016-11-23 14:26:30
【问题描述】:

如何在 seaborn 的 FacetGrid 中自定义 ticklabels 的属性?在下面的示例中,我只想更改 x 轴刻度标签的字体大小:

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)

我的解决方案遍历FacetGrid 对象的axes 数组,这似乎不必要地复杂。我知道fontscale 可以全局设置,但我只想更改 x 轴刻度标签字体。有一个更好的方法吗?还有没有办法将标签的样式自定义为科学记数法?

【问题讨论】:

    标签: python numpy matplotlib seaborn


    【解决方案1】:

    您可以使用flatten 方法简化循环。我还添加了一种以科学记数法格式化的方法。 .0e 表示科学记数法,小数点为 0。

    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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-29
      • 2018-02-01
      • 2020-07-29
      • 2015-07-10
      • 1970-01-01
      • 2016-03-29
      • 2022-01-12
      • 2020-04-10
      相关资源
      最近更新 更多