0

I use the following code to produce a plot:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv('Position_Salaries.csv')

X = dataset.iloc[: , 1:2].values
y = dataset.iloc[: , 2:3].values

from sklearn.linear_model import LinearRegression

l_regressor = LinearRegression()
l_regressor.fit(X, y)

from sklearn.preprocessing import PolynomialFeatures

p_regressor = PolynomialFeatures(degree = (0,5))

X_poly = p_regressor.fit_transform(X)

lp_regressor = LinearRegression()

lp_regressor.fit(X_poly, y)

plt.scatter(X, y, color='red')

plt.plot(X, l_regressor.predict(X), color='blue')

plt.title('Salary vs Position (Linear Reg)')

plt.xlabel('Position')

plt.ylabel('Salary')

plt.show()

Output:

This is the output

How can I achieve something like the following output instead?

This is the desired output

ouroboros1
  • 9,113
  • 3
  • 7
  • 26
  • 1
    What exactly do you mean by "Matplotlib is reducing the graph scale". `matplotlib` is automatically applying "scientific notation". Note "1e6" in the top left corner. You can use `plt.ticklabel_format(style='plain')` to prevent this. If you want to extend the `y-axis` to 1.2e6 (as in the desired output), add `plt.ylim(top=1.2e6)`. – ouroboros1 Sep 04 '22 at 09:00
  • What is your problem? Both Y scale range to million (notice the 10e6 at the top of the axes). – jlandercy Sep 04 '22 at 09:00
  • Does this answer your question? [How do I change the size of figures drawn with Matplotlib?](https://stackoverflow.com/questions/332289/how-do-i-change-the-size-of-figures-drawn-with-matplotlib) – Jody Klymak Sep 05 '22 at 16:08

1 Answers1

0

Your figure has the default size, which is 6.4 by 4.8 inches. The desired result is a larger figure. You can specify the size of the figure as follows:

fig, ax = plt.subplots(1, 1, figsize=(13, 10))
ax.scatter(X, y, color='red')
ax.plot(X, l_regressor.predict(X), color='blue')

ax.set_title('Salary vs Position (Linear Reg)')
ax.set_xlabel('Position')
ax.set_ylabel('Salary')
dkruit
  • 135
  • 6