Lec 3.2 .2 Data Visualization Using Polty
Lec 3.2 .2 Data Visualization Using Polty
Lec 3.2 .2 Data Visualization Using Polty
PYTHON PROGRAMMING
2
Contents
• Plotly: gantt
• Plotly: heatmap
• Plotly: 3D graph
3
Plotly: Line
• Plotly is an open-source plotting library in Python. Python users can
use Plotly to generate different types of interactive web-based charts
including scientific charts, 3D graphs, statistical charts, financial
charts, etc.
• For Example :
4
Plotly: Line
• Poltly : Line
Follow the steps given below to generate a multiple line chart using
Plotly Express.
• Step 1
Import the plotly.express module and alias as px.
import plotly.express as px
• Step 2
Create a dataset with the following values −
5
Plotly: Line
data = {
'year':[2019,2020,2021,2022],
'loss':[0,1,2,3],
'gain':[90,91,92,93],
'profit':[100,90,95,97]
}
df = pd.DataFrame(data)
• Step 3
6
Plotly: Line
Use the px.line() method to create a line plot.
fig = px.line(df, x='year', y='loss')
• Step 4
Use the add_scatter() method to generate two scatter plots.
# generate scatter plot
fig.add_scatter(x=df['year'], y=df['gain'])
fig.add_scatter(x=df['year'], y=df['profit'])
7
Plotly: Bar
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.
For Example :
import plotly.graph_objs as go
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
data = [go.Bar(
8
Plotly: Bar
x = langs,
y = students
)]
fig = go.Figure(data=data)
iplot(fig)
9
Plotly: Scatter
10
Plotly: Scatter
import plotly.express as px
import numpy as np
# creating random data through randomint
# function of numpy.random
np.random.seed(42)
random_x= np.random.randint(1,101,100)
random_y= np.random.randint(1,101,100)
plot = px.scatter(random_x, random_y)
plot.show()
11
Plotly: Histogram
A histogram is a graph where the data are stocked and the each stocked
is counted and represented. More broadly, in plotly a histogram is an
accumulated bar chart, with several possible accumulation functions.
The data to be stocked can be numerical data but also categorical or date
data
. It is commonly used in dealing with large data of sets.
For Example :
12
Plotly: Histogram
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill")
fig.show()
13
Plotly:Violin
Violin Plot is a method to visualize the distribution of numerical data of different
variables. It is similar to Box Plot but with a rotated plot on each side, giving more
information about the density estimate on the y-axis.
The density is mirrored and flipped over and the resulting shape is filled in, creating
an image resembling a violin.
The advantage of a violin plot is that it can show nuances in the distribution that
aren’t perceptible in a boxplot. On the other hand, the boxplot more clearly shows
the outliers in the data.
14
Plotly:Violin
Example
import plotly.express as pt
import pandas as pd
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
data = df.head()
fig = pt.violin(data, y="Year")
fig.show()
15
Plotly:gantt
A Gantt chart is parallel to bar chart that delineates a project schedule. In Gantt
chart tasks are performed in the vertical axis and the time intervals are performed in
the horizontal axis.
The width of the horizontal bars in the graph shows the endurance of both the
activity.
A Gantt chart is made up of numerous different elements. the main 8 key
components are:
• Task list
• Timeline
16
Plotly:gantt
• Date line
• Bars
• Milestones
• Dependencies
• Progress
• Resource assigned
• For Example :
17
Plotly:gantt
For Example :
import plotly.figure_factory as ff
fig = ff.create_gantt(df)
fig.show()
18
Plotly: Heatmap
19
Plotly:3D Graph
To create a 3D graph using Plotly in Python, you can follow these steps:
• Install Plotly
• Import the necessary modules
• Prepare the data
• Create the 3D surface plot
• Customize the plot
• Display the plot
20
Plotly:3D Graph
For Example:
import plotly.graph_objects as go
x_values = [1, 2, 3, 4, 5]
y_values = [1, 2, 3, 4, 5]
z_values = [
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
21
Plotly:3D Graph
[4, 5, 6, 7, 8],
[5, 6, 7, 8, 9]
]
fig = go.Figure(data=[go.Surface(x=x_values, y=y_values, z=z_values)])
fig.update_layout(
title="3D Surface Plot",
scene=dict(
22
Plotly:3D Graph
scene=dict(
xaxis_title="X values",
yaxis_title="Y values",
zaxis_title="Z values"
)
)
fig.show()
23
THANK YOU
24