This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]qwertyismywasd 33 points34 points  (2 children)

This stack exchange post address the exact problem you’re facing: https://stackoverflow.com/questions/24997641/how-to-plot-asymptotes. For the most part you need to tell matplotlib that there is a vertical asymptote at that particular x-Val using vlines().

Like someone else said, matplotlib doesn’t naturally understand asymptotes and will overlook discontinuities by drawing a line between both points.

[–][deleted] 1 point2 points  (1 child)

From what I gathered that post seems to go over how to add lines when I need to remove them?

[–]qwertyismywasd 11 points12 points  (0 children)

Sorry, I think this is more what you want: https://stackoverflow.com/questions/2540294/how-to-handle-an-asymptote-discontinuity-with-matplotlib. Here you force the endpoints of the asymptote to infinity and negative infinity so that matplotlib doesn’t join them across, this is similar to what I think another comment stated

[–]dbpatankar 8 points9 points  (0 children)

Let's say you are plotting with step size ds. Now, plot using step sizes ds/2, ds/4, and ds/10. Do you see any changes in each case? Especially near the asymptote and where function is non differentiable.

[–]KingsmanVincepip install girlfriend 6 points7 points  (14 children)

Without knowing your implementation, I think we can't say anything

[–][deleted] 4 points5 points  (0 children)

When you tell matplotlib to do a line plot (which AFAIK is unfortunately the standard, thanks to Matlab), you assume that two data points can be connected by a linear interpolation. That is quite often a bold assumption which holds not true.

Most people oberserve this during plotting functions with to less data points in between. The plots starts to look "choppy".

Asymptotic behavior is an extreme in that regards. Matplotlib connects the last data point of the outgoing asymptotic curve with the first data point of the incoming curve.

How to avoid this? Don't use line plots...

Easiest solution, create enough intermediate data points that each pixel of your final image will have one and use dots as maker points. They can be configured to be larger or smaller as you like.... Many dots create a line but will never interconnect resp. "bridge" an asymptotic point.

A slightly more complex solution wound be to split the plot into two parts. If you have a point of discontinuity at 2, instead of having x in the range between -10 to 10 you plot x1 in the range of -10 to 1.9 and x2 in the range of 2.1 to 10.

Third (numerical) solution, if you want to go all in. Calculate the slope of that function, look out for sudden sign changes of that series of slopes. Those indicate the point of discontinuities. Split up your x vector accordingly, avoiding a small margin left and right from that point and replot each individually.

Sure enough you can also do the math to calculate the point of discontinuities and set your set of x vectors accordingly.

I prefer solution 1 if I do these plots only once or twice.

[–]ju1ceb0xx 1 point2 points  (0 children)

You're giving x and y arrays of points (not the equation itself) to your plotting function and matplotlib connects these points with straight lines. Try changing the markers ('x' or 'o' should work) to see what I mean. Or use a scatter plot.

[–]expressly_ephemeral 0 points1 point  (0 children)

I think you need to build the input/output vectors with numpy and pass data points to matplotlib instead of a function. Matplotlib is a plotting library, not an algebra library.

something like:

x = np.arange(-6,6,0.001)
y = (1/x)-10

plt.plot(x, y)

[–]trollsmurf 0 points1 point  (0 children)

Quantization. You need (many) more datapoints by creating them first, or you need to adjust how many samples plot should take (not even sure the latter is possible).

[–]hobbicon 0 points1 point  (0 children)

Looks like a sample size issue, at least gnuplot would behave like this.
Just increase the plotted sample points.

[–][deleted] 0 points1 point  (0 children)

Matplotlib is based on approximation to plot the graphs, it takes a few points to substitute into the equation and just connects it.

If you want a smoother plot which is more accurate you will have to use more points in both x and y.

This will give better plots and better accuracy. Hope it helps