Thanks to visit codestin.com
Credit goes to www.scribd.com

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

UNIT 5 Plotly in Python

Uploaded by

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

UNIT 5 Plotly in Python

Uploaded by

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

UNIT 5:Introduction to other

visualization tools:
plotly in Python
Sea Born library in Python
What is Plotly?
• Python Plotly Library is an open-source library that can be used for data
visualization and understanding data simply and easily.
• Plotly supports various types of plots like line charts, scatter plots, histograms,
cox plots, etc.
• Why Plotly over other visualization tools or libraries?
– Plotly has hover tool capabilities that allow us to detect any outliers or
anomalies in a large number of data points.
– It is visually attractive that can be accepted by a wide range of audiences.
– It allows us for the endless customization of our graphs that makes our plot
more meaningful and understandable for others.
Package Structure of Plotly
• There are three main modules in Plotly. They are:
– plotly.plotly
– plotly.graph.objects
– plotly.tools
• plotly.plotly acts as the interface between the local machine and Plotly.
– It contains functions that require a response from Plotly’s server.
• plotly.graph_objects module contains the objects (Figure, layout, data,
and the definition of the plots like scatter plot, line chart) that are
responsible for creating the plots.
• The Figure can be represented either as dict or instances of
plotly.graph_objects.Figure and these are serialized as JSON before it gets
passed to plotly.js. Consider the below example for better understanding.
Bar Plot using Plotly
• A bar chart presents categorical data with rectangular bars with heights or
lengths proportional to the values that they represent.
• Bars can be displayed vertically or horizontally. It helps to show comparisons
among discrete categories.
• One axis of the chart shows the specific categories being compared, and the
other axis represents a measured value.
• Example:
– Following example plots a simple bar chart about number of students
enrolled for different courses.
– The go.Bar() function returns a bar trace with x coordinate set as list of
subjects and y coordinate as number of students.
Bar Plot: Example
• import plotly.graph_objs as go
• langs = ['C', 'C++', 'Java', 'Python', 'PHP']
• students = [23,17,35,29,12]
• data = [go.Bar( x = langs, y = students )]
• fig = go.Figure(data=data)
• iplot(fig)
Bar Plot: Example 2
• To display a grouped bar chart, the
barmode property of Layout object
must be set to group.
Bar Plot: Example
• To display a grouped bar chart, the
barmode property of Layout object
must be set to stack.
Pie using Plotly
• A Pie Chart displays only one series of data.
• Pie Charts show the size of items (called wedge) in one data series,
proportional to the sum of the items.
• Data points are shown as a percentage of the whole pie.
• The pie() function in graph_objs module – go.Pie(), returns a Pie
trace.
• Two required arguments are labels and values.
• import plotly.graph_objs as go
• langs = ['C', 'C++', 'Java', 'Python', 'PHP']
• students = [23,17,35,29,12]
• trace = go.Pie(labels = langs, values = students)
• data = [trace]
• fig = go.Figure(data = data)
• iplot(fig)
Sea Born Library
• Seaborn is an amazing visualization library for statistical
graphics plotting in Python.

• It provides beautiful default styles and color palettes to


make statistical plots more attractive.

• It is built on top matplotlib library and is also closely


integrated with the data structures from pandas.
Different categories of plot in Seaborn
• Relational plots: This plot is used to understand the relation
between two variables.
• Categorical plots: This plot deals with categorical variables and
how they can be visualized.
• Distribution plots: This plot is used for examining univariate and
bivariate distributions
• Regression plots: The regression plots in Seaborn are primarily
intended to add a visual guide that helps to emphasize patterns in
a dataset during exploratory data analyses.
• Matrix plots: A matrix plot is an array of scatterplots.
• Multi-plot grids: It is a useful approach to draw multiple
instances of the same plot on different subsets of the dataset.
Bar Plot using Seaborn Library
• seaborn.barplot() method is used to draw a
barplot.
• Parameters : This method is accepting the following
parameters that are described below :
• x, y : This parameter take names of variables in data or
vector data, Inputs for plotting long-form data.
• hue : (optional) This parameter take column name for
colour encoding.
• data : (optional) This parameter take DataFrame, array, or
list of arrays, Dataset for plotting. If x and y are absent,
this is interpreted as wide-form. Otherwise it is expected to
be long-form.
• color : (optional) This parameter take matplotlib color,
Color for all of the elements, or seed for a gradient palette.
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')

# class v / s fare barplot


sns.barplot(x = 'class', y = 'fare', data = df)

# Show the plot


plt.show()
Scatter Plot using Seaborn library
• Scatterplot can be used with several
semantic groupings which can help to
understand well in a graph.

• They can plot two-dimensional graphics that


can be enhanced by mapping up to three
additional variables while using the semantics
of hue, size, and style parameters.
Parameters:
• x, y: Input data variables that should be numeric.

• data: Dataframe where each column is a variable and each row is an observation.

• size: Grouping variable that will produce points with different sizes.

• style: Grouping variable that will produce points with different markers.

• palette: Grouping variable that will produce points with different markers.

• markers: Object determining how to draw the markers for different levels.

• alpha: Proportional opacity of the points.


import seaborn

seaborn.set(style='whitegrid')
fmri =
seaborn.load_dataset("fmri")

seaborn.scatterplot(x="timepoin
t",
y="signal",
hue="region
",
style="even
t",
data=fmri)
R language
• R is a popular programming language used for statistical computing and
graphical presentation.
• Its most common use is to analyze and visualize data.
• It is a great resource for data analysis, data visualization, data science and
machine learning
• It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction)
• It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter
plot, etc++
• It works on different platforms (Windows, Mac, Linux)
• It is open-source and free
Data Visualization using R
• Bar Plot
– Use the barplot() function to draw a vertical bar chart.
• Example:
– # x-axis values
x <- c("A", "B", "C", "D")

# y-axis values
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x)
• The x variable represents values in the x-axis (A,B,C,D)
• The y variable represents values in the y-axis (2,4,6,8)
• Then we use the barplot() function to create a bar chart of the
values
• names.arg defines the names of each observation in the x-
axis.
• Other parameters
– Use the col parameter to change the color of the bars.
– To change the bar texture, use the density parameter,
density=10.
– If you want the bars to be displayed horizontally instead of
vertically, use horiz=TRUE
• Pie Plot
– Use the pie() function to draw pie charts.
– By default, the plotting of the first pie starts from the x-
axis and move counterclockwise.
– The size of each pie is determined by comparing the value
with all the other values, by using this formula:
• The value divided by the sum of all values: x/sum(x)
• Example:
– # Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart


pie(x)
• We can change the start angle of the pie chart with the init.angle parameter.
• The value of init.angle is defined with angle in degrees, where default angle
is 0.
• Use the label parameter to add a label to the pie chart, and use the main
parameter to add a header.
• Example:
– # Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels


mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Display the pie chart with labels


pie(x, label = mylabel, main = "Fruits")
• We can add a color to each pie with the col parameter.
• Example:
– # Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors


pie(x, label = mylabel, main = "Fruits", col = colors)
• To add a list of explanation for each pie, use the legend() function.
– The legend can be positioned as either:
• bottomright, bottom, bottomleft, left, topleft, top, topright, right, center
• Example:
– # Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Create a vector of colors


colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors


pie(x, label = mylabel, main = "Pie Chart", col = colors)

# Display the explanation box


legend("bottomright", mylabel, fill = colors)

You might also like