Home > Software engineering >  plotting very long scientific numbers in python with pandas?
plotting very long scientific numbers in python with pandas?

Time:12-01

I need to manipulate very long scientific numbers in python and the pandas dataframe format seems convenient... except I can't plot. For instance, the following baby code:

import pandas as pd
import matplotlib.pyplot as plt
import decimal as dec
obs={}
dec.getcontext().prec=10
obs['x']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
obs['y']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
df=pd.DataFrame(data=obs)
df.plot(x='x',y='y',marker='*')
plt.show()

gives: TypeError: no numeric data to plot. How should I get my plot ?

CodePudding user response:

df=df.astype(float)
df.plot(x='x', y='y', marker='*')

CodePudding user response:

decimal.decimal is an object not a numeric value, you have to do the conversion before plotting!

import pandas as pd
import matplotlib.pyplot as plt
import decimal as dec
obs={}
dec.getcontext().prec=10
obs['x']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
obs['y']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
df=pd.DataFrame(obs)
print(df.dtypes)
df['x'] = df['x'].astype(int)
df['y'] = df['y'].astype(int)
print(df.dtypes)
df.plot(x ='x',y='y',marker='*')
plt.show()

Outpout

x    object
y    object
dtype: object


x    int32
y    int32
dtype: object

Plot

  • Related