0
x = [101.52762499 105.79521102 103.5158705   93.55296605 108.73719223 98.57426097  98.73552014  79.88138657  91.71042366 114.15815465]
y = [107.83168825  93.11360106 102.49196148  97.84879532 114.41714004 97.39079067 111.35664058  76.97523782  88.63047332  90.11216039]

I would like to do a scatter plot whereby it displays the regressed line with x-values span across 100 intervals evenly, from the minimum value to maximum value of x data.

What code do I need to use to change the x axis?

m,c = np.polyfit(x,y,1) #this is to find the best fit line
plt.plot(x, m*x + c) # this to plot the best fit line
plt.plot(x,y,'o') # this is to plot in 

I tired using plt.xticks(0,200) but it gives me an error message

TypeError: object of type 'int' has no len()

Gen
  • 27
  • 5
  • You mean [`plt.xlim(0, 200)`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xlim.html). You tried to set two [x-ticks](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html?highlight=xticks#matplotlib.pyplot.xticks). – Mr. T Feb 06 '21 at 12:53
  • This is a duplicate for subplots but the linked question there provides additional solutions using `plt.xlim()`. I do not want to link directly to the duplicate question because the accepted answer imports pylab which is discouraged due to namespace littering. [How to set xlim and ylim for a subplot in matplotlib](https://stackoverflow.com/questions/15858192/how-to-set-xlim-and-ylim-for-a-subplot-in-matplotlib) – Mr. T Feb 06 '21 at 12:57

1 Answers1

1

The following code puts x ticks to create 100 equally-sized intervals between the first and last value.

import numpy as np
from matplotlib import pyplot as plt

x = np.array([101.52762499, 105.79521102, 103.5158705, 93.55296605, 108.73719223, 98.57426097, 98.73552014, 79.88138657, 91.71042366, 114.15815465])
y = np.array([107.83168825, 93.11360106, 102.49196148, 97.84879532, 114.41714004, 97.39079067, 111.35664058, 76.97523782, 88.63047332, 90.11216039])

m, c = np.polyfit(x, y, 1)  # this is to find the best fit line
plt.figure(figsize=(15, 4))
plt.plot(x, m * x + c)  # this to plot the best fit line
plt.plot(x, y, 'o')  # this is to plot in

bins = np.linspace(x.min(), x.max(), 101) # 100 equally-sized intervals
plt.xticks(bins, rotation=90)
plt.grid(True, axis='x') # the grid lines show the 100 intervals of the xticks
plt.margins(x=0.02) # less whitespace left and right
plt.tight_layout()
plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66