Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D graph
#1
Hello everyone ,
Well I'm new in Python , I have to submit an homework at university ,I'm in finance field ,it's about creating a volatility surface , so I want to use python to create it , the problem is because I don't know what's wrong in my code :


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns


fig = plt.figure()
ax = fig.gca(projection='3d')
sur = ax.plot_surface(donnees['Time'] ,donnees['IV'],donnees['Money']) 

plt.show()


then
Error:
KeyError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Time' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-89-f6fcdf9f14c3> in <module> 1 fig = plt.figure() 2 ax = fig.gca(projection='3d') ----> 3 sur = ax.plot_surface(donnees['Time'] ,donnees['IV'],donnees['Money']) 4 5 plt.show() ~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2925 if self.columns.nlevels > 1: 2926 return self._getitem_multilevel(key) -> 2927 indexer = self.columns.get_loc(key) 2928 if is_integer(indexer): 2929 indexer = [indexer] ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2657 return self._engine.get_loc(key) 2658 except KeyError: -> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2661 if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Time'
i arlready converted my datas in float
please help
Reply
#2
I observe when the column name case mismatch, that kind of error was raised, thus i used to make all columns to lower using lower function and feeded all in lower. Also, use with plot_trisurf.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

donnees = pd.read_csv('book3.csv', sep=",", index_col=False)
blankIndex=[''] * len(donnees)
donnees.index=blankIndex
donnees.columns = donnees.columns.str.lower() 

fig = plt.figure()
ax = fig.gca(projection='3d')

#fails as we already lowered the col names:  sur = ax.plot_surface(donnees['Time'] ,donnees['IV'],donnees['Money']) 
#success: sur = ax.plot_trisurf(donnees['time'] ,donnees['iv'],donnees['money'])  
sur = ax.plot_trisurf(donnees['time'] ,donnees['iv'],donnees['money'],linewidth=0.2, antialiased=False) 

plt.show()
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020