[英]Changing order of seaborn lineplot
我有一个小的pd.DataFrame
看起来像这样:
Col1 | 数列 |
---|---|
0 | 10000000 |
1 | 750万 |
2 | 12500000 |
3 | 37500000 |
4 | 110000000 |
5 | 65000000 |
NumCol
实际上是美元值。
我想创建一个seaborn lineplot
,但不是使用创建时髦外观轴的数值,而是显示美元值。
sns.lineplot(data=plot_df, x='Col1', y='NumCol')
正确创建:
但是,我希望轴显示$10,000,000
、 $7,500,000
等。我知道我可以使用创建列的字符串表示
plot_df['NumCol_Str'] = plot_df.NumCol.apply(lambda x: "${:,}".format(x))
这创造了:
Col1 | 数列 | NumCol_Str |
---|---|---|
0 | 10000000 | 10,000,000 美元 |
1 | 750万 | 7,500,000 美元 |
2 | 12500000 | 12,500,000 美元 |
3 | 37500000 | 37,500,000 美元 |
4 | 110000000 | 110,000,000 美元 |
5 | 65000000 | 65,000,000 美元 |
但是,在绘图时,它会更改列的顺序
sns.lineplot(data=plot_df, x='Col1', y='NumCol_Str')
如何正确 plot 折线图,同时在轴上保持新的字符串表示法?
plot_df = pd.DataFrame.from_dict({'Col1': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5},
'NumCol': {0: 10000000,
1: 7500000,
2: 12500000,
3: 37500000,
4: 110000000,
5: 65000000}})
plot_df['NumCol_Str'] = plot_df.NumCol.apply(lambda x : "${:,}".format(x))
sns.lineplot(data=plot_df, x='Col1', y='NumCol_Str')
sns.lineplot(data=plot_df, x='Col1', y='NumCol')
只需 plot 使用数值,然后使用 matplotlib 刻度格式化程序更改轴格式化程序:
import matplotlib.ticker as mtick
ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('${x:,.0f}'))
编辑:或者更简单的@BigBen 指出:
ax.yaxis.set_major_formatter('${x:,.0f}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.