Plotting
The module provides methods to perform visual analytics. It provides methods to create the following plots:
- Bar plot
- Line plot
- Pie plot
Accessor
Initialise the DataFrame with the plotting method. Minimal working example:
df.crm.plotting()
Returns:
Type | Description |
---|---|
Plotting
|
Returns a class called "Plotting" providing plotting analytics methods. |
Methods
plot_bar(x, y, y_2=None, pct=True, barh=False, stacked=False, switch_order=False, sort_by_col=None, sort_by_list=None, sort_asc=True, title=None, x_axis_label='Category', x_tick_labels=None, x_tick_rot=0, x_color=cfg.COLOUR_BLACK, y_axis_label='Per Cent', y_lim_min=None, y_lim_max=None, y_lim_rounded=True, y_tick_spacing=None, y_tick_digits=0, y_tick_rot=0, y_color=cfg.COLOUR_BLACK, color=None, legend=True, legend_label=None, legend_loc='upper right', fig_size=(10, 6.18), path=None, show=False)
Minimal working example:
df.crm.plotting().plot_bar(x="DATE", y="SCORE")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
str
|
Defines the values to plot on the x-axis. Use ".reset_index()" to access the index of the DataFrame since the index itself is not directly accessible. |
required |
y
|
Union[str, list]
|
Defines the values to plot on the y-axis. |
required |
y_2
|
Union[str, list]
|
Defines additional values to be plotted on the y-axis (with transparency). |
None
|
pct
|
bool
|
Defines whether the y-axis is in percentage or absolute scale. |
True
|
barh
|
bool
|
Defines whether the bar plot is horizontal or not. |
False
|
stacked
|
bool
|
Defines whether the bar plot is stacked or not. |
False
|
switch_order
|
bool
|
Defines whether to switch the order of the bars within one tick. |
False
|
sort_by_col
|
str
|
Defines the column to sort. |
None
|
sort_by_list
|
list
|
Defines the list to sort in case of categorical items which have no intrinsic sorting order. |
None
|
sort_asc
|
bool
|
Sorts the previously defined column or list ascending. |
True
|
title
|
str
|
Defines the title. |
None
|
x_axis_label
|
str
|
Defines the x-axis label. |
'Category'
|
x_tick_labels
|
Union[str, list]
|
Defines the x-tick labels. It can be an explicit list of labels or, in case of a date axis, a format code based on the 1989 C standard such as "%b %Y". |
None
|
x_tick_rot
|
int
|
Defines the x-tick labels rotation. |
0
|
x_color
|
str
|
Defines the color of the x-ticks, the x-tick labels, and the x-axis label. |
COLOUR_BLACK
|
y_axis_label
|
str
|
Defines the y-axis label. |
'Per Cent'
|
y_lim_min
|
Union[float, Timestamp, date]
|
Defines the y-axis minimum limit. |
None
|
y_lim_max
|
Union[float, Timestamp, date]
|
Defines the y-axis maximum limit. |
None
|
y_lim_rounded
|
bool
|
Defines whether the y-axis limits should be rounded. This only works for limits which are not explicitly defined via "y_lim_min" or "y_lim_max". The values are rounded to the next order of magnitude or zero if appropriate. |
True
|
y_tick_spacing
|
float
|
Defines the y-tick spacing in absolute values. |
None
|
y_tick_digits
|
int
|
Defines the y-tick digits. |
0
|
y_tick_rot
|
int
|
Defines the x-tick labels rotation. |
0
|
y_color
|
str
|
Defines the color of the y-ticks, the y-tick labels, and the y-axis label. |
COLOUR_BLACK
|
color
|
Union[str, list]
|
Defines the colour of the bars. Defaults to standard colour palette. |
None
|
legend
|
bool
|
Defines whether a legend is plotted or not. |
True
|
legend_label
|
Union[str, list]
|
Defines the legend label. |
None
|
legend_loc
|
str
|
Defines the legend location. |
'upper right'
|
fig_size
|
tuple
|
Defines the figure size. |
(10, 6.18)
|
path
|
str
|
Defines the saving path including the filename of the figure and should be entered as r"C:\<path>\<filename>.<image_format>". |
None
|
show
|
bool
|
If True, plot is shown. |
False
|
Returns:
Type | Description |
---|---|
BytesIO
|
Returns a bar plot. |
plot_line(x, y, y_2=None, pct=True, sort_by_col=None, sort_by_list=None, sort_asc=True, title=None, x_axis_label='Category', x_tick_labels=None, x_tick_rot=0, x_color=cfg.COLOUR_BLACK, y_axis_label='Per Cent', y_lim_min=None, y_lim_max=None, y_lim_rounded=True, y_tick_spacing=None, y_tick_digits=0, y_tick_rot=0, y_color=cfg.COLOUR_BLACK, color=None, linewidth=None, linestyle=None, marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None, markerfacecolor=None, vline=None, legend=True, legend_label=None, legend_loc='upper right', fig_size=(10, 6.18), path=None, show=False)
Minimal working example:
df.crm.plotting().plot_line(x="DATE", y="SCORE")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
str
|
Defines the values to plot on the x-axis. Use ".reset_index()" to access the index of the DataFrame since the index itself is not directly accessible. |
required |
y
|
Union[str, list]
|
Defines the values to plot on the y-axis. |
required |
y_2
|
Union[str, list]
|
Defines additional values to be plotted on the y-axis (with transparency). |
None
|
pct
|
bool
|
Defines whether the y-axis is in percentage or absolute scale. |
True
|
sort_by_col
|
str
|
Defines the column to sort. |
None
|
sort_by_list
|
list
|
Defines the list to sort in case of categorical items which have no intrinsic sorting order. |
None
|
sort_asc
|
bool
|
Sorts the previously defined column or list ascending. |
True
|
title
|
str
|
Defines the title. |
None
|
x_axis_label
|
str
|
Defines the x-axis label. |
'Category'
|
x_tick_labels
|
Union[str, list]
|
Defines the x-tick labels. It can be an explicit list of labels or, in case of a date axis, a format code based on the 1989 C standard such as "%b %Y". |
None
|
x_tick_rot
|
int
|
Defines the x-tick labels rotation. |
0
|
x_color
|
str
|
Defines the color of the x-ticks, the x-tick labels, and the x-axis label. |
COLOUR_BLACK
|
y_axis_label
|
str
|
Defines the y-axis label. |
'Per Cent'
|
y_lim_min
|
Union[float, Timestamp, date]
|
Defines the y-axis minimum limit. |
None
|
y_lim_max
|
Union[float, Timestamp, date]
|
Defines the y-axis maximum limit. |
None
|
y_lim_rounded
|
bool
|
Defines whether the y-axis limits should be rounded. This only works for limits which are not explicitly defined via "y_lim_min" or "y_lim_max". The values are rounded to the next order of magnitude or zero if appropriate. |
True
|
y_tick_spacing
|
float
|
Defines the y-tick spacing in absolute values. |
None
|
y_tick_digits
|
int
|
Defines the y-tick digits. |
0
|
y_tick_rot
|
int
|
Defines the x-tick labels rotation. |
0
|
y_color
|
str
|
Defines the color of the y-ticks, the y-tick labels, and the y-axis label. |
COLOUR_BLACK
|
color
|
Union[str, list]
|
Defines the colour of the lines. Defaults to standard colour palette. |
None
|
linewidth
|
float
|
Defines the line width. |
None
|
linestyle
|
str
|
Defines the line style. |
None
|
marker
|
str
|
Defines the marker. |
None
|
markersize
|
float
|
Defines the marker size. |
None
|
markeredgewidth
|
float
|
Defines the marker edge width. |
None
|
markeredgecolor
|
str
|
Defines the marker edge color. |
None
|
markerfacecolor
|
str
|
Defines the marker face color. |
None
|
vline
|
Union[int, float]
|
Adds a dashed vertical line at position x = vline. |
None
|
legend
|
bool
|
Defines whether a legend is plotted or not. |
True
|
legend_label
|
Union[str, list]
|
Defines the legend label. |
None
|
legend_loc
|
str
|
Defines the legend location. |
'upper right'
|
fig_size
|
tuple
|
Defines the figure size. |
(10, 6.18)
|
path
|
str
|
Defines the saving path including the filename of the figure and should be entered as r"C:\<path>\<filename>.<image_format>". |
None
|
show
|
bool
|
If True, plot is shown. |
False
|
Returns:
Type | Description |
---|---|
BytesIO
|
Returns a line plot. |
plot_pie(x, y, sort_by_col=None, sort_by_list=None, sort_asc=True, title=None, x_tick_labels=None, explode=None, color=None, autopct='%1.1f%%', autocolor=cfg.COLOUR_WHITE, startangle=90, radius=1, fig_size=(10, 6.18), path=None, show=False)
Minimal working example:
df.crm.plotting().plot_pie(x="DATE", y="SCORE")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
str
|
Defines the values to plot on the x-axis. Use ".reset_index()" to access the index of the DataFrame since the index itself is not directly accessible. |
required |
y
|
Union[str, list]
|
Defines the values to plot on the y-axis. |
required |
sort_by_col
|
str
|
Defines the column to sort. |
None
|
sort_by_list
|
list
|
Defines the list to sort in case of categorical items which have no intrinsic sorting order. |
None
|
sort_asc
|
bool
|
Sorts the previously defined column or list ascending. |
True
|
title
|
str
|
Defines the title. |
None
|
x_tick_labels
|
Union[str, list]
|
Defines the x-tick labels. It can be an explicit list of labels or, in case of a date axis, a format code based on the 1989 C standard such as "%b %Y". |
None
|
explode
|
tuple
|
Defines the fraction of the radius with which to offset each wedge. |
None
|
color
|
Union[str, list]
|
Defines the colour of the lines. Defaults to standard colour palette. |
None
|
autopct
|
str
|
Defines the format of the labels inside the wedges. |
'%1.1f%%'
|
autocolor
|
str
|
Defines the colour of the wedges. Defaults to the standard colour palette. |
COLOUR_WHITE
|
startangle
|
int
|
Defines the angle by which the start of the pie is rotated, counterclockwise from the x-axis. |
90
|
radius
|
float
|
Defines the radius of the pie. |
1
|
fig_size
|
tuple
|
Defines the figure size. |
(10, 6.18)
|
path
|
str
|
Defines the saving path including the filename of the figure and should be entered as r"C:\<path>\<filename>.<image_format>". |
None
|
show
|
bool
|
If True, plot is shown. |
False
|
Returns:
Type | Description |
---|---|
BytesIO
|
Returns a pie plot. |
Examples
>>> import credit_risk_modelling as crm
>>> data = (
>>> crm.load_data.load_data()
>>> .crm.frequency(index="DATE", column="GRADE")
>>> .table()
>>> .iloc[:, 0:2]
>>> .set_axis(["DATE", "COUNT"], axis=1)
>>> )
>>> data
DATE COUNT
0 2019-12-31 83
1 2020-12-31 90
2 2021-12-31 84
3 2022-12-31 118
4 2023-12-31 128
>>> (
>>> data
>>> .crm.plotting().plot_bar(
>>> x="DATE",
>>> y="COUNT",
>>> pct=False,
>>> x_axis_label="Date",
>>> x_tick_labels="%d %B %Y",
>>> y_axis_label="Count",
>>> legend_label="Count",
>>> show=True
>>> )
>>> )
>>> (
>>> data
>>> .crm.plotting().plot_line(
>>> x="DATE",
>>> y="COUNT",
>>> pct=False,
>>> x_axis_label="Date",
>>> x_tick_labels="%d %B %Y",
>>> y_axis_label="Count",
>>> legend_label="Count",
>>> marker="o",
>>> show=True
>>> )
>>> )
>>> (
>>> data
>>> .crm.plotting().plot_pie(
>>> x="DATE",
>>> y="COUNT",
>>> x_tick_labels="%d %B %Y",
>>> explode=(0.05,) * 5,
>>> autocolor="black",
>>> show=True
>>> )
>>> )