0% found this document useful (0 votes)
13 views38 pages

Lab Manual for Students

The document outlines various data visualization techniques using Python libraries such as Matplotlib and Seaborn, as well as an introduction to Tableau software for creating charts and dashboards. It covers the creation of bar plots, scatter plots, histograms, and box plots, along with the functionalities of Tableau for data connection, chart creation, and calculations. Additionally, it discusses the importance of dashboards in presenting key performance indicators and related information effectively.

Uploaded by

aimlbtech7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views38 pages

Lab Manual for Students

The document outlines various data visualization techniques using Python libraries such as Matplotlib and Seaborn, as well as an introduction to Tableau software for creating charts and dashboards. It covers the creation of bar plots, scatter plots, histograms, and box plots, along with the functionalities of Tableau for data connection, chart creation, and calculations. Additionally, it discusses the importance of dashboards in presenting key performance indicators and related information effectively.

Uploaded by

aimlbtech7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Experiment 1

Visualization of Spreadsheet Models.

Aim: Data Visualization is the presentation of data in graphical format. It helps people
understand the significance of data by summarizing and presenting huge amount of data in a
simple and easy-to-understand format and helps communicate information clearly and
effectively.

Bar plot

A bar plot or bar chart is a graph that represents the category of data with rectangular bars with
lengths and heights that is proportional to the values which they represent. The bar plots can be
plotted horizontally or vertically. A bar chart describes the comparisons between the discrete
categories. One of the axis of the plot represents the specific categories being compared, while
the other axis represents the measured values corresponding to those categories.

Program1:

import pandas as pd
import matplotlib.pyplot as plt

DVT LAB DEPARTMENT OF CSD, NRIIT


df = pd.read_csv("C:\\Users\\GOPAL\\Desktop\\Book1.csv")
#df.plot.bar()
# plot between 2 attributes
plt.bar(df['Age'], df['Sales'],color="hotpink")
plt.xlabel("Age")
plt.ylabel("Sales")
plt.show()

Scatter Plot
The scatter() function plots one dot for each observation. It needs two arrays of the same length,
one for the values of the x-axis, and one for values on the y-axis.

Program2:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("C:\\Users\\GOPAL\\Desktop\\Book1.csv")
# scatter plot between income and age
plt.scatter(df['Income'], df['Age'])
plt.xlabel("Income")
plt.ylabel("Age")
plt.show()
# scatter plot between income and sales
plt.scatter(df['Income'], df['Sales'])
plt.xlabel("Income")
plt.ylabel("Sales")
plt.show()
# scatter plot between sales and age
plt.scatter(df['Sales'], df['Age'])
plt.xlabel("Sales")
plt.ylabel("Age")
plt.show()

DVT LAB DEPARTMENT OF CSD, NRIIT


Histogram
A histogram is a graph showing frequency distributions. It is a graph showing the number of
observations within each given interval.The hist() function will use an array of numbers to
create a histogram, the array is sent into the function as an argument.

Program3:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("C:\\Users\\GOPAL\\Desktop\\Book1.csv")
# create histogram for numeric data
df.hist()
# show plot
plt.show()

Box plot
A Box plot is a way to visualize the distribution of the data by using a box and some vertical
lines. It is known as the whisker plot. The data can be distributed between five key ranges, which
are as follows:

1. Minimum: Q1-1.5*IQR
2. 1st quartile (Q1): 25th percentile
3. Median:50th percentile
4. 3rd quartile(Q3):75th percentile
5. Maximum: Q3+1.5*IQR

Here IQR represents the InterQuartile Range which starts from the first quartile (Q1) and ends
at the third quartile (Q3).

Box Plot visualization

In the box plot, those points which are out of range are called outliers. We can create the box plot
of the data to determine the following:

o The number of outliers in a dataset

DVT LAB DEPARTMENT OF CSD, NRIIT


o Is the data skewed or not
o The range of the data
The range of the data from minimum to maximum is called the whisker limit.

Program4:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("C:\\Users\\GOPAL\\Desktop\\Book1.csv")
# For each numeric attribute of dataframe
#df.plot.box()
# individual attribute box plot
plt.boxplot(df['Income'])
plt.show()

