Interactive Data Visualization became much easier with help of Plotly-express.
By: Chamanth Mvs
Tags: Plotly Express, Plotly, Data Visualization, Python

Difference between Plotly and Plotly-express (in terms of plotting).

Plotly

Please Note: Plotly has been updated recently. Plotly as well as any sources from plotly updates frequently (as they are new libraries when compared to other libraries in python).

Till Plotly 3, we have two modes in plotting with plotly (Online and Offline).

Plotly online

When plotting online, plot and data will be saved to your plotly’s cloud account. There are two methods to plot online. plotly.plot() — used to return the unique URL and optionally open the URL. plotly.iplot() — used when working in jupyter-notebook, to display the plot within the notebook. These both methods create a unique URL for the plot and save it in your plotly account and internet connection is required to use plotly online.

Plotly offline

Plotly offline, allows to create plots offline and save them locally (which doesn’t require any internet connection).There are two methods to plot offline. plotly.offline.plot() — used to create a standalone HTML, that is saved locally and opened inside your web browser. plotly.offline.iplot() — used when working offline in jupyter-notebook, to display the plot in the notebook. When we intend to use plotly.offline.iplot(), we need to run an additional step, i.e., plotly.offline.init_notebook_mode() at start of each session.

From Plotly 4, (which is updated and recent version of Plotly)

Fig-1 : plotly3_vs_4

Plotly 4 made life much easier as it is completely offline (So, there is NO plotly online from plotly 4).

Whoever loves to work with plotly.offline in jupyter-notebook, they can avoid connection statement in their code (which includes connecting plotly in offline mode to their notebook ), now they can directly import plotly rather than importing plotly.offline.

plotly.graph_objs — This has several functions, which is useful in generating graph objects. grpah_objs — It is a class, which contains several structures that are consistent across visualizations made in python regardless of type.

From plotly 3 to plotly 4, plotly.graph_objs package has been aliased as plotly.graph_objects “because the latter is much easier to communicate verbally “— according to official documentation.

Plotly Express

Fig-2:Importing plotly express (careful with versions)

Plotly Express was separately-installed using plotly_express package but it is now part of plotly. Plotly should be updated to plotly 4 before using it Or you will be encountered with error as shown in Fig-2.

Comparing Scatter plot with plotly and plotly express

Scatter plot allows the comparison of two variables for a set of data. Depending on the trend of the scatter plot, we could interpret a correlation.

With Plotly:

Fig-3:plotting between sepal_length and sepal_width

Plotly follows a particular syntax as seen in Fig-3. Initially, a variable is to be created to assign a plot (Note:plot type should be given in the form of list, as shown in first line in Fig-3). In this case, we named it “data” (most followed notation), variable name can be of your choice. This “data” variable contains a plot type call. go.Scatter is one among many graph objects, each plot type has it’s own graph object. These objects typically accepts few parameters. For instance, scatter graph objects requires two mandatory parameters (assigning x-axis and y-axis). go.Layout(this is also one of the graph object), which is used to define layout for the plot. Then figure object from graph objects, is created to use both data and layout variables to plot.

The dataset I have used is very famous and best known database to be found in the pattern recognition literature .This dataset contains 3 classes(Setosa, Versicolour, Virginica) of 50 instances each, where each class refers to a type of iris plant. In Fig-3, we have plotted all classes to find relation between (Sepal_length and Sepal_width). But, as all the datapoints are represented in same color, we are unable to draw any conclusions from the plot because plotly doesn’t give you hue in a plot (which is a parameter in seaborn). So, alternative to it is to either data should be plotted using grouped-by method or individual traces should be created for each class variables.

Grouping by data to plot with variation in each class