Mark As Completed Discussion

Plotly

Plotly is a plotting/data visualization library written in Javascript. It is ported to python and used with HTML, JS right inside python. The most amazing feature of plotly is that it helps you create interactive plots. Interactive plots are those plots where you can interact. You can zoom in, filter a subset of the data visualized, and change the scale of any axis. All of these and much more are achievable inside plotly.

The only downside of plotly is its performance. It is reasonably very slow compared to matplotlib as it is manipulating a full-fledged website written with HTML, CSS, and Javascript.

Installing plotly is a little trickier than installing other python libraries. First, install the plotly core library using pip or conda.

SNIPPET
1pip install plotly
2# or
3# The latest version of plotly is not available in the base channel. 
4# So we install it from its own channel
5conda install plotly -c plotly 

This will install everything required to use plotly. But, if you want to install it into jupyter notebook or jupyter-lab, then you need to install ipywidgets python package.

SNIPPET
1pip install ipywidgets
2# or
3conda install ipywidgets

For jupyter-lab, you want to add the jupyterlab extension as well.

SNIPPET
1# Only for jupyter-lab
2jupyter labextension install jupyterlab-plotly

Mostly, plotly works with Graph Objects and traces inside them. On the other hand, plotly provides a submodule named express. This is a layer above plotly to make common types of plotting much easier. For this lesson, we will only use plotly express for most of the plotting.

Unlike matplotlib, plotly works best with pandas Dataframes. So it is best to create a dataframe from the NumPy array or matrix to visualize with plotly.

Plotly also has a set of datasets inside the library mostly for demo purposes. Let's use the iris dataset and plot it with plotly express.

PYTHON
1import plotly.express as px
2df = px.data.iris()
3fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
4fig.show()

Plotly

Let us start to draw different kinds of plots for different kinds of data using both matplotlib and plotly library.