Plotly

Plotly is an interactive, open source graphing library in Python. It is a charting library that provides over 30 chart types, such as scientific charts, 3D graphs, statistical charts, financial charts, and more.

To use plotly in Python, first we have to install it in our system. To install plotly, run the following command in your Terminal:

$ pip3 install plotly

We can use plotly online as well as offline. For online usage, you need to have a plotly account and after that you need to set up your credentials in Python:

            plotly.tools.set_credentials_file(username='Username', api_key='APIkey')

To use plotly offline, we need to use the plotly function:  plotly.offline.plot()

In this section, we are going to use plotly offline. Now, we are going to look at a simple example. For that, create a script called sample_plotly.py and write the following content in it:

import plotly
from plotly.graph_objs import Scatter, Layout

plotly.offline.plot({
"data": [Scatter(x=[1, 4, 3, 4], y=[4, 3, 2, 1])],
"layout": Layout(title="plotly_sample_plot")
})

Run the preceding script as sample_plotly.py. You will get the following output:

student@ubuntu:~/work$ python3 sample_plotly.py

The output is as follows:

In the preceding example, we imported the plotly module and then we set plotly for offline use. We put arguments in it, which are useful to plot a graph. In the example, we used some of arguments: data and layout. In the data argument, we define the scatter function with x and y arrays, which have values to plot over the x and y axes, respectively. Then we use the layout argument, in which we define the layout function to provide the title for the graph. The output of the preceding program is saved as an HTML file and gets opened in your default browser. This HTML file is in the same directory as your script.

Now let's look at some different types of charts for visualizing the data. So, first, we are going to start with the scatter plot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset