Is there any option to run a polyfit on a scatter plot? (2024)

15 views (last 30 days)

Show older comments

Wolfgang McCormack on 28 May 2021

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot

Answered: Image Analyst on 29 May 2021

Hi all,

I have a scatter plot and there are some dots on that. Is there any option to get the X and Y of those points on the scatter plot? Furthermore, anyoption to run polyfit among those points directly on the scatter plot?

Thanks

8 Comments

Show 6 older commentsHide 6 older comments

Cris LaPierre on 28 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549860

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549860

How did you create the scatter plot?

Walter Roberson on 28 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549880

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549880

Do you still have the data that was used to create the scatter plot?

If not, is the scatter plot itself still available on the display, so that the data could be extracted from it?

Or is what you have an image of a scatter plot?

dpb on 28 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549940

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1549940

Presuming you still do have the figure, in recent (I don't know how recent is required), under the Tools menu on the figure is the ability to do so interactively.

As Walter says, if you have either the original data or the figure, you can certainly do whatever you want; just may have to retrieve the X|YData from the figure in the one case.

Wolfgang McCormack on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550580

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550580

@Walter Roberson @dpb @Cris LaPierre, I have both the initial data and figure itself. but DataA and DataB are not simple ordinal X and Y but rather they gain meaning in a scatter plot. The data entries are more than 10,000 entries but the scatter plot created from them is just 6 points on the scatter plot. Now I want a fit in these 6 points and their locations. Also, any idea on how to code the statistics tool box options such as the R^2 and RMSE? Right now, I rewrite them from the tool box using a text but that would be a pain for the rest of my plots.

dpb on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550650

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550650

Even if the data are redundant samples of some small number of values, the statistics of the fit of the data are based on the weighted errors; you could either fit the raw data or use the counts to classify the points and fit the unique values.

Depending on on the distribution of the points relative to the locations, just fitting the N unique values as one-each point may return a quite different result if the relative number of points is grossly different between these values.

In the Statistics TB, regstats will return all the diagnostic statistics you could want...

Star Strider on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550685

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550685

Open in MATLAB Online

@Wolfgang McCormack If there are 10,000 points represented by only 6 distinct points on the scatter plot, are they all the same values, or do they have a third dimension as well?

Example —

x = repmat(rand(6,1),1, 100);

y = repmat(rand(6,1),1, 100);

z = reshape(rand(600,1),6, 100);

figure

scatter(x, y, 'filled')

grid

xlabel('x')

ylabel('y')

title('2D Scatter Plot')

Is there any option to run a polyfit on a scatter plot? (8)

figure

scatter3(x(:), y(:), z(:), 'filled')

grid on

xlabel('x')

ylabel('y')

zlabel('z')

title('3D Scatter Plot')

Is there any option to run a polyfit on a scatter plot? (9)

The polyfit (and polyval) functions would likely not have problems with only ‘x’ and ‘y’, however if weighting with respect to the ‘z’ coordinates would be required, that could be more difficult. The Statistics and Machine Learning Toolbox functions (such as fitlm and fitnlm) would likely be more appropriate here, not only because they would provide the desired statistics, but also because they will allow weighting.

.

Wolfgang McCormack on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550895

@Star Strider Yes, they are 10k points but in one the variable y changes between only 6 values. For example there are 3000 values of 4.36 and 2000 of 6.54 and so on. Same thing happens in variable x too. Are they duplicates? They are not actually duplicates because the data is hourly. But in other sense, you can say they are the same in many hours. THere is no third dimension in my case but thanks for pointing that out. It'll def help me in future. :D I guess I can write a book based on all my questions so far on MATLAB, naming it MATLAB for junior researchers :D Thank you all

Star Strider on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550945

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550945

Open in MATLAB Online

@Wolfgang McCormack I am doing my best to understand what the data are in the absence of the data themselves.

So they are actually something like this, then —

N = 25;

x = rand(1,N);

y = repmat(randn(6,1),1,N);

figure

scatter(x, y, 'filled')

grid

Is there any option to run a polyfit on a scatter plot? (12)

or this —

figure

scatter(x, y(randi(6,1,N)), 'filled')

grid

Is there any option to run a polyfit on a scatter plot? (13)

?

.

Sign in to comment.

Sign in to answer this question.

Answers (2)

Cris LaPierre on 29 May 2021

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#answer_712080

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#answer_712080

Polyfit is not going to return the X and Y values of those 6 points. It's going to return the polynomial coefficients for the equation that best fits the data. You then supply that equation with whatever X values you want to obtain the corresponding Y values. Using those, you can plot the fit line.

If you need the X and Y of the 6 groups, I'd suggest using something like kmeans clustering to identify 6 clusters and return their centroids first.

[idx,C] = kmeans(___)

Use the centroids as inputs to polyfit.

Calculating Is there any option to run a polyfit on a scatter plot? (15) and RMSE is fairly simple. You just need to do some math to calculate SSR and SST. See this answer.

2 Comments

Show NoneHide None

Wolfgang McCormack on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550900

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550900

@Cris LaPierre Thank you Cris, this will def help me in future however in my current case, I only have 6 changes in each variable. like there are 3000 of 3.46 in X and 5000 of 4.6 and so on.

Cris LaPierre on 29 May 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550920

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#comment_1550920

Share your data. It will be easier than trying to guess what is going on. Save your variables to a mat file and attach them to your post using the paperclip icon.

Sign in to comment.

Image Analyst on 29 May 2021

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#answer_712185

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/842620-is-there-any-option-to-run-a-polyfit-on-a-scatter-plot#answer_712185

Open in MATLAB Online

Chris suggests a nice trick. And attach your data like he says in his hidden comment (click link above to show it).

save('answers.mat', 'DataA', 'DataB');

Use the paperclip icon.

Another trick I've used when you have quantized data (multiple points with the same x value) is to add a very slight amount of noise to the x data. Add enough noise to make them unique and avoid the error polyfit throws, but not enough to change the formula it will find:

% Determine range of data.

minx = min(x)

maxx = max(x)

% Add a fraction of a percent of noise to x to make them unique.

xNoisy = x + 0.00001 * (maxx - minx);

% Determine the formula with the noisy x instead of the actual x.

% Below we will use a second order polynomial.

coefficients = polyfit(xNoisy, y, 2); % Fit a quadratic.

% Get estimated y from arbitrary x

estimatedY = coefficients(3) * thisX .^2 + coefficients(2) * thisX + coefficients(1);

Note that this will give a different formula than Chris's because this will consider how many points are in the cluster, so more points in a cluster will influence the line more, while Chris's uses the centroids of the clusters which ignores how many points are in the cluster. If you have about the same number of points in each cluster, it won't make much of a difference, but if some clusters have wildly different number of points than other clusters, then it could make a noticeable difference.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsSurfaces, Volumes, and PolygonsSurface and Mesh Plots

Find more on Surface and Mesh Plots in Help Center and File Exchange

Tags

  • scatter
  • polyfit
  • fit
  • line
  • x
  • y

Products

  • MATLAB

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Is there any option to run a polyfit on a scatter plot? (19)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Is there any option to run a polyfit on a scatter plot? (2024)

FAQs

How to fit a polynomial to a scatter plot in Matlab? ›

You specify a quadratic, or second-degree polynomial, using 'poly2' . The first output from fit is the polynomial, and the second output, gof , contains the goodness of fit statistics you will examine in a later step. [population2,gof] = fit(cdate,pop,'poly2'); To plot the fit, use the plot function.

How to put a line of best fit on a scatter plot in Stata? ›

Line of Best Fit

In Stata, you do this by using lfit and add it to the end of your scatter plot command. In the lfit command you include the same variables on your scatterplot in the same order. There is a slight slope to the line, indicating we may have a significant linear relationship.

How do you fit a function to a scatter plot? ›

First of all, a scatterplot is built using the native R plot() function. Then, a polynomial model is fit thanks to the lm() function. It is possible to have the estimated Y value for each step of the X axis using the predict() function, and plot it with line() .

How do you make a line of best fit on a scatter plot excel? ›

To better visualize the relationship between the two variables, you can draw a trendline in your Excel scatter graph, also called a line of best fit. To have it done, right click on any data point and choose Add Trendline… from the context menu.

Does a scatter graph need a line of best fit? ›

Scatter graphsLine of best fit. Scatter graphs are a visual way of showing if there is a connection between groups of data. If there is a strong connection or correlation, a 'line of best fit' can be drawn.

What is the formula for Polyfit? ›

polyfit (MATLAB Functions) [p,S] = polyfit(x,y,n) returns the polynomial coefficients p and a structure S for use with polyval to obtain error estimates or predictions. If the errors in the data y are independent normal with constant variance, polyval produces error bounds that contain at least 50% of the predictions.

How to use polyfit command? ›

p = polyfit(x, y, N); Returns the coefficients for a polynomial p(x) of degree N that is the best fit for the data in y. The length of the coefficients is N+1. In this example random noise is added to a smooth function.

What is polyfit in MATLAB? ›

polyfit. polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the y data by minimizing the sum of the squares of the deviations of the data from the model (least-squares fit). polyval. polyval(p,x) returns the value of a polynomial of degree n that was determined by polyfit , evaluated at x ...

How do you add a best fit line to a scatter plot sheet? ›

Implementing the Line of Best Fit

Click on the scatter plot and navigate to “Customize” > “Series” > “Trendline” > “Linear” to add the line of best fit.

How do you add a line of best fit to a scatter plot in SPSS? ›

First, to add the line of fit described by the regression analysis, right click on the chart and select “Add fit Line at Total”, which is towards the bottom of the listed options. This will add the line to the scatter plot. You can close the “Properties” box that is displayed after you have added this line.

How to add a line of best fit to a scatter plot in R? ›

To plot a line of best fit in R, use the lm() function to fit a linear model to the data, then plot the model using the plot() function. You can also add the line of best fit to the existing plot using the abline() function.

How do you draw a line of fit on a scatter plot? ›

Draw a line of best fit for the scatter plot given. Solution: Draw a line through the maximum number of points, balancing about an equal number of points above and below the line.

What is the polynomial trendline in a scatter plot? ›

A polynomial trendline is a curved line that is used when data fluctuates. It is useful, for example, for analyzing gains and losses over a large data set. The order of the polynomial can be determined by the number of fluctuations in the data or by how many bends (hills and valleys) appear in the curve.

What is the best fit curve of a scatter plot? ›

Curve of Best Fit: a curve the best approximates the trend on a scatter plot. If the data appears to be quadratic, we perform a quadratic regression to get the equation for the curve of best fit. If it appears to be cubic, then we perform a cubic regression.

Does a best fit line have to start at 0? ›

Lines of best fit do not have to begin at (0,0). In this case a data point of (0,0) does make sense because no entrants would, presumably, mean no ice cream sales, but it is not wise to automatically draw lines of best fit starting at (0,0).

What is the line of best fit on a graph plot? ›

A line of best fit, also called a trend line or linear regression, is a straight line drawn on a graph that best represents the data on a plot. This line passes through some of the points, all of the points, or none of the points. It can be used to make predictions or to show trends in data.

How do you find the line of best fit on a scatter plot on a TI 84? ›

Press @ and set up appropriately Press S> to get this screen. or choose Zoom:Stat. If the data looks linear, Press e`Ω,`æ,v>ee select 4:LinReg(ax +b) as shown. to get this screen. This will calculate the best fitting line for your data whose x-values are in L1 and y-values are in L2.

References

Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6820

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.