如何在 Seaborn 中使用 Facet Grid 用逗号分隔数千个 [英] How to separate thousands with commas using Facet Grid in Seaborn
问题描述
我有以下代码:
d = sns.FacetGrid(data = df,
col = 'Company',
sharex = False,
sharey = False,
col_wrap = 4)
d.map(sns.distplot, 'Volume', kde = False, rug = True, fit = stats.norm)
d.set_xlabels('volume')
d.set_xticklabels(rotation = 45)
plt.savefig(myDataRepository + 'figure_02__' + str(time_stamp) + '.png')
ax.get_xaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
最后两行尝试用逗号分隔 x 轴值.例如,500,000"而不是500000".
The last two lines attempt to separate the x-axis values with commas. For example, '500,000' instead of '500000'.
我看到一条错误消息说:
I see an error message saying:
"global name ax is not defined"
我将如何修改 ax.
以便我可以用逗号分隔格式化 x 轴值?
How would I amend ax.
such that I can format the x-axis values with comma separation?
提前致谢!
---在下面编辑---
这是我发布的更新代码.它仍然失败,但这次在评估
Here is updated code that I'm posting. It still fails, but this time with error message @property
when evaluating
d.ax.get_xaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))
修订代码:
fig, ax = plt.subplots()
d = sns.FacetGrid(data = df,
col = 'Company',
sharex = False,
sharey = False,
col_wrap = 4)
d.map(sns.distplot, 'Volume', kde = False, rug = True, fit = stats.norm)
d.set_xlabels('volume')
d.set_xticklabels(rotation = 45)
plt.savefig(myDataRepository + 'figure_02__' + str(time_stamp) + '.png')
d.ax.get_xaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))
@cphlewis
建议我将 ax.get_axis() 放在 Facet Grid 中.但是怎么样???
@cphlewis
is suggesting that I put the ax.get_axis() inside the Facet Grid. But how???
将 ax.get_axis()
放入 FacetGrid 似乎并没有解决这个问题(假设我这样做是正确的).在 Seaborn FacetGrid 中使用这个 ax.get_axis()
函数是否可能?
Putting ax.get_axis()
inside the FacetGrid does not seem to solve this problem (assuming I did so correctly). Is it even possible to use this ax.get_axis()
function with the Seaborn FacetGrid?
错误信息:
971 return self.axes[0, 0]
972 else:
--> 973 raise AttributeError
974
975 @property
AttributeError:
推荐答案
我也遇到了这个问题,并通过添加一个简单的循环来用逗号格式化所有轴,在这里想出了一个干净的解决方案.
I was having this problem too and came up with a clean solution here by adding a simple loop to format all of the axes with the comma.
# set up the standard plotting of the df
g = sns.PairGrid(wmix, vars = ['ambulatory', 'wheelchair', 'stretcher'], hue="holiday")
g.map_upper(plt.scatter)
g.map_lower(sns.kdeplot)
g.map_diag(sns.kdeplot, lw=2, legend=False)
g.add_legend()
# now add the comma and remove decimal formats for each y axis
for ax in g.axes[:,0]:
ax.get_yaxis().set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}'))
# this adds the axis labels to each plot
plt.subplots_adjust(top=0.9)
g.fig.suptitle('Ride Count Relationships');
这篇关于如何在 Seaborn 中使用 Facet Grid 用逗号分隔数千个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!