Seaborn Library:
Seaborn is a library for making statistical graphics in Python. It builds on top of matplotlib and
integrates closely with pandas data structures. Seaborn helps you explore and understand your
data
Program5:

import seaborn as sns


import matplotlib.pyplot as plt
import pandas as pd
sns.set(style='whitegrid')
tip = pd.read_csv('C:\\Users\\GOPAL\\Desktop\\Book1.csv')
sns.scatterplot(x='Income', y='Age', hue='Age',data=tip)
plt.title("Income Vs Age")
plt.show()

Plotly library
Plotly Python is a library which is used to design graphs, especially interactive graphs. It can
plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is
mainly used in data analysis as well as financial analysis. Plotly python is an interactive
visualization library.

DVT LAB DEPARTMENT OF CSD, NRIIT


Program6:

import plotly.express as px
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('C:\\Users\\GOPAL\\Desktop\\Book1.csv')
fig = px.scatter(df, x="Income", y="Age")
fig.update_traces(marker=dict(color='red'))
fig.show()

DVT LAB DEPARTMENT OF CSD, NRIIT


Experiment 2

Aim: Getting started with Tableau Software using Data file formats, connecting your Data to
Tableau, creating basic charts(line, bar charts, Tree maps),Using the Show me panel.

Introduction to Tableau and Installation

Tableau is a data visualization tool that provides pictorial and graphical representations of data.
It is used for data analytics and business intelligence. Tableau provides limitless data exploration
without interrupting flow of analysis. With an intuitive drag and drop interface, user can uncover
hidden insights in data and make smarter decisions faster.

Tableau can be downloaded from the following website:


https://www.tableau.com/products/public/download

Prepare Your Data:

Before connecting your data to Tableau, ensure that your data is in a suitable format. Common
data file formats that Tableau supports include Excel (.xlsx), CSV (.csv), and text files (.txt).
Make sure your data is organized with headers for each column.

Steps to follow –
 Run Tableau. On the left side of the start screen, there is a Connect Panel.
 Go to the File Section and choose the type of data file you want. Let’s choose any excel
file.

DVT LAB DEPARTMENT OF CSD, NRIIT


On clicking the Microsoft Excel option, a dialogue box will appear for the user to choose the
required data file. As shown below I have chosen a data – ‘Sample - Superstore.xlsx’

Now, Tableau will create connection to the data file and as you can see we have our data at the
bottom.

DVT LAB DEPARTMENT OF CSD, NRIIT


After connecting to the data file, the user can go the Worksheet

Tableau Desktop Workspace Menu

The Tableau desktop workspace consists of various elements as given below:

DVT LAB DEPARTMENT OF CSD, NRIIT


Menu Bar: It consists of menu options like File, Data, Worksheet, Dashboard, Story,
Analysis, Map, Format, Server, Window, and Help. The options in the menu bar, including
features like data source connection, file saving, design, table calculation options, and file export
features for creating a dashboard, worksheet, and storyboard.

 File Menu: For any Windows program the file menu contains New, Open, Close, Save,
Save As, and Print, functions. The most frequently used feature found in this menu is the
Print to pdf option. This allows us to export our dashboard or worksheet in pdf form. If
you don't remember where Tableau places files, or you want to change the default file-
save location, use the repository location option for review the file and change it. We can
create a packaged workbook from the export packaged workbook option in a fast manner.
 Data Menu: You can use a data menu if you find some interesting tabular data on a
website that you want to analyze with Tableau. Highlight and copy the data from the site,
then use the Paste Data option to input it into Tableau. Once pasted, then Tableau will
copy the data from the Windows clipboard and add a data source in the data window. The
Edit Relationships menu option is used in data blending. This menu option is needed if
the field names are not identical in two different data sources. It allows you to define the
related fields correctly.
 Worksheet Menu: The Export option allows you to export the worksheet as an Excel
crosstab, an image, or in Access database file format. The Duplicate as Crosstab option
creates a crosstab version of the worksheet and places it in a new worksheet.
 Dashboard Menu: The Action Menu is a useful feature that is reachable from both the
Worksheet Menu and the Dashboard Menu.
 Analysis Menu: In this menu, you can access the stack marks and aggregate measures
