
Visualization with Plotly

To know the insights of the data we need wonderful visualization tools. Python is very useful programming to visualize data with pre-defined libraries in it. There are many useful visualization libraries like:
Among this matplotlib is a base library and others are built on top of it. Matplotlib idea comes from Matlab visuals plotting. It gives us static images. The matplotlib contains almost every plot.
Seaborn is designed to create an interactive statistical plot. It uses matplotlib as a backend. Visualization with seaborn in python is very simple and do plotting with less number of lines in python.
Plotly is an open-source library and creates interactive visualizations. Plotly not just has a library package in python but also in R, React, javascript and others.
The other thing we will see with plotly is the dash that is a very interactive plotting as a dashboard in web applications. Dash is an open-source library used for creating a fully functional dashboard.
To know more about the types of plots and chart can visit the site if plotly.
Extra Note: Power BI is a business intelligence tool that will help to analyze the data, clean the data, and converting that data into visual formats. PowerBI is developed by Microsoft Corporation with an initial release in 2014.
Scatter plot: It is used for correlation between two numerical variables and helps us to find insights and patterns from the graph. In the below figure we can see the types of correlation between two features.
Let’s see a practical example of scatter and line chart with python.
For this, I used spyder IDE for the code in an anaconda distribution.
#import the necessary libraries
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
If plotly is not installed in your anaconda then install in the anaconda prompt by pip install plotly.
NumPy: NumPy is a python library package used for multidimensional array computing. NumPy process is faster than list because of its homogeneous type that is packed densely in memory and performed vectorization.
Plotly offline is used for creating a standalone HTML file and we can save it locally to see in our web browser.
Plotly graph_objs is used for importing all graphs objects in plotly.
# to generate the data with random number for x and y axes
np.random.seed(42)
random_x = np.random.randint(1,101,100)
random_y = np.random.randint(1,101,100)
Random is used for getting random numbers with given range in the randint method.
data = [go.Scatter(x = random_x, y = random_y, mode = ‘markers’)]layout = go.Layout(title=’Hello First Plot’,
xaxis = dict(title=’My X axis’),
yaxis = dict(title=’My Y axis’),
hovermode = ‘closest’
)
Here, we select the scatter graph object in that we provided the input data and selected the mode as markers. The markers mean the dot point will be shown in the graph.
fig = go.Figure(data= data, layout = layout)
pyo.plot(fig, filename = ‘Scatter.html’)
The figure method is used to make the data and layout in proper graph format. The plot method is used to save the plot in an HTML file.
After opening the HTML file the plot will look like this as shown below.
Line chart is a series of data connecting markers with line. The python code for this is shown below:
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as gonp.random.seed(50)
random_x = np.linspace(0,1,100)
random_y = np.random.randn(100)data0 = go.Scatter(x = random_x, y = random_y, mode = 'markers', name='markers')data1 = go.Scatter(x = random_x, y = random_y+5, mode = 'lines', name='mylines')data = [data0, data1]layout = go.Layout(title='Line Chart')fig = go.Figure(data= data, layout = layout)
pyo.plot(fig, filename = 'line_chart.html')
These are the basic plot visualization of plotly with python.
Conclusion:
The plotly is a great visualization tool embed with an HTML dashboard. Nowadays the demand for interactive visualization is on the boom because of the voluminous data flow in the world.
Reach me on my LinkedIn
- NLP — Zero to Hero with Python
2. Python Data Structures Data-types and Objects
3. MySQL: Zero to Hero
4. Basics of Time Series with Python
5. NumPy: Zero to Hero with Python
6. Fundamentals of series and Data Frame in Pandas with python