Plotly Express

Plotly Express#

  • A high-level interface for creating visualizations with Plotly.

  • Simplifies the process of creating complex visualizations.

  • website

import plotly.express as px

Line Plot#

  • A simple line plot using Plotly Express.

  • the positional axis order is different from Matplotlib

fig = px.line([1, 2, 3, 4], [1, 4, 2, 3])
fig

Just be explicit, which is always a good idea!

fig = px.line(x=[1, 2, 3, 4], y=[1, 4, 2, 3])

The plotly Figure object offers many methods to update the Figure. There is no separate axes object. The underlying data can be seen as a dictionary containing data and the layout to apply (as a json).

dir(fig)

Hide code cell output

['__class__',
 '__contains__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_add_annotation_like',
 '_allow_disable_validation',
 '_animation_duration_validator',
 '_animation_easing_validator',
 '_batch_layout_edits',
 '_batch_trace_edits',
 '_bracket_re',
 '_build_dispatch_plan',
 '_build_update_params_from_batch',
 '_config',
 '_data',
 '_data_defaults',
 '_data_objs',
 '_data_validator',
 '_dispatch_layout_change_callbacks',
 '_dispatch_trace_change_callbacks',
 '_filter_by_selector',
 '_frame_objs',
 '_frames_validator',
 '_get_child_prop_defaults',
 '_get_child_props',
 '_get_subplot_coordinates',
 '_get_subplot_rows_columns',
 '_grid_ref',
 '_grid_str',
 '_has_subplots',
 '_in_batch_mode',
 '_index_is',
 '_init_child_props',
 '_initialize_layout_template',
 '_ipython_display_',
 '_is_dict_list',
 '_is_key_path_compatible',
 '_layout',
 '_layout_defaults',
 '_layout_obj',
 '_layout_validator',
 '_make_axis_spanning_layout_object',
 '_normalize_trace_indexes',
 '_perform_batch_animate',
 '_perform_plotly_relayout',
 '_perform_plotly_restyle',
 '_perform_plotly_update',
 '_perform_select_traces',
 '_perform_update',
 '_process_multiple_axis_spanning_shapes',
 '_px_trendlines',
 '_raise_invalid_rows_cols',
 '_relayout_child',
 '_repr_html_',
 '_repr_mimebundle_',
 '_restyle_child',
 '_select_annotations_like',
 '_select_layout_subplots_by_prefix',
 '_select_subplot_coordinates',
 '_selector_matches',
 '_send_addTraces_msg',
 '_send_animate_msg',
 '_send_deleteTraces_msg',
 '_send_moveTraces_msg',
 '_send_relayout_msg',
 '_send_restyle_msg',
 '_send_update_msg',
 '_set_in',
 '_set_property',
 '_set_trace_grid_position',
 '_set_trace_uid',
 '_str_to_dict_path',
 '_subplot_not_empty',
 '_to_ordered_dict',
 '_valid_underscore_properties',
 '_validate',
 '_validate_get_grid_ref',
 '_validate_rows_cols',
 'add_annotation',
 'add_bar',
 'add_barpolar',
 'add_box',
 'add_candlestick',
 'add_carpet',
 'add_choropleth',
 'add_choroplethmap',
 'add_choroplethmapbox',
 'add_cone',
 'add_contour',
 'add_contourcarpet',
 'add_densitymap',
 'add_densitymapbox',
 'add_funnel',
 'add_funnelarea',
 'add_heatmap',
 'add_histogram',
 'add_histogram2d',
 'add_histogram2dcontour',
 'add_hline',
 'add_hrect',
 'add_icicle',
 'add_image',
 'add_indicator',
 'add_isosurface',
 'add_layout_image',
 'add_mesh3d',
 'add_ohlc',
 'add_parcats',
 'add_parcoords',
 'add_pie',
 'add_sankey',
 'add_scatter',
 'add_scatter3d',
 'add_scattercarpet',
 'add_scattergeo',
 'add_scattergl',
 'add_scattermap',
 'add_scattermapbox',
 'add_scatterpolar',
 'add_scatterpolargl',
 'add_scattersmith',
 'add_scatterternary',
 'add_selection',
 'add_shape',
 'add_splom',
 'add_streamtube',
 'add_sunburst',
 'add_surface',
 'add_table',
 'add_trace',
 'add_traces',
 'add_treemap',
 'add_violin',
 'add_vline',
 'add_volume',
 'add_vrect',
 'add_waterfall',
 'append_trace',
 'batch_animate',
 'batch_update',
 'data',
 'for_each_annotation',
 'for_each_coloraxis',
 'for_each_geo',
 'for_each_layout_image',
 'for_each_legend',
 'for_each_map',
 'for_each_mapbox',
 'for_each_polar',
 'for_each_scene',
 'for_each_selection',
 'for_each_shape',
 'for_each_smith',
 'for_each_ternary',
 'for_each_trace',
 'for_each_xaxis',
 'for_each_yaxis',
 'frames',
 'full_figure_for_development',
 'get_subplot',
 'layout',
 'plotly_relayout',
 'plotly_restyle',
 'plotly_update',
 'pop',
 'print_grid',
 'select_annotations',
 'select_coloraxes',
 'select_geos',
 'select_layout_images',
 'select_legends',
 'select_mapboxes',
 'select_maps',
 'select_polars',
 'select_scenes',
 'select_selections',
 'select_shapes',
 'select_smiths',
 'select_ternaries',
 'select_traces',
 'select_xaxes',
 'select_yaxes',
 'set_subplots',
 'show',
 'to_dict',
 'to_html',
 'to_image',
 'to_json',
 'to_ordered_dict',
 'to_plotly_json',
 'update',
 'update_annotations',
 'update_coloraxes',
 'update_geos',
 'update_layout',
 'update_layout_images',
 'update_legends',
 'update_mapboxes',
 'update_maps',
 'update_polars',
 'update_scenes',
 'update_selections',
 'update_shapes',
 'update_smiths',
 'update_ternaries',
 'update_traces',
 'update_xaxes',
 'update_yaxes',
 'write_html',
 'write_image',
 'write_json']

Explore the most common function types intended for users using the first word as the indicator

from collections import Counter

items = Counter([x.split("_")[0] for x in dir(fig) if x.split("_")[0]])
items
Counter({'add': 59,
         'update': 18,
         'for': 16,
         'select': 16,
         'to': 6,
         'plotly': 3,
         'write': 3,
         'batch': 2,
         'append': 1,
         'data': 1,
         'frames': 1,
         'full': 1,
         'get': 1,
         'layout': 1,
         'pop': 1,
         'print': 1,
         'set': 1,
         'show': 1})

Proteomics data example#

import pathlib

import pandas as pd

dir_data = pathlib.Path("data")
df = pd.read_csv(dir_data / "proteins" / "proteins.csv", index_col=0).T
df
Reference DMSO_rep1 DMSO_rep2 DMSO_rep3 DMSO_rep4 Suf_rep1 Suf_rep2 Suf_rep3 Suf_rep4
A5A613 27.180209 NaN NaN NaN NaN NaN NaN NaN
P00350 28.151576 27.926204 27.653250 27.151643 27.441837 27.031610 27.814631 27.587217
P00363 30.247131 30.261665 29.969625 29.470663 30.004725 30.085997 29.904057 29.575194
P00370 27.459171 26.873349 26.599971 26.438623 27.399691 27.189188 27.139030 27.223715
P00393 26.823758 26.756617 25.442346 25.798954 26.671118 26.885970 26.711192 26.320866
... ... ... ... ... ... ... ... ...
Q57261 28.410859 27.940694 27.070328 26.679649 27.995432 27.055135 27.313219 26.643479
Q59385-2 23.554913 25.240354 NaN 22.524292 NaN NaN NaN NaN
Q59385 27.640279 27.243650 27.525020 27.403753 27.498873 27.666957 27.708407 27.847610
Q7DFV3 28.512794 27.620780 27.678892 27.255831 28.090220 27.525537 27.814369 27.605449
Q93K97 27.223010 25.291110 24.358694 25.767196 25.956190 25.230565 26.103059 26.177716

2269 rows × 8 columns

x = df.iloc[:, 0]

px.histogram(x)
px.histogram(df)