options. These switches allow you to adjust default Tableau behaviors that are useful if
you required to build non-standard chart types. The Create Edit Calculated Field and
Calculated Field options are used to make measures and new dimensions that don't exist
in your data source.
 Map Menu: The Map Menu bar is used to alter the base map color schemes. The other
menu bar are related in the way of replacing Tableau's standard maps with other map
sources. You can also import the geocoding for the custom locations using the geocoding
menu.
 Format Menu: This menu is not used very commonly because pointing at anything, and
right-clicking gets you to a context-specific formatting menu more quickly. You may
need to alter the cell size in a worksheet rarely. If you don't like the default workbook
theme, use the Workbook Theme menu to select one of the other two options.
Toolbar Icon: Toolbar icon below the menu bar can be used to edit the workbook using
different features like redo, undo, new data source, save, slideshow, and so on.

Dimension Shelf: The dimension presents in the data source for example- customer (customer
name, segment), order (order date, order id, ship date, and ship mode), and location (country,
state, and city) these all type of data source can be viewed in the dimension shelf.

Measure Shelf: The measures present in the data source, for example- Discount, Profit, Profit
ratio, Quantity, and Sales- These all types of data source can be viewed in the measure shelf.

DVT LAB DEPARTMENT OF CSD, NRIIT


Sets and Parameters Shelf: The user-defined sets and parameters can view in the sets and
parameters. It is also used to edit the existing sets and parameters.

Page Shelf: Page shelf is used to view the visualization in video format by keeping the related
filter on the page shelf.

Filter Shelf: Filter Shelf is used to filter the graphical view by the help of the measures and
dimensions.

Marks Card: Marks card is used to design the visualization. The data components of the
visualization like size, color, path, shape, label, and tooltip are used in the visualizations. It can
be modified in the marks card.

Worksheet: The worksheet is the space where the actual visualization, design, and
functionalities are viewed in the workbook.

Tableau Repository: Tableau repository is used to store all the files related to the Tableau
desktop. It includes various folders like Connectors, Bookmarks, Data sources, Logs, Extensions,
Map sources, Shapes, Services, Tab Online Sync Client, and Workbooks. My Tableau repository
is located in the file path C:\Users\User\Documents\My Tableau Repository.

DVT LAB DEPARTMENT OF CSD, NRIIT


Experiment 3

Aim: Getting started with Tableau Software creating basic charts (line, bar charts, Tree maps),
using the Show me panel.

Data Source Pane:

Once your data is connected, the Data Source Pane will appear on the left-hand side of the
Tableau interface. Here, you can see a preview of your data and perform data transformations or
join multiple data sources if necessary.

Creating Basic Charts:

Now, let's create some basic charts using Tableau:

a. Line Chart:

1. From the "Data Source pane", drag and drop the date field to the Columns shelf and a numeric
field (e.g., sales, revenue) to the Rows shelf.

DVT LAB DEPARTMENT OF CSD, NRIIT


2. Then Tableau will automatically create a line chart. You can customize it by adding labels,
titles, and formatting.

DVT LAB DEPARTMENT OF CSD, NRIIT


b. Bar Chart:

1. Drag and drop a categorical field (e.g., product category, region) to the Columns shelf and a
numeric field to the Rows shelf.

2. Then Tableau will create a bar chart. You can adjust the orientation and formatting as needed.
To display Labels on the bars click on Lables and select "Show mark lables".

DVT LAB DEPARTMENT OF CSD, NRIIT


c. Treemap:

1. Drag and drop a categorical field to the Columns shelf.

2. Drag and drop a numeric field to the Size shelf.

3. Tableau will create tree map visualization. You can further customize it by adjusting colors
and labels.

Using the Show Me Panel:-

The Show Me panel in Tableau helps you explore various chart types based on your data and the
fields you select. Here's how to use it:

1. After adding fields to the Rows and Columns shelves, click on the "Show Me" panel
located on the left side of the Tableau interface.
2. In the Show Me panel, you'll see a variety of chart options that Tableau recommends
based on your data. Click on a chart type to create it.
3. Tableau will automatically generate the selected chart type with your data. You can
further customize it as needed.
4. To go back to the regular worksheet view, click the "Clear" button in the Show Me panel.

