简体   繁体   中英

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

Dataset:

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

When I import the data into a Jupyter notebook, the numbers in the gdpMillion column become scientific notation. How to change them back to normal? And when I draw the line chart, I would like to have the CountryName at the end of each line.

Here is the code of my lineplot

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()

First, I'll consider that your data is available in a CSV-like file. My solution includes using the pandas built-in function set_option to account for proper number formating and then using pyplot.text for plotting the names at the end of each line.

I don't think it's the best solution in terms of visualization but, if I understood well what you're looking for, here it goes:

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')

first solution

Instead, I'd seek for something like:

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.)

second solution

I hope it helped you. Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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