juanxo90 commited on
Commit
be58b6b
·
verified ·
1 Parent(s): dee227a

Update tools/normal_distribution.py

Browse files
Files changed (1) hide show
  1. tools/normal_distribution.py +19 -10
tools/normal_distribution.py CHANGED
@@ -1,6 +1,7 @@
1
  import random
2
  import math
3
  import matplotlib.pyplot as plt
 
4
  from smolagents import tool
5
 
6
  @tool
@@ -35,7 +36,7 @@ def generate_normal_distribution(mean: float, std_dev: float, count: int = 10000
35
  return samples
36
 
37
  @tool
38
- def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_numbers:list)->None:
39
  """Generate a histogram of random numbers and overlay the theoretical
40
  probability density function (PDF) of a normal distribution.
41
  Return the histogram as a base64-encoded string.
@@ -71,13 +72,21 @@ def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_number
71
  for x in x_values
72
  ]
73
 
74
- # Plotting
75
- plt.figure(figsize=(10, 6))
76
- plt.bar(x_values, hist_data, width=bin_width, alpha=0.6, color='g', label='Histogram')
77
- plt.plot(x_values, pdf_values, 'k', linewidth=2, label='Theoretical PDF')
78
 
79
- plt.title(f'Histogram of {len(random_numbers)} Random Numbers\nMean: {mean}, Std Dev: {std_dev}')
80
- plt.xlabel('Value')
81
- plt.ylabel('Density')
82
- plt.legend()
83
- plt.grid()
 
 
 
 
 
 
 
 
 
 
1
  import random
2
  import math
3
  import matplotlib.pyplot as plt
4
+ import matplotlib
5
  from smolagents import tool
6
 
7
  @tool
 
36
  return samples
37
 
38
  @tool
39
+ def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_numbers:list)->matplotlib.figure.Figure:
40
  """Generate a histogram of random numbers and overlay the theoretical
41
  probability density function (PDF) of a normal distribution.
42
  Return the histogram as a base64-encoded string.
 
72
  for x in x_values
73
  ]
74
 
75
+ # Create a figure
76
+ fig = plt.figure(figsize=(10, 6))
77
+ ax = fig.add_subplot(111) # Create an Axes object on the figure
 
78
 
79
+ # Add histogram as a bar plot to the Axes
80
+ ax.bar(x_values, hist_data, width=bin_width, alpha=0.6, color='g', label='Histogram')
81
+
82
+ # Add theoretical PDF as a line plot to the same Axes
83
+ ax.plot(x_values, pdf_values, 'k', linewidth=2, label='Theoretical PDF')
84
+
85
+ # Set title and labels
86
+ ax.set_title(f'Histogram of {len(random_numbers)} Random Numbers\nMean: {mean:.2f}, Std Dev: {std_dev:.2f}')
87
+ ax.set_xlabel('Value')
88
+ ax.set_ylabel('Density')
89
+ ax.legend()
90
+ ax.grid()
91
+
92
+ return fig