Violin
- D3Blocks.violin(x, y, size=5, color=None, x_order=None, opacity=0.6, stroke='#000000', tooltip=None, fontsize=12, fontsize_axis: int = 12, cmap: str = 'inferno', bins: int = 50, ylim=[None, None], title: str = 'Violin - D3blocks', filepath='violin.html', figsize=[None, None], showfig: bool = True, overwrite: bool = True, notebook: bool = False, save_button: bool = True, return_html: bool = False, reset_properties: bool = True)
Violin block.
The Violin plot allows to visualize the distribution of a numeric variable for one or several groups. It is an alternative to the boxplot and brings insights into large datasets where the boxplot could hide a part of the information. The original javascript code is forked from D3.js graph gallery but brought alive by Pythonizing the chart. Now it is possible to configure your charts for one or several groups, change colors, add tooltips, customize the bin size, change figure size and store on a specified location. There are many input parameters for the Violin plot that can help to create the most insightful figures.
- Parameters:
x (list of String or numpy array.) – This 1d-vector contains the class labels for each datapoint in y.
y (list of float or numpy array.) – This 1d-vector contains the values for the samples.
size (list/array of with same size as (x,y). Can be of type str or int.) – Size of the samples.
color (list/array of hex colors with same size as y) –
‘#002147’ : All dots/nodes are get the same hex color.
None: The colors are generated on value using the colormap specified in cmap.
[‘#000000’, ‘#ffffff’,…]: list/array of hex colors with same size as y.
x_order (list of String (default: None)) –
- The order of the class labels on the x-axis.
[“setosa”, “versicolor”, “virginica”]
opacity (float or list/array [0-1] (default: 0.6)) – Opacity of the dot. Shoud be same size as (x,y)
stroke (list/array of hex colors with same size as (x,y)) –
- Edgecolor of dot in hex colors.
’#000000’ : Edge colors are all black.
tooltip (list of labels with same size as (x,y)) – labels of the samples.
fontsize (int or list of labels with the same size as (x, y), optional (default: 12)) – Text fontsize for the tooltip.
fontsize_axis (int, optional (default: 12)) – Text fontsize for the x-axis and y-axis.
cmap (String, (default: 'inferno')) –
- All colors can be reversed with ‘_r’, e.g. ‘binary’ to ‘binary_r’
’tab20c’, ‘Set1’, ‘Set2’, ‘rainbow’, ‘bwr’, ‘binary’, ‘seismic’, ‘Blues’, ‘Reds’, ‘Pastel1’, ‘Paired’, ‘twilight’, ‘hsv’
bins (Int (default: 50)) – The bin size is the ‘resolution’ of the violin plot.
ylim (tuple, (default: [None, None])) –
- Limit the width of the y-axis [min, max].
[None, None] : The width is determined based on the min-max value range.
title (String, (default: None)) –
- Title of the figure.
’Violin Chart’
filepath (String, (Default: user temp directory)) –
- File path to save the output.
Temporarily path: ‘Violin.html’
Relative path: ‘./Violin.html’
Absolute path: ‘c://temp//Violin.html’
None: Return HTML content.
figsize (tuple) –
- Size of the figure in the browser, [width, height].
[None, None]: The width is auto-determined based on the #labels.
showfig (bool, (default: True)) –
True: Open browser-window.
False: Do not open browser-window.
overwrite (bool, (default: True)) –
True: Overwrite the html in the destination directory.
False: Do not overwrite destination file but show warning instead.
notebook (bool) –
True: Use IPython to show chart in notebook.
False: Do not use IPython.
save_button (bool, (default: True)) –
True: Save button is shown in the HTML to save the image in svg.
False: No save button is shown in the HTML.
return_html (bool, (default: False)) –
True: Return html
False: Nothing is returned
reset_properties (bool, (default: True)) –
True: Reset the node_properties at each run.
False: Use the d3.node_properties()
- Returns:
d3.node_properties – Contains properties of the unique input label/nodes/samples.
- Return type:
DataFrame of dictionary
- d3.edge_properties: DataFrame of dictionary
Contains properties of the unique input edges/links.
- d3.config: dictionary
Contains configuration properties.
Examples
>>> # Load d3blocks >>> from d3blocks import D3Blocks >>> # >>> # Initialize >>> d3 = D3Blocks() >>> # >>> # Import example dataset >>> df = d3.import_example('cancer') >>> # >>> # Set some input variables. >>> tooltip = df['labx'].values + ' <br /> Survival: ' + df['survival_months'].astype(str).values >>> fontsize = df['age'].values >>> # fontsize = 16 # Set one fontsize for all nodes >>> # >>> # Create the chart >>> d3.violin(x=df['labx'].values, y=df['age'].values, fontsize=fontsize, tooltip=tooltip, bins=50, size=df['survival_months'].values/10, x_order=['acc','kich', 'brca','lgg','blca','coad','ov'], filepath='violine.html', figsize=[900, None]) >>> #
Examples
>>> # Load d3blocks >>> from d3blocks import D3Blocks >>> # >>> # Initialize for the Violin chart and set output to Frame. >>> d3 = D3Blocks(chart='Violin', frame=True) >>> # >>> # Import example dataset >>> df = d3.import_example('cancer') >>> # >>> # Set the properties by providing the labels >>> d3.set_edge_properties(x=df['labx'].values, y=df['age'].values, size=df['survival_months'].values/10, x_order=['acc','kich', 'brca','lgg','blca','coad','ov']) >>> # >>> # Set specific node properties. >>> d3.edge_properties.loc[0,'size']=50 >>> d3.edge_properties.loc[0,'color']='#000000' >>> d3.edge_properties.loc[0,'tooltip']='I am adjusted!' >>> d3.edge_properties.loc[0,'fontsize']=30 >>> # >>> # Configuration can be changed too. >>> print(d3.config) >>> # >>> # Show the chart >>> d3.show()
References
Input Data
The input dataset are the x-coordinates and y-coordinates that needs to be specified seperately.
# x y age ... labels
# labels ...
# acc 37.204296 24.162813 58.0 ... acc
# acc 37.093090 23.423557 44.0 ... acc
# acc 36.806297 23.444910 23.0 ... acc
# acc 38.067886 24.411770 30.0 ... acc
# acc 36.791195 21.715324 29.0 ... acc
# ... ... ... ... ...
# brca 0.839383 -8.870781 NaN ... brca
# brca -5.842904 2.877595 NaN ... brca
# brca -9.392038 1.663352 71.0 ... brca
# brca -4.016389 6.260741 NaN ... brca
# brca 0.229801 -8.227086 NaN ... brca
# [4674 rows x 9 columns]