Advertisement
mahmoodn

test.py

Nov 21st, 2021
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import pandas as pd
  2. import csv,sys
  3. import matplotlib
  4. import matplotlib.pyplot as plt
  5.  
  6. print("Reading file... ")
  7. df = pd.read_csv('test.batch.csv')
  8. #print(df)
  9.  
  10. # Creating a dictionary
  11. data = {'Value':[0,0,0]}
  12. kernel_df = pd.DataFrame(data, index=['M1','M2','M3'])
  13. my_dict = {'dummy':kernel_df}
  14. my_dict2 = {'dummy':kernel_df}
  15. # dummy -> Value
  16. # M1 0
  17. # M2 0
  18. # M3 0
  19.  
  20. for name, df_group in df.groupby('Name'):
  21. my_dict[name] = pd.concat(
  22. [g.reset_index(drop=True) for _, g in df_group.groupby('ID')['Value']],
  23. axis=1
  24. )
  25.  
  26.  
  27.  
  28. def plot_dataframe(df, cnt, axes):
  29. df.columns = range(1, len(df.columns)+1) # Ignore the column header
  30. row = df.iloc[0].astype(int) # First row in the dataframe
  31. plt.subplot(2, 1, 1)
  32. print("axes=", axes)
  33. print("axes[0]=", axes[0])
  34. print("cnt=", cnt)
  35. print("row=", row)
  36. ax1 = row.plot(label=cnt, ax=axes[0], marker='o') # Line chart
  37. ax1.set_ylabel( 'test', fontsize=15 )
  38. plt.subplot(2, 1, 2)
  39. df2 = row.value_counts()
  40. df2.reindex().plot(kind='bar', label=cnt, ax=axes[1]) # Histogram
  41.  
  42.  
  43. def plot_kernels(mydict2):
  44. print("New dictionary for plot = ", my_dict2)
  45. fig,axes = plt.subplots(2,1, figsize=(20, 15))
  46. cnt=1
  47. for key in my_dict2:
  48. if key == 'dummy': # skip the dummy key
  49. continue
  50. df = my_dict2[key]
  51. print("Key is ", key, " -> df is ", df)
  52. plot_dataframe(df, cnt, axes)
  53. cnt = cnt + 1
  54. for ax in axes:
  55. ax.legend()
  56. plt.show()
  57.  
  58. print("matplotlib version = ", matplotlib.__version__)
  59. print("pandas version = ", pd.__version__)
  60. print("sys version", sys.version_info)
  61.  
  62.  
  63.  
  64.  
  65. yax = 'bar'
  66. print("Original dictionary = ", my_dict)
  67. for k in my_dict.keys():
  68. if yax in k:
  69. # Should definitely go here
  70. df = my_dict[k]
  71. my_dict2[k] = df
  72. #print(df)
  73.  
  74. plot_kernels(my_dict2)
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement