2016-11-23 65 views
0

如何在海寶中的FacetGrid內定製蜱標籤的屬性?在下面的例子中我想僅改變x軸ticklabels的字體大小:通過axes陣列FacetGrid對象這似乎不必要的複雜的在seaborn中定製facetgrid xticks和標籤

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) 

我的溶液進行迭代。我知道fontscale可以在全局設置,但我只想更改x軸ticklabel字體。有一個更好的方法嗎?還有一種方法可以將標籤樣式自定義爲科學記數法?

回答

0

您可以通過使用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))) 
相關問題