繁体   English   中英

如何抑制 Pandas 中的科学计数法并在折线图末尾添加 label?

[英]How to suppress the scientific notation in Pandas and add the label at the end of the line chart?

数据集:

Year        Country gdpMillion
1980-01-01  Canada  273854
1980-01-01  China   191149
1980-01-01  United Kingdom  564948
1980-01-01  India   186325
1980-01-01  Japan   1105390
1980-01-01  Singapore   11896.25678
1980-01-01  Thailand    32353.44073
1980-01-01  United States   2857310
1981-01-01  Canada  306215
1981-01-01  China   195866
1981-01-01  United Kingdom  540766
1981-01-01  India   193491
1981-01-01  Japan   1218990
1981-01-01  Singapore   14175.22884
1981-01-01  Thailand    34846.10786
1981-01-01  United States   3207040
1982-01-01  Canada  313507
1982-01-01  China   205090
1982-01-01  United Kingdom  515049
1982-01-01  India   200715
1982-01-01  Japan   1134520
1982-01-01  Singapore   16084.25238
1982-01-01  Thailand    36589.79786
1982-01-01  United States   3343790
1983-01-01  Canada  340548
1983-01-01  China   230687
1983-01-01  United Kingdom  489618
1983-01-01  India   218262
1983-01-01  Japan   1243320
1983-01-01  Singapore   17784.11215
1983-01-01  Thailand    40042.82624
1983-01-01  United States   3634040
1984-01-01  Canada  355373
1984-01-01  China   259947
1984-01-01  United Kingdom  461487
1984-01-01  India   212158
1984-01-01  Japan   1318380
1984-01-01  Singapore   19749.3611
1984-01-01  Thailand    41797.59296
1984-01-01  United States   4037610
1985-01-01  Canada  364756
1985-01-01  China   309488
1985-01-01  United Kingdom  489285
1985-01-01  India   232512
1985-01-01  Japan   1398890
1985-01-01  Singapore   19156.53275
1985-01-01  Thailand    38900.69271
1985-01-01  United States   4338980

当我将数据导入 Jupyter 笔记本时,gdpMillion 列中的数字变为科学计数法。 如何让它们恢复正常? 当我绘制折线图时,我希望在每条线的末尾都有 CountryName。

这是我的线图的代码

import seaborn as sns

sns.lineplot(x='Year', y='gdpMillion', hue='Country', data=dataset_C,
        marker="o", palette="Blues")
sns.despine(left=True, bottom=True)
plt.show()

首先,我会考虑您的数据在类似 CSV 的文件中可用。 我的解决方案包括使用pandas内置 function set_option来说明正确的数字格式,然后使用pyplot.text在每行末尾绘制名称。

我认为这不是可视化方面的最佳解决方案,但是,如果我能很好地理解您在寻找什么,那就是:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('darkgrid')

pd.set_option('display.float_format', lambda x: '%.3f' % x)
dataset_C = pd.read_csv('dataset_C.csv')
dataset_C['YYYY'] = dataset_C['Year'].apply(pd.to_datetime).apply(lambda x: x.year)

fig, ax = plt.subplots(1,1,figsize=(10,6))
sns.lineplot(x='YYYY',y='gdpMillion',hue='Country', data=dataset_C, marker='o',palette='Blues', ax=ax, legend=False)
for country in dataset_C['Country'].unique():
    xpos = dataset_C[dataset_C['Country'] == country].YYYY.max() - 0.2
    ypos = dataset_C[dataset_C['Country'] == country].gdpMillion.max() + 100000
    plt.text(xpos,ypos,country)
ax.set_xlabel('Year')

第一个解决方案

相反,我会寻找类似的东西:

fig, ax = plt.subplots(1,1,figsize=(10,6))
sns.lineplot(x='YYYY',y='gdpMillion',hue='Country', data=dataset_C, marker='o',palette='deep', ax=ax, legend='full')
ax.set_xlabel('Year')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

第二种解决方案

我希望它对你有所帮助。 问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM