[Matplotlib-users] Query about Programming Error

vincent.adrien at gmail.com vincent.adrien at gmail.com
Thu Dec 22 05:11:25 EST 2022


Hi Roberto,

Besides the problem of trying to modify the parameters of a frozen distribution instance as pointed out by Cameron, it looks like your example code is missing the display of the graphical elements to define the parameters of the Gaussian distribution that you want to plot. There is also a small typo in your call to `np.linspace` (`np.linspace(...)` and not `np.linspace[...]`).

The modified program below seems to work, at least on my computer. Please note that I am no user of tkinter so those parts of the code might not really follow to usual best practices regardind the GUI elements.

```python
from tkinter import*
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats


root = Tk()

# Set up Gaussian parameters with default dummy values
mu = DoubleVar(master=root, value=0.0)
sigma = DoubleVar(master=root, value=1.0)
a = DoubleVar(master=root, value=-5.0)
b = DoubleVar(master=root, value=5.0)
c = DoubleVar(master=root, value=1000)

label10 = Label(root,text="Media", font=("Times_New_Roman", 20))
label10.place(x=180, y=100)
entry6 = Entry(root, textvariable=mu, font=("Times_New_Roman", 20))
entry6.place(x=600, y=100)

label11 = Label(root, text="Desviación estándar", font=("Times_New_Roman", 20))
label11.place(x=180, y=200)
entry7 = Entry(root, textvariable=sigma, font=("Times_New_Roman", 20))
entry7.place(x=600, y=200)

label12 = Label(root, text="Linspace1", font=("Times_New_Roman", 20))
label12.place(x=180, y=300)
entry8 = Entry(root, textvariable=a, font=("Times_New_Roman", 20))
entry8.place(x=600, y=300)

label13 = Label(root, text="Linspace2", font=("Times_New_Roman", 20))
label13.place(x=180, y=400)
entry9 = Entry(root, textvariable=b, font=("Times_New_Roman", 20))
entry9.place(x=600, y=400)

label14 = Label(root, text="Linspace3", font=("Times_New_Roman", 20))
label14.place(x=180, y=500)
entry10 = Entry(root, textvariable=c, font=("Times_New_Roman", 20))
entry10.place(x=600, y=500)


def Graph_Generator ():

     # Set up the Gaussian distribution
     normal = stats.norm(loc=mu.get(), scale=sigma.get())
     
     # Parameters for the display
     x_min = a.get()
     x_max = b.get()
     nb_samples = int(c.get())  # beware that linspace expects an integer

     # Compute what to plot
     x = np.linspace(x_min, x_max, nb_samples)
     fp = normal.pdf(x) #función de probabilidad

     # Display the results
     fig, ax = plt.subplots()
     ax.plot(x, fp)
     ax.set_xlim(x_min, x_max)
     plt.show()

graph_button = Button(root, text="Generate graph", command=Graph_Generator)
graph_button.pack(pady=30)

# Display elements related to `mu` parameter
label10.pack()
entry6.pack()

# Display elements related to `sigma` parameter
label11.pack()
entry7.pack()

# Display elements related to `a` parameter
label12.pack()
entry8.pack()

# Display elements related to `b` parameter
label13.pack()
entry9.pack()

# Display elements related to `c` parameter
label14.pack()
entry10.pack()

root.mainloop()
```

Hopefully this will help you.

Best regards,
Adrien

Le 21/12/2022 à 23:50, Cameron Simpson a écrit :
> On 20Dec2022 14:14, Roberto Martin ZAMORA NEVADO <roberto.zamora1 at unmsm.edu.pe> wrote:
>> I am developing a program for research purposes to enter from Tkinter
>> values of mean, standard deviation and ranges, to graph a normal
>> distribution, but it throws me an error of
>>
>> Traceback (most recent call last):
>>  File "C:\Program
>> Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py",
>> line 1921, in __call__
>>    return self.func(*args)
>>  File "g:\Cursos\Curso Básico de Python\Prueba 102", line 45, in
>> Graph_Generator
>>    x=np.linspace[normal(a.get()), normal(b.get()),(c.get())]
>> TypeError: 'rv_continuous_frozen' object is not callable
> 
> That suggests that `normal` is a `rv_continuous_frozen` instance. That has a bunch of methods to obtain various things.
> 
> Looking at the docs for `scipy.stats.norm`:
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.stats.norm
> 
> It looks like your line:
> 
>      normal= stats.norm(mu,sigma)
> 
> obtains one of these objects. Based on the examples there, maybe you need the distribution's `.pdf` method (probablity distribution function)? Eg in the erroring line:
> 
>      x=np.linspace[normal(a.get()), normal(b.get()),(c.get())]
> 
> you want to say `normal.pdf(a.get())` and so forth instead of just `normal`?
> 
> The docs for `rv_continuous_frozen` are here:
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.html#scipy.stats.rv_continuous
> and it has a list of the methods about a screen or so down the page.
> I do not know if you want the `.pdf()` method or one of the others.
> 
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list