python - 如何在 Pandas 中抑制科学记数法并在折线图末尾添加标签?
问题描述
数据集:
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内置函数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.)
我希望它对你有帮助!问候。
推荐阅读
- assembly - 缓冲区溢出链接
- javascript - 如何将 JSON 转换为 multipart/form-data
- reactjs - 如何解决 gatsby js 中的依赖错误?
- c# - Polly 同步重试
- javascript - 状态的功能被另一个状态破坏了?
- javascript - 有没有办法使用反应将此字典列出到前端(显示在浏览器中)?
- python - 抓取超时。:在 pypylon usb 相机帧抓取中抛出 TimeoutException(文件'InstantCameraImpl.h',第 1064 行)
- arrays - 切换数组元素的问题
- yaml - 将逗号分隔的字符串转换为 yaml 中的列表
- css - 如何修复网页的移动可读性?