DVT LAB DEPARTMENT OF CSD, NRIIT


Experiment 4

Aim: Tableau Calculations, Overview of SUM, AVR, and Aggregate features, Creating custom
calculations and fields.

Aggregate Function: Aggregate function can be a function where the values of multiple lines
are grouped together to form a single summative value. Typical integration functions include:
Measurement (e.g., arithmetic means), Count, etc.

The most commonly used functions in tableau are listed below:


1. SUM: sum of values
2. AVG: average of values
3. MIN: minimum of values
4. MAX: maximum of values
5. VAR: variance of sample population
6. VARP: variance of entire population
7. STDEV: standard deviation of sample population
8. STDEVP: standard deviation of entire population
9. COUNT: count of values

Tableau Sum Function


The Tableau Sum function is employed to seek out the Sum of records during a column.

Syntax:
SUM(Expression)

DVT LAB DEPARTMENT OF CSD, NRIIT


Tableau Avg Function
Returns the average of all the values in the expression. Null values are ignored.

Syntax:
AVG(Expression)

DVT LAB DEPARTMENT OF CSD, NRIIT


Tableau MIN Function
Returns the minimum of the two arguments, which must be of the same data type.MIN can also
be applied to a single field as an aggregation.

Syntax:
MIN(Expression1, Expression2)

DVT LAB DEPARTMENT OF CSD, NRIIT


Tableau MAX Function
Returns the maximum of the two arguments, which must be of the same data type. MAX can also
be applied to a single field as an aggregation.

Syntax:
MAX(Expression 1, Expression 2)

DVT LAB DEPARTMENT OF CSD, NRIIT


Tableau VAR Function

Returns the statistical variance of all values in the given expression based on a sample of the
population.

Syntax:
VAR(Expression)

DVT LAB DEPARTMENT OF CSD, NRIIT


DVT LAB DEPARTMENT OF CSD, NRIIT
Experiment 5

Aim: create a dashboard in tableau software,

Dashboards

A dashboard is a way of displaying various types of visual data in one place. Usually, a
dashboard is intended to convey different, but related information in an easy-to-digest form. And
oftentimes, this includes things like key performance indicators (KPI)s or other important
business metrics that stakeholders need to see and understand at a glance.

Dashboards are useful across different industries and verticals because they’re highly
customizable. They can include data of all sorts with varying date ranges to help you understand:
what happened, why it happened, what may happen, and what action should be taken.

Example: A car dashboard provides real-time information about a car's speed, fuel volume,
RPM, and other engine-related indicators. Similarly, a data dashboard provides information
about company historical sales, key performance indicators (KPIs), sales growth, operational
indicators, and customer feedback. This information is presented in a precise manner so that
managers or executives can understand the situation and make appropriate decisions.

There are several ways to customize the dashboard, and they all fall into one of three categories -

1. Operational Dashboards: these dashboards show the real-time performance of day-to-day


business operations. They are connected to multiple data sources and contain hundreds of
metrics, indicating various functionalities of the business.
2. Analytical Dashboards: these dashboards use historical data to identify trends. They are
mainly used by data analysts to write detailed reports about a company's past
performance and what steps they can use to improve current systems.
3. Strategic Dashboards: these dashboards are mainly used to track current performance
compared to key performance indicators and align actions with strategy.
Steps:

1. Build the charts required for dashboard creation.


2. Customize the charts created in the step1.
3. Place them in the dashboard.
4. Add interactivity to the dashboard.

Creating the charts:

Worksheet1

The first worksheet will include the insights related to the sales and profit (KPI’s (Key
Performance Indicators) ).

DVT LAB DEPARTMENT OF CSD, NRIIT


Worksheet2

In second worksheet lets create another visualization that provides insights related to the sales by
state in USA.

DVT LAB DEPARTMENT OF CSD, NRIIT


Worksheet3

In third worksheet let’s create another visualization that provides insights related to the sales by
month

.Worksheet4

In fourth worksheet let’s create another visualization that provides insights related to the sales by
product.

DVT LAB DEPARTMENT OF CSD, NRIIT


