The plotly library is an interactive, open-source plotting library that supports over 40 unique chart types. They cover a wide range of statistical, financial, geographic, scientific, and 3-dimensional cases.
It is based on JavaScript and focused on interactive and web-based visualizations, which means the figures are interactive when viewed in a web browser. Also, it is possible to save graphs as local files on your computer and open them later in any web browser.
Some of the main features of plotly are:
It produces interactive graphs;
Graphs are stored in JSON format;
Other programming languages such as R, Julia, MATLAB use the figures from
plotly;We can export graphs as vector image formats.
Plotly modules
Plotly contains the following main modules:
the
plotly.graph_objsmodule (usually imported asgo) consists of all of the class definitions for the objects that make up the plots of the graph. The graph objects are defined as figures, data, layout, or charts such as histograms;the
plotly.expressmodule (usually imported aspx) contains functions that can create complete figures at once. This is the recommended starting point for making the most common figures;The
plotly.iomodule is a low-level interface for displaying, reading, and writing figures.
Previous library versions contained functions to create figures in both online and offline modes. In online mode, figures were uploaded to the Chart Studio cloud service, whereas in offline mode, figures were created and stored in the local computer. The latest versions of Plotly support only the offline mode. All online functionalities of the module plotly.plotly now are present at the chart_studio package.
Getting started
In this first example, we will use the plotly.graph_objs library to build the chart by using the Figure() function:
import plotly.graph_objs as go
fig = go.Figure(
data=[go.Bar(x=["Mon", "Fri"], y=[17, 10])],
layout_title_text="Figure as Graph Object"
)
fig.show()Now, let's create the linear graph representing the sales in a store, Monday to Friday. We will use the plotly.express library and its function — line(). The code is the following:
import plotly.express as px
fig = px.line(x=['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], y=[7, 10, 14, 9, 11])
fig.show()Figures as dictionaries
At the basic level, figures are dictionaries. We can display a figure using functions from the plotly.io module. In this example, we will build a bar chart using a dictionary data structure that contains data and layout:
import plotly.io as ply
fig = dict({
"data": [{"type": "bar",
"x": ['Mon', 'Wed', 'Fri'],
"y": [7, 14, 11]}],
"layout": {"title": {"text": "Sales of the 2nd week"}},
"frame": {"duration": 50, "redraw": False}
})
ply.show(fig)Figures are represented as trees where the root node has three top layer attributes – data, layout, and frame.
The nodes are called attributes. The layout.legend is a nested dictionary, where the legend key is inside the dictionary. The legend value itself is a dictionary.
The first attribute of a figure is data. Its value must be a list of dictionaries referred to as traces. Each trace has a type and represents a set of related graphical marks in a figure.
The second attribute of a figure is the layout. It contains attributes that control the position and the configuration of the figure, such as dimensions, margins, title, legend, color axes, and so on.
The third attribute of a figure is frame. Its value must be a list of dictionaries that define sequential frames in an animated plot. Each frame contains its data attribute as well as other parameters.
Different charts in one figure
This example will create a figure that includes two graphs, side by side, called subplots. The figure consists of one row and two columns in the subplot layout:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Bar(x=["Mon", "Tue", "Wed", "Thu", "Fri"], y=[17, 30, 12, 15, 16]),
row=1, col=1
)
fig.add_trace(
go.Bar(x=["Sat", "Sun"], y=[30, 25]),
row=1, col=2
)
fig.update_layout(height=600, width=800, title_text="Sales of this week")
fig.show()Why Plotly?
There are some advantages, which motivate us to try Plotly.
Ease to use.
Plotlyis super user-friendly. You don't need any training or knowledge to utilize all tools and features;Attractive. A wide range of audiences will find it to their liking;
Advanced analytics.
Plotlycontains high-powered tools for analytics, which are compatible with Python, Julia, R, MATLAB, and Arduino.Customization. It has an open API that can fully customize any user's experience. We can easily integrate it with third-party apps and existing workflow structures.
Web Interface. It allows embedding interactive plots in projects or websites using iframes or HTML.
Some disadvantages of Plotly are:
Lack of documentation;
A vast range of modules makes it hard to keep it up.
Conclusions
To summarize, Plotly is a great visualization tool capable of handling geographical, scientific, statistical, and financial data. These visualizations are compatible with many programming languages and tools, such as Python, R, and MATLAB. We consider this the main reason to try Plotly.
Read more on this topic in Julia vs Python: A Comparison of Two Popular Programming Languages on Hyperskill Blog.