2

The following example of the curve function in R,

curve(dgamma(x, 3, .1), add=T, lwd=2, col="orange"),

plots the curve for the probability density function of the dgamma continuous distribution. The equivalent to dgamma in Python is scipy.stats.dgamma.

How can I plot the same curve for the same distribution in Python? I would like this more than fitting a kernel density estimator (KDE), which tend to be inaccurate.

develarist
  • 1,224
  • 1
  • 13
  • 34
  • First choose a visualisation library. Matplotlib is the most widely used, but depending on your environment some other may be more appropriate or provide better look & feel. – zvone Jan 07 '21 at 21:29
  • is there a `curve` equivalent in `matplotlib` or `scipy`? – develarist Jan 07 '21 at 21:30
  • 1
    `scipy` is for calculation, `matplotlib` is for drawing and can definitely draw a curve. You should go through the examples and tutorial they offer. – zvone Jan 07 '21 at 21:35
  • Is there an equivalent function for `curve` in Python? It's not just any curve we're graphing here. It's a curve of a continuous distribution *function*. `curve` draws a curve corresponding to a *function* over the interval [from, to] https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/curve – develarist Jan 07 '21 at 21:38
  • 1
    When drawn, a curve is just a set of pixels. It is usually drawn as a finite and rather small set of lines connecting an equally small set of points. The fact that the data is derived from a continuous distribution function is completely lost once it is converted to pixels. Honestly, I don't remember whether matplotlib has some utility function whould would take the same arguments and R, but it should be fairly easy to use numpy to generate a set of points in a given range and then draw them. – zvone Jan 07 '21 at 21:45
  • Something like this: https://stackoverflow.com/a/63217196/496803 - specify a range of x points with a sufficient number to make it look smooth, apply the function to the x points to get y, and then line plot it. Which if you look at the code of R's `curve`, is exactly what it does. – thelatemail Jan 07 '21 at 21:58
  • [This example](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gamma.html) plots a gamma density in a very small number of lines of code; . I don't know if what you're looking for (automatically construct the x vector, then evaluate the specified function for that x) exists. You do need to define your x-vector first, then `ax.plot(x, gamma.pdf(x, a))` will draw the curve. (Or whatever the appropriate incantation is for matching `add=TRUE`, i.e. adding a line rather than drawing a new plot.) – Ben Bolker Jan 07 '21 at 21:58
  • You can probably retrieve the x-axis limits from an existing `matplotlib` plot object, but I don't know how without googling it. – Ben Bolker Jan 07 '21 at 22:01

1 Answers1

1

I don't think you have a curve equivalent in matplotlib or seaborn for that matter. You have to define a set of points and plot over it on the same device. In this case, since you are doing a histogram, it's getting a number of evenly spaced points between the min and max :

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

x = stats.gamma.rvs(a=3,scale=1/0.1,size=1000)
plt.hist(x,density=True)
xl = np.linspace(x.min(),x.max(),1000)
plt.plot(xl,stats.gamma.pdf(xl,a=3,scale=1/0.1))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72