1

I'm trying to generate a non-linear curve between two integers, let's say 0 and 25 that matches up to this curve here:

enter image description here

It seems like a stupid simple problem, the curve I need even looks like part of a circle, so generating a large circle, and taking the points at the bottom right would solve this, but I don't know how to do any of that.

All I need is a list from 0 to 25 with the curve as a part of that list, if that makes sense. Starting slowly increasing, then speeding up, then slowing down near the end.

I've tried this with a similar result, but it's not at the exact angle/curve.

x=[] y=range(25) for i in range(25): x.append((i**.25)**2) plt.plot(x, y) plt.show() 

enter image description here

Thanks

3
  • Do you have the original data used to generate the first curve? Commented Mar 28, 2019 at 1:34
  • If you want this exact curve you'll need to do curve fitting stackoverflow.com/questions/19165259/…. other wise just do x = [-(25-(i/5)**2)**0.5+5 for i in range(25)] Commented Mar 28, 2019 at 1:38
  • 1
    Thanks @Recessive! That link helped. Didn't know about curve fitting until just now haha Commented Mar 28, 2019 at 2:14

1 Answer 1

3

If you really want to have the same curve, you could load the plot image, and use PIL to get every blue pixel:

from urllib.request import urlopen from PIL import Image img = Image.open(urlopen('https://i.sstatic.net/NpiMq.png')) X = [] Y = [] for x in range(img.size[0]): for y in range(img.size[1]): r, g, b, a = img.getpixel((x, y)) if b > r and b > g: X.append(x) Y.append(y) 

Then, knowing that (0, 20) in the plot is the pixel (63, 355), and (25, 160) in the plot is the pixel (516, 32), you can transform pixel coordinates to data points:

X = [(x - 63) * (25 - 0) / (516 - 63) + 0 for x in X] Y = [(y - 355) * (160 - 20) / (32 - 355) + 20 for y in Y] 

Finally, you can use numpy polyfit to get a polynomial fit to the points you previously obtained:

>>> np.polyfit(X, Y, 3) [ 8.23918277e-03 -7.33330644e-02 2.60046715e+00 2.03012850e+01] 

And then, plot the result using poly1d to get a function that given a x value returns its y value:

import numpy as np from matplotlib import pyplot as plt x2y = np.poly1d(np.polyfit(X, Y, 3)) new_X = [x / 10 for x in range(250)] new_Y = [x2y(x) for x in new_X] plt.plot(new_X, new_Y) plt.show() 

Output of the plot

Sign up to request clarification or add additional context in comments.

1 Comment

@Recessive's comment helped me complete what I wanted to do, but thanks for the answer! I'm sure this would work too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.