PieCharts.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from scipy.stats import ttest_ind, f_oneway
  5. import glob
  6. import os
  7. from scipy.stats import ttest_ind
  8. from statsmodels.stats.multitest import multipletests
  9. import re
  10. # Set font to Times New Roman
  11. plt.rcParams['font.family'] = 'Times New Roman'
  12. script_dir = os.path.dirname(__file__)
  13. p_address = os.path.join(script_dir, '..', 'input', 'AIDAqc_Testdaten_PieChart.csv')
  14. p_save = os.path.join(script_dir, '..', 'figures')
  15. # Load the CSV data into a pandas DataFrame
  16. data = pd.read_csv(p_address)
  17. # Clean up the 'Scanner' column to extract the field strength (including decimals)
  18. def extract_field_strength(scanner):
  19. match = re.search(r'(\d+(\.\d+)?)T', scanner)
  20. if match:
  21. return f"{match.group(1)}T"
  22. else:
  23. return None
  24. data['Scanner'] = data['Scanner'].apply(extract_field_strength)
  25. cm = 1/2.54 # centimeters in inches
  26. # Set up the figure and axes using subplots
  27. fig, axes = plt.subplots(1, 4, figsize=(22*cm, 10*cm), dpi=300)
  28. rrr = 1.1
  29. # Species pie chart
  30. species_counts = data['Species'].value_counts()
  31. axes[0].pie(species_counts, labels=species_counts.index, autopct='%1.1f%%', startangle=180, pctdistance=0.75,radius=rrr)
  32. #axes[0].set_title('(a) Species', weight='bold', fontsize=10)
  33. # Field strength pie chart
  34. scanner_counts = data['Scanner'].value_counts()
  35. axes[1].pie(scanner_counts, labels=scanner_counts.index, autopct='%1.1f%%', startangle=180, pctdistance=0.70,radius=rrr)
  36. #axes[1].set_title('(b) Field strength', weight='bold', fontsize=10)
  37. # Sequence type pie chart
  38. sequences_data = data['Sequences'].str.split(', ', expand=True)
  39. sequences_melted = sequences_data.melt(value_name='Sequence').dropna()['Sequence']
  40. sequence_counts = sequences_melted.value_counts()
  41. axes[2].pie(sequence_counts, labels=sequence_counts.index, autopct='%1.1f%%', startangle=180, pctdistance=0.65,radius=rrr)
  42. #axes[2].set_title('(c) Sequence type', weight='bold', fontsize=10)
  43. # Data format pie chart
  44. format_counts = data['Data format'].value_counts()
  45. axes[3].pie(format_counts, labels=format_counts.index, autopct='%1.1f%%', startangle=180,radius=rrr)
  46. #axes[3].set_title('(d) Data format', weight='bold', fontsize=10)
  47. # Turn off axes for all subplots
  48. for ax in axes:
  49. ax.axis('off')
  50. # Adjust layout and save the figure
  51. #plt.tight_layout()
  52. plt.savefig(os.path.join(p_save , "All_Pie_Charts" + ".svg"), format='svg', bbox_inches='tight')
  53. plt.savefig(os.path.join(p_save , "All_Pie_Charts" + ".png"), format='png', bbox_inches='tight')
  54. plt.show()