Beeswarm
daspi.plotlib.plotter.Beeswarm(source, target, feature='', n_bins=None, width=CATEGORY.FEATURE_SPACE, skip_na=None, target_on_y=True, color=None, marker=None, ax=None, visible_spines=None, hide_axis=None, **kwds)
¶
Bases: TransformPlotter
A class to create and display a basic bee swarm plot.
This class includes methods that organize the input data into bins according to a specified number of bins (or a default value if none is provided). It calculates the upper limits for each bin and positions the data points within these bins to achieve a horizontal distribution in the plot, ensuring as little overlap as possible among the points.
| 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:
|
n_bins
|
The number of bins to divide the data into. If not specified, it is calculated as the length of the data divided by 6. Defaults to None
TYPE:
|
width
|
The width of the beeswarm, by default
TYPE:
|
skip_na
|
Flag indicating whether to skip missing values in the feature grouped data, by default None - None, no missing values are skipped - all', grouped data is skipped if all values are missing - any', grouped data is skipped if any value is missing
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:
|
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 Beeswarm
fig, ax = plt.subplots()
df = pd.DataFrame(dict(
x = ['first'] * 50 + ['second'] * 50 + ['third'] * 50,
y = (
list(np.random.normal(loc=3, scale=1, size=50))
+ list(np.random.normal(loc=4, scale=1, size=50))
+ list(np.random.normal(loc=2, scale=1, size=50)))))
beeswarm = Beeswarm(
source=df, target='y', feature='x', ax=ax)
beeswarm()
beeswarm.label_feature_ticks()
Apply using the plot method of a DaSPi Chart object:
import numpy as np
import daspi as dsp
import pandas as pd
df = pd.DataFrame(dict(
x = ['first'] * 50 + ['second'] * 50 + ['third'] * 50,
y = (
list(np.random.normal(loc=3, scale=1, size=50))
+ list(np.random.normal(loc=4, scale=1, size=50))
+ list(np.random.normal(loc=2, scale=1, size=50)))))
chart = dsp.SingleChart(
source=df,
target='y',
feature='x',
categorical_feature=True # neded to label feature ticks
).plot(
dsp.Beeswarm,
).label() # neded to label feature ticks
Source
This code is based on the following source: https://python-graph-gallery.com/509-introduction-to-swarm-plot-in-matplotlib/
n_bins = n_bins
instance-attribute
¶
The number of bins to divide the data into
width = width
instance-attribute
¶
The maximum width of the beeswarm.
kw_default
property
¶
Default keyword arguments for plotting (read-only)
transform(feature_data, target_data)
¶
Generates the spread values for the beeswarm plot by arranging the target data into bins.
The method divides the input data into bins based on the specified number of bins and calculates the spread of values within each bin to create a horizontal distribution.
| PARAMETER | DESCRIPTION |
|---|---|
feature_data
|
The center position on the feature axis where the beeswarm values will be centered.
TYPE:
|
target_data
|
feature grouped target data, coming from `feature_grouped' generator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
data
|
The transformed data source for the plot.
TYPE:
|