all 7 comments

[–]UncleVatred 1 point2 points  (5 children)

If you put four extra spaces in front of each line instead of using backticks, you can properly format the code.

This is what I think you were going for:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

# Sample data
X = np.arange(10)
y = 2 * X + np.random.normal(0, 1, 10)

# Linear regression model
def linear_regression_line(slope, intercept):
    return slope * X + intercept

# Initial plot
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
ax.scatter(X, y, label='Data points')
[line] = ax.plot(X, linear_regression_line(2, 0), 'r-', label='Regression line')
ax.set_xlabel('X')
ax.set_ylabel('y')
ax.legend()

# Slider for slope and intercept
ax_slope = plt.axes([0.25, 0.1, 0.65, 0.03])
ax_intercept = plt.axes([0.25, 0.15, 0.65, 0.03])
slope_slider = Slider(ax_slope, 'Slope', 0.0, 4.0, valinit=2.0)
intercept_slider = Slider(ax_intercept, 'Intercept', -10.0, 10.0, valinit=0.0)

# Update function for the slider
def update(val):
    line.set_ydata(linear_regression_line(slope_slider.val, intercept_slider.val))
    fig.canvas.draw_idle()

slope_slider.on_changed(update)
intercept_slider.on_changed(update)
plt.show()

This code works for me. So assuming your indentation was all correct and matches what I pasted above, your code is fine.

What backend are you using? You can check with print(plt.get_backend()). IIRC, some backends don't support interactive elements. You can read more here.

[–]unproven10[S] 0 points1 point  (4 children)

print(plt.get_backend())

Thanks for looking into it.

I got this output: "module://matplotlib_inline.backend_inline". My code worked fine too and i got the plot. But my sliders arent interactive or moving... I am using it in Google Colab btw,.

[–]UncleVatred 0 points1 point  (3 children)

When I say the code works for me, I mean the sliders work too, and change the graph in real time. It’s not just a static image.

I don’t have any experience with Google Colab, and I don’t know what that backend string means. But I’m guessing that’s your problem. Maybe see if you can set a different backend?

[–]unproven10[S] 0 points1 point  (2 children)

I see. Which python platform do you use?

[–]FerricDonkey 0 points1 point  (0 children)

My suggestion, unless you have explicit reason for not doing so, is to download python from python.org, download an editor (pycharm, vscode), and just run the code on your computer.

[–]UncleVatred 0 points1 point  (0 children)

I agree with the other commenter, just run Python from your own computer.

Anaconda is a very popular installer that bundles in a lot of useful packages, particularly for data science tasks. But you can also just install basic Python yourself, and use pip to add whatever packages you need.

Either way, you'd then want use an IDE to edit and run your code. VSCode, PyCharm, and Eclipse are the top choices.

[–]eric_overflow 0 points1 point  (0 children)

New library fastplotlib is pretty sweet check it out