[英]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.