Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:46:01+00:00 2026-06-06T17:46:01+00:00

I am trying to plot a linear regression line on a scatter plot. I

  • 0

I am trying to plot a linear regression line on a scatter plot. I have researched it (for example Linear regression with matplotlib / numpy) but my implimentation doesn’t work

    x = [-6.0, -5.0, -10.0, -5.0, -8.0, -3.0, -6.0, -8.0, -8.0, 7.5, 8.0, 9.0, 10.0, 7.0, 5.0, 5.0, -8.0, 8.0, 7.0] 
    y = [-7.094043198985176, -6.1018562538660044, -15.511155265492038, -2.7131460277126984, -8.6127363078417609, -3.1575686002528163, -10.246242711042497, -6.4333658386991992, -16.167988119268013, 2.4709555610646134, 4.5492058088492948, 5.5896790992867942, 3.3824425476540005, -1.8140272426684692, -1.5975329456235758, 5.1403915611396904, -4.4469105070935955, 0.51211850576547091, 5.7059436876065952]
    m,b = numpy.polyfit(x,y,1)
    plt.plot(x, y, x, m*x+b) 
    plt.show()

Returns:

Traceback (most recent call last):
  File "test.py", line 464, in <module>
    correlate(trainingSet,trainingSet.trainingTexts)
  File "test.py", line 434, in correlate
    plt.plot(x, y, x, m*x+b)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2458, in plot
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3848, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 323, in _grab_ne
xt_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 300, in _plot_ar
gs
    x, y = self._xy_from_xy(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 240, in _xy_from
_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

I have checked that both x and y are the same length, and they are (19). Any ideas what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:46:02+00:00Added an answer on June 6, 2026 at 5:46 pm

    The problem is that x is a python list, not a numpy array, so * works not the way you expected:

    >>> m * x
    []
    

    This is the working example

    plt.plot(x, y, x, numpy.array(x) * m +b) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a linear regression on a scatter plot I have generated,
I'm trying to plot a graph in matplotlib using numpy and meshgrid. I want
I'm trying to plot a histogram with matplotlib. I need to convert my one-line
I am trying to plot a graph using gnuplot. I have six text files.
I am trying to plot some data using pylab scatter function. I'm using pylab.scatter(X,
I'm trying to plot a couple of UnitStep functions, but for some reason, Mathematica
I am trying to create a filled contour plot in matplotlib (Win7, 1.1.0). I
I am trying to plot a ratio but my problem is that when the
I am trying to plot a variable number of lines in matplotlib where the
I am trying to plot a 2nd order linear ODE with the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.