Skip to content
Snippets Groups Projects
Commit 6d8f9941 authored by niklasfranz's avatar niklasfranz
Browse files

feat: introduced flexible x_ticks for boxplots

parent 84e4930e
No related branches found
No related tags found
1 merge request!22Resolve "flexible x-label-tick-counts for boxplots"
......@@ -198,7 +198,10 @@ class Plotter:
)
def plot_categorical_boxplot(
self, target: str, category: str, styling_params: dict = {}
self, target: str, category: str,
styling_params: dict = {},
x_tick_count: int = None,
x_tick_step: int = None
) -> None:
"""plot a categorical boxplot.
......@@ -206,6 +209,12 @@ class Plotter:
target (str, must be present as a column in the dataset),
category (str, must be present as a column in the dataset),
styling_params (dict)
x_tick_count: define the amount of x_ticks on the x axis.
cannot be used in combination with x_tick_step
should not be used with categorical values
x_tick_step: define the incremental steps of x_ticks values.
cannot be used in combination with x_tick_count
should not be used with categorical values
Returns:
......@@ -249,6 +258,32 @@ class Plotter:
)
raise ValueError("parameter styling params should be a dict.")
if (type(x_tick_count) != int) and (x_tick_count is not None):
logging.error(
"Plotter.plot_categorical_boxplot(): parameter \
x_tick_count should be an integer."
)
raise ValueError("parameter styling params should be a dict.")
if (type(x_tick_step) != int) and (x_tick_step is not None):
logging.error(
"Plotter.plot_categorical_boxplot(): parameter \
x_tick_step should be an integer."
)
raise ValueError("parameter styling x_tick_step should be an int.")
if x_tick_step is not None and x_tick_count is not None:
logging.error(
"Plotter.plot_categorical_boxplot(): parameters \
x_tick_step and x_tick_count cannot be used \
simultaneously. Use either one or the other, \
exclusively"
)
raise KeyError("Plotter.plot_categorical_boxplot(): parameters \
x_tick_step and x_tick_count cannot be used \
simultaneously. Use either one or the other, \
exclusively")
logging.debug(
"Plotter.plot_categorical_boxplot(): all params valid, \
plotting plot_categorical_boxplot(): \n \
......@@ -262,6 +297,25 @@ class Plotter:
self.customize_plot(fig, ax, styling_params)
sns.boxplot(x=category, y=target, data=self.df, palette="rainbow")
if x_tick_count or x_tick_step:
x_limits = ax.get_xlim()
upper_xlim = int(x_limits[1])
lower_xlim = int(x_limits[0])
if x_tick_count:
step = (upper_xlim - lower_xlim) // (x_tick_count - 1)
x_ticks = [int(lower_xlim + step * i) for i in
range(x_tick_count)]
elif x_tick_step:
step = x_tick_step
current_tick = lower_xlim
x_ticks = [current_tick]
while current_tick + step < upper_xlim:
x_ticks.append(current_tick + step)
current_tick += step
print(x_ticks)
plt.xticks(x_ticks)
def plot_categorical_histplot(
self,
target: str,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment