0

It's been impossible finding an answer to this question. If you have a simple linear graph, you have one slope and one intercept abline(intercept = 1, slope = .5; if you plot a regression, you can easily insert the result of the regression into abline function; however, let's say you're running a perceptron model with two features/variables you would get the line m1x1 + m2x2 + b. How can you add these two coefficients to the abline() function? It should be the same in Python as R, so feel free to use whatever code is easier for you.

I mostly use R but I'm in a course for Python, but I find that ggplot works much better than matplotlib or seaborn, and I can use it from within Python.

With regard to a minimal reproducible example: in python, let's use the iris dataset. I have the slope 1 and slope 2 of -0.2 and 1 and an intercept of 0. Here is the ggplot code I have for plotting the first slope, but not being able to plot the second:

from plotnine import ggplot, aes, geom_point, geom_histogram, geom_abline


ggplot(iris_df_feat_13, aes(x="sepal_width", y="petal_width", color="target")) + geom_point()+ geom_abline(intercept = 0, slope = -.2, color="red", 
                 linetype="dashed", size=1.5)
iris = datasets.load_iris() 
x = iris.data
y = iris.target
y = pd.Series(y, name = 'target')

iris_df = pd.DataFrame(x, columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
iris_df = iris_df.join(y) #remember this.
vals = [0,1]
iris_df_01 = iris_df.query('target == @ vals')
iris_df_01[['target']] = iris_df_01[['target']].replace(0, -1)
iris_df_feat_13 = iris_df_01[['sepal_width', 'petal_width', 'target']]
iris_df_feat_13
4
  • Your 2 parameter model is not a line but a plane in 3D space thus it is beyond the intent of "abline". Other packages better designed for 3D graphics could do a better job of handling multi dimensional regressions. Commented May 10, 2022 at 23:36
  • Right, but it would still handle a multiple regression with several coefficients, no (if you just passed in the model <- lm(dv ~ var1 + var2 + ...)? Commented May 10, 2022 at 23:56
  • You may find the sjPlot package useful for plotting regression models. Commented May 11, 2022 at 0:20
  • Yes, I love the sjPlot package. I've only used it with forrest/coefficient plots. How else have you used it? Commented May 11, 2022 at 21:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.