Goutham Krishna
Goutham Krishna

Reputation: 3

How can I curve fit this particular data plot using python?

I have to make a cdm diagram using the data from a fits file. I have successfully did so. Now the next task is to make a line which goes through the most populated area. so how can I do it?

I am not sure how to even start as I am weak at programming myself

from astropy.io import fits
import matplotlib.pyplot as plt

leo = fits.open('Leo IV.fits')
data = leo[1].data

plt.scatter(data['M606']-data['M814'], data['M814'], color='k', s=1)
plt.title('Leo IV')
plt.gca().set_xlim([0.0,-0.8])
plt.gca().set_ylim([18,28])
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()
plt.show()

This is what I get: Graph I got

This is what I want:

enter image description here

How can I make the green line appear in my graph?

Upvotes: 0

Views: 464

Answers (2)

Deep
Deep

Reputation: 646

Sure. Here's a same code snippet:

    import numpy as np
    import matplotlib.pyplot as plt

    x = np.arange(0.0,10.0,0.1)
    y = 2*x**3 + 2

    fit = np.polyfit(x, y, 1)
    fit_fn = np.poly1d(fit)

    plt.scatter(x, y)          # blue
    plt.scatter(x, fit_fn(x))  # orange
    plt.grid()
    plt.show() 

You shall this as your output: polyfit_plot

Upvotes: 1

Deep
Deep

Reputation: 646

You can either use Matlabor numpy for polynomial curve fitting. Even a simple google sheet will also plot a trendline in the chart.

For numpy, check the function polyfit and poly1d for curve fit.

Upvotes: 0

Related Questions