Open a new dashboard
You can open a dashboard window either from the Dashboard option given on the menu bar or
from the Dashboard icon highlighted in red on the bottom bar.
Selecting the New Dashboard option or clicking on the Dashboard icon will open a new
window named Dashboard. You change the name of the dashboard as per your liking.

Dashboard pane
In the window where we can create our dashboard, we get a lot of tabs and options related to
dashboarding. On the left, we have a Dashboard pane which shows the dashboard size, list of
available sheets in a workbook, objects, etc.

DVT LAB DEPARTMENT OF CSD, NRIIT


From the Dashboard tab, we can set the size of our dashboard. We can enter custom dimensions
like the width and height of the dashboard as per our requirements.

Layout pane
Right next to the Dashboard pane is the Layout pane where we can enhance the appearance and
layout of the dashboard by setting the position, size, border, background, and paddings.

DVT LAB DEPARTMENT OF CSD, NRIIT


Adding a sheet
Now, we’ll add a sheet onto our empty dashboard. To add a sheet, drag and drop a sheet from
the Sheets column present in the Dashboard tab. It will display all the visualizations we have on
that sheet on our dashboard. If you wish to change or adjust the size and place of the
visual/chart/graph, click on the graph then click on the small downward arrow given at the
right. A drop-down list appears having the option Floating, select it. This will unfix your chart
from one position so that you can adjust it as per your liking.

We can add as many sheets as we require and arrange them on the dashboard properly.

Final dashboard
Now, we move towards making a final dashboard in Tableau with all its elements in place.

DVT LAB DEPARTMENT OF CSD, NRIIT


Presentation mode
Once our dashboard is ready, we can view it in the Presentation Mode. To enable the
presentation mode, click on the icon present on the bar at the top as shown in the screenshot
below or press F7.

This opens our dashboard in the presentation mode. So far we were working in the Edit Mode. In
the presentation mode, it neatly shows all the visuals and objects that we have added on the
dashboard. We can see how the dashboard will look when we finally present it to others or share
it with other people for analysis.

DVT LAB DEPARTMENT OF CSD, NRIIT


Experiment 6

Aim: Python programs for interactive plots.

Interactive plots in Python are a great way to enhance data visualization by allowing users to
explore data dynamically. These plots can be zoomed, panned, and modified in real time,
offering a richer experience compared to static visualizations.

Plotly

The plotly Python library is an interactive, open-source plotting library that supports over 40
unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-
dimensional use-cases.

Program1:

import plotly.express as px
import pandas as pd
# Sample dataset
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 11, 12, 13, 14],
'category': ['A', 'B', 'C', 'D', 'E'] })
# Create an interactive scatter plot
fig = px.scatter(df, x='x', y='y', color='category', title="Interactive Scatter Plot")
fig.show()

Program2:
import plotly.express as px
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'time': [1, 2, 3, 4, 5],
'value': [10, 20, 30, 40, 50] })

DVT LAB DEPARTMENT OF CSD, NRIIT


# Line Plot
fig = px.line(df, x='time', y='value', title="Interactive Line Plot")
fig.show()

Program3:

import plotly.express as px
# using the iris dataset
df = px.data.iris()
# plotting the bar chart
fig = px.bar(df, x="sepal_width", y="sepal_length")
# showing the plot
fig.show()

Program4:

import plotly.express as px
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 11, 12, 13, 14],
'size': [100, 200, 300, 400, 500],
'category': ['A', 'B', 'C', 'D', 'E']
})
# Bubble Chart
fig = px.scatter(df, x='x', y='y', size='size', color='category', title="Interactive Bubble Chart")
fig.show()

DVT LAB DEPARTMENT OF CSD, NRIIT


Bokeh

Bokeh is a Python library for creating interactive visualizations for modern web browsers. It
helps you build beautiful graphics, ranging from simple plots to complex dashboards with
streaming datasets. With Bokeh, you can create JavaScript-powered visualizations without
writing any JavaScript yourself.

Program5:

from bokeh.plotting import figure, show


# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# create a new plot with a title and axis labels
p = figure(title="Simple line example", x_axis_label="x", y_axis_label="y")
# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)
# show the results
show(p)

Program6:
from bokeh.plotting import figure, show
# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]
# create a new plot with a title and axis labels
p = figure(title="Multiple line example", x_axis_label="x", y_axis_label="y")
# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="blue", line_width=2)

DVT LAB DEPARTMENT OF CSD, NRIIT


p.line(x, y2, legend_label="Rate", color="red", line_width=2)
p.line(x, y3, legend_label="Objects", color="green", line_width=2)
# show the results
show(p)

Program7:
from bokeh.plotting import figure,show
x=[1,2,3,4,5]
y=[4,5,5,7,2]
p=figure(title="bar graph",x_axis_label='x',y_axis_label='y')
p.scatter(x,y,legend_label='objects',color='#aa54ff',size=12)
show(p)

Mpld3

The mpld3 project brings together Matplotlib, the popular Python-based graphing library,
and D3js, the popular JavaScript library for creating interactive data visualizations for the web.
The result is a simple API for exporting your matplotlib graphics to HTML code which can be
used within the browser, within standard web pages, blogs, or tools such as the IPython
notebook.

Program8:

import mpld3
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
np.random.seed(0)
x, y = np.random.normal(size=(2, 200))
color, size = np.random.random((2, 200))
ax.scatter(x, y, c=color, s=500 * size, alpha=0.3)

DVT LAB DEPARTMENT OF CSD, NRIIT


ax.grid(color='lightgray', alpha=0.7)
mpld3.display(fig)

Program9:

#Line Plots with Legend


import mpld3
import matplotlib.pyplot as plt
import numpy as np
# Draw lines
fig, ax = plt.subplots()
x = np.linspace(-5, 15, 1000)
for offset in np.linspace(0, 3, 4):
ax.plot(x, 0.9 * np.sin(x - offset), lw=5, alpha=0.4, label="Offset: {0}".format(offset))
ax.set_xlim(0, 10)
ax.set_ylim(-1.2, 1.0)
ax.text(5, -1.1, "Here are some curves", size=18, ha='center')
ax.grid(color='lightgray', alpha=0.7)
ax.legend()
mpld3.display(fig)

Pygal

Pygal is a Python library and It is an open-source that is used for creating visual and interactive
charts. This library is based on SVG(Scalar Vector Graphics) technology, which ensures that
the charts are scalable without any loss in quality. SVG is a vector-based graphic in the XML
format that can be edited in any editor. Pygal can create graphs with minimal lines of code that
can be easy to understand and write.

Program10:

import pygal

DVT LAB DEPARTMENT OF CSD, NRIIT


import numpy as np
# creating the chart object
bar_chart = pygal.Bar()
# naming the title
bar_chart.title = 'Bar Chart'
# Random data
bar_chart.add('A', np.random.rand(10))
bar_chart.add('B', np.random.rand(10))
bar_chart.add('C', np.random.rand(10))
bar_chart.add('D', np.random.rand(10))
bar_chart

Program11:
import pygal
import numpy as np
# creating line chart object
line_chart = pygal.Line()
# naming the title
line_chart.title = 'Line chart'
# adding lines
line_chart.add('A', np.random.rand(5))
line_chart.add('B', np.random.rand(5))
line_chart.add('C', np.random.rand(5))
line_chart.add('D', np.random.rand(5))
line_chart

DVT LAB DEPARTMENT OF CSD, NRIIT


Experiment 7

Aim: Advanced data visualization techniques using python.

Chord diagram

A chord diagram represents flows or connections between several entities (called nodes). Each
entity is represented by a fragment on the outer part of the circular layout. Then, arcs are drawn
between each entities. The size of the arc is proportional to the importance of the flow.

Program1:

import openchord as ocd


adjacency_matrix = [[ 3, 18, 9, 0, 23],
[18, 0, 12, 5, 29],
[ 9, 12, 0, 27, 10],
[ 0, 5, 27, 0, 0],
[23, 29, 10, 0, 0]]
labels = ['Emma', 'Isabella', 'Ava', 'Olivia', 'Sophia']
fig = ocd.Chord(adjacency_matrix, labels)
#fig.colormap = ['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A', '#19D3F3',
'#FF6692', '#B6E880', '#FF97FF', '#FECB52']
fig.show()

Treemaps

Treemaps display hierarchical (tree-structured) data as a set of nested rectangles. Each branch of
the tree is given a rectangle, which is then tiled with smaller rectangles representing sub-
branches. A leaf node's rectangle has an area proportional to a specified dimension of the
data. Often the leaf nodes are colored to show a separate dimension of the data.

Program2:

import matplotlib.pyplot as plt

DVT LAB DEPARTMENT OF CSD, NRIIT


import squarify # pip install squarify (algorithm for treemap)
import pandas as pd
# Create a data frame with fake data
df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"]
})
# plot it
squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 )
plt.axis('off')
plt.show()

Program3:

import plotly.express as px
fig = px.treemap(
names = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
)
fig.update_traces(root_color="lightgrey")
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

Sankey diagrams

A Sankey Diagram is a visualisation technique that allows displaying flows. Several entities
(nodes) are represented by rectangles or text. Their links are represented with arrows or arcs that
have a width proportional to the importance of the flow.

Program4:
"""As from figure we can see there are 6 nodes(label in code above) A1(0), A2(1),
B1(2),B2(3),C1(4) and C2(5). Here we have given numbers to all the nodes.

Now let create source list . A1(0), A2(1), B1(2) and B2(3) are working as source, so source list is
[0(A1),0(A1),1(A2),2(B1),3(B2),3(B2) ]=> [0, 0, 1, 2, 3, 3].

DVT LAB DEPARTMENT OF CSD, NRIIT


For creating target list, B1(2),B2(3), C1(4) and C2(5) are working as target , so target list is
[2(B1),3(B2),3(B2), 4(C1),4(C1),5(C2)]=>[2, 3, 3, 4, 4, 5].

Values list is [8(A1->B1), 2(A1->B2), 4(A2->B2), 8(B1->C1), 4(B2->C1), 2(B2->C2)]."""


Program4:

import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = ["A1", "A2", "B1", "B2", "C1", "C2"]
),
link = dict(
source = [0, 0, 1, 2, 3, 3],
target = [2, 3, 3, 4, 4, 5],
value = [8, 2, 4, 8, 4, 2],
))])
fig.update_layout(title_text="Basic Sankey Diagram", font_size=10,width=600, height=400)
fig.show()

Radar charts

A radar chart displays multivariate data stacked at an axis with the same central point. The
chart features three or more quantitative variables for comparison; these variables are known as
radii. The map looks similar to the spider web, which is why it’s also called a spider chart.

Program5:

import plotly.graph_objects as go
import plotly.offline as pyo
genre = ['Action', 'Comedy', 'Drama', 'Horror', 'Mystery', 'Romance']

DVT LAB DEPARTMENT OF CSD, NRIIT


c_1 = [22, 98, 8, 109, 111, 29]
c_2 = [49, 67, 140, 13, 24]
c_3 = [34, 45, 57, 34, 77, 25]
fig = go.Figure(
data=[
go.Scatterpolar(r=c_1, theta=genre, fill='toself', name='Customer 1'),
go.Scatterpolar(r=c_2, theta=genre, fill='toself', name='Customer 2'),
go.Scatterpolar(r=c_3, theta=genre, fill='toself', name='Customer 3')
],
layout=go.Layout(
title=go.layout.Title(text='Customer movie genre comparison'),
polar={'radialaxis': {'visible': True}},
showlegend=True
)
)
pyo.plot(fig)

3D Plot

Program6:

import plotly.graph_objects as go

import numpy as np

np.random.seed(1)

N = 70

fig = go.Figure(data=[go.Mesh3d(x=(70*np.random.randn(N)),

y=(55*np.random.randn(N)),

z=(40*np.random.randn(N)),

DVT LAB DEPARTMENT OF CSD, NRIIT


opacity=0.5,

color='rgba(244,22,100,0.6)'

)])

fig.update_layout(

scene = dict(

xaxis = dict(nticks=4, range=[-100,100],),

yaxis = dict(nticks=4, range=[-50,100],),

zaxis = dict(nticks=4, range=[-100,100],),),

width=700,

margin=dict(r=20, l=10, b=10, t=10))

fig.show()

DVT LAB DEPARTMENT OF CSD, NRIIT

You might also like