Violin

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.

param x:

This 1d-vector contains the class labels for each datapoint in y.

type x:

list of String or numpy array.

param y:

This 1d-vector contains the values for the samples.

type y:

list of float or numpy array.

param size:

Size of the samples.

type size:

list/array of with same size as (x,y). Can be of type str or int.

param color:
  • ‘#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.

type color:

list/array of hex colors with same size as y

param x_order:
The order of the class labels on the x-axis.
  • [“setosa”, “versicolor”, “virginica”]

type x_order:

list of String (default: None)

param opacity:

Opacity of the dot. Shoud be same size as (x,y)

type opacity:

float or list/array [0-1] (default: 0.6)

param stroke:
Edgecolor of dot in hex colors.
  • ‘#000000’ : Edge colors are all black.

type stroke:

list/array of hex colors with same size as (x,y)

param tooltip:

labels of the samples.

type tooltip:

list of labels with same size as (x,y)

param fontsize:

Text fontsize for the tooltip.

type fontsize:

int or list of labels with the same size as (x, y), optional (default: 12)

param fontsize_axis:

Text fontsize for the x-axis and y-axis.

type fontsize_axis:

int, optional (default: 12)

param cmap:
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’

type cmap:

String, (default: ‘inferno’)

param bins:

The bin size is the ‘resolution’ of the violin plot.

type bins:

Int (default: 50)

param ylim:
Limit the width of the y-axis [min, max].
  • [None, None] : The width is determined based on the min-max value range.

type ylim:

tuple, (default: [None, None])

param title:
Title of the figure.
  • ‘Violin Chart’

type title:

String, (default: None)

param filepath:
File path to save the output.
  • Temporarily path: ‘Violin.html’

  • Relative path: ‘./Violin.html’

  • Absolute path: ‘c://temp//Violin.html’

  • None: Return HTML content.

type filepath:

String, (Default: user temp directory)

param figsize:
Size of the figure in the browser, [width, height].
  • [None, None]: The width is auto-determined based on the #labels.

type figsize:

tuple

param showfig:
  • True: Open browser-window.

  • False: Do not open browser-window.

type showfig:

bool, (default: True)

param overwrite:
  • True: Overwrite the html in the destination directory.

  • False: Do not overwrite destination file but show warning instead.

type overwrite:

bool, (default: True)

param notebook:
  • True: Use IPython to show chart in notebook.

  • False: Do not use IPython.

type notebook:

bool

param save_button:
  • True: Save button is shown in the HTML to save the image in svg.

  • False: No save button is shown in the HTML.

type save_button:

bool, (default: True)

param reset_properties:
  • True: Reset the node_properties at each run.

  • False: Use the d3.node_properties()

type reset_properties:

bool, (default: True)

returns:

d3.node_properties – Contains properties of the unique input label/nodes/samples.

rtype:

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]

Chart