Linear regression line
daspi.plotlib.plotter.LinearRegressionLine(source, target, feature, target_on_y=True, color=None, marker=None, show_scatter=True, show_fit_ci=True, show_pred_ci=False, ax=None, visible_spines=None, hide_axis=None, **kwds)
¶
Bases: Plotter
A linear regression plotter that extends the Plotter base class.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Pandas long format DataFrame containing the data source for the plot.
TYPE:
|
target
|
Column name of the target variable for the plot.
TYPE:
|
feature
|
Column name of the feature variable for the plot, by default ''
TYPE:
|
target_on_y
|
Flag indicating whether the target variable is plotted on the y-axis, by default True
TYPE:
|
color
|
Color to be used to draw the artists. If None, the first color is taken from the color cycle, by default None.
TYPE:
|
marker
|
The marker style for the scatter plot. Available markers see: https://matplotlib.org/stable/api/markers_api.html, by default None
TYPE:
|
show_scatter
|
Flag indicating whether to show the individual points, by default True.
TYPE:
|
show_fit_ci
|
Flag indicating whether to show the confidence interval for the fitted line as filled area, by default True.
TYPE:
|
show_pred_ci
|
Flag indicating whether to show the confidence interval for predictions as additional lines, by default False
TYPE:
|
ax
|
The axes object for the plot. If None, the current axes is
fetched using
TYPE:
|
visible_spines
|
Specifies which spines are visible, the others are hidden. If 'none', no spines are visible. If None, the spines are drawn according to the stylesheet. Defaults to None.
TYPE:
|
hide_axis
|
Specifies which axes should be hidden. If None, both axes are displayed. Defaults to None.
TYPE:
|
**kwds
|
Those arguments have no effect. Only serves to catch further arguments that have no use here (occurs when this class is used within chart objects).
DEFAULT:
|
Examples:
Apply to an existing Axes object:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from daspi import LinearRegressionLine
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
df = pd.DataFrame(dict(
x=x,
y=x + np.random.randn(100) - 0.05 * np.square(x)))
reg_line = LinearRegressionLine(
source=df, target='y', feature='x', ax=ax,
show_scatter=True, show_fit_ci=True, show_pred_ci=True)
reg_line(
kw_scatter=dict(color='black', s=10, alpha=0.5),
kw_fit_ci=dict(color='skyblue'),
kw_pred_ci=dict(color='steelblue', alpha=1),
color='deepskyblue')
Apply using the plot method of a DaSPi Chart object
import numpy as np
import daspi as dsp
import pandas as pd
x = np.linspace(0, 10, 100)
df = pd.DataFrame(dict(
x=x,
y=x + np.random.randn(100) - 0.05 * np.square(x)))
chart = dsp.SingleChart(
source=df,
target='y',
feature='x'
).plot(
dsp.LinearRegressionLine,
show_scatter=True,
show_fit_ci=True,
show_pred_ci=True,
kw_call=dict(
kw_scatter=dict(color='black', s=10, alpha=0.5),
kw_fit_ci=dict(color='skyblue'),
kw_pred_ci=dict(color='steelblue', alpha=1),
color='deepskyblue'))
fit = PLOTTER.FITTED_VALUES
instance-attribute
¶
The name of the column containing the fitted values as defined in the PLOTTER.FITTED_VALUES.
show_scatter = show_scatter
instance-attribute
¶
Whether to show the individual points in the plot.
show_fit_ci = show_fit_ci
instance-attribute
¶
Whether to show the confidence interval for the fitted line.
show_pred_ci = show_pred_ci
instance-attribute
¶
Whether to show the confidence interval for the predictions.
model = sm.OLS(df[target], sm.add_constant(df[feature])).fit()
instance-attribute
¶
The fitted results of the linear regression model.
kw_default
property
¶
Default keyword arguments for plotting (read-only)
x_fit
property
¶
Get values used for x-axis for fitted line (read-only).
y_fit
property
¶
Get values used for y-axis for fitted line (read-only)
ci_data()
¶
Get confidence interval for prediction and fitted line as DataFrame.