#pip install plotly ##uncomment this line to install package
#pip install --upgrade plotly ##uncomment this line to upgrade plotly
DATA VISUALIZATION
Data visualization is the representation of data through use of common graphics, such as charts, plots, infographics, and even animations. These visual displays of information communicate complex data relationships and data-driven insights in a way that is easy to understand.
Some of the famous libraries used for data visualization are:
MATPLOTLIB
SEABORN
PLOTLY
# This is formatted as code
Plotly is an open-source module of Python that is used for data visualization and supports various graphs like line charts, scatter plots, bar charts, histograms, area plots, etc. Plotly produces interactive graphs, can be embedded on websites, and provides a wide variety of complex plotting options.
LINE CHART IN PLOTLY
Line chart A line chart is one of the simple plots where a line is drawn to show relation between the X-axis and Y-axis. It can be created using the px.line() method with each data position is represented as a vertex (which location is given by the x and y columns) of a polyline mark in 2D space.
Syntax:
Syntax: plotly.express.line(data_frame=None, x=None, y=None, line_group=None, color=None, line_dash=None, hover_name=None, hover_data=None, title=None, template=None, width=None, height=None)
import warnings
warnings.filterwarnings('ignore')
import plotly.express as px
import pandas as pd
import numpy as np
# Loading the data
df = px.data.tips()
df.head()
import plotly.express as px
# Loading the data
df = px.data.tips()
# Creating the bar chart
# plotting the line chart
fig = px.line(df, y="total_bill")
# showing the plot
fig.show()
import plotly.express as px
# using the iris dataset
df = px.data.tips()
# plotting the line chart
fig = px.line(df, y="total_bill", line_dash='time',
color='sex')
# showing the plot
fig.show()
BAR CHART IN PLOTLY
A bar chart is a pictorial representation of data that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. In other words, it is the pictorial representation of dataset. These data sets contain the numerical values of variables that represent the length or height. It can be created using the px.bar() method.
Syntax:
plotly.express.bar(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, hover_name=None, hover_data=None, custom_data=None, text=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, title=None, template=None, width=None, height=None, **kwargs)
import plotly.express as px
# Loading the data
df = px.data.tips()
# Creating the bar chart
fig = px.bar(df, x='day', y="total_bill")
fig.show()
import plotly.express as px
# Loading the data
df = px.data.tips()
# Creating the bar chart
fig = px.bar(df, x='day', y="total_bill", color='sex',
facet_row='time', facet_col='sex')
fig.show()
##SCATTER PLOT IN PLOTLY
A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and vertical axis. A graph in which the values of two variables are plotted along X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. it can be created using the px.scatter() method.
Syntax:
plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, facet_row=None, facet_col=None, facet_col_wrap=0, opacity=None, title=None, template=None, width=None, height=None, **kwargs)
# x and y given as array_like objects
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the scatter chart
fig = px.scatter(df, x='total_bill', y="tip")
# showing the plot
fig.show()
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the scatter chart
fig = px.scatter(df, x='total_bill', y="tip", color='time',
symbol='sex', size='size', facet_row='day',
facet_col='time')
# showing the plot
fig.show()
PIE CHART IN PLOTLY
A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. It depicts a special chart that uses “pie slices”, where each sector shows the relative sizes of data. A circular chart cuts in a form of radii into segments describing relative frequencies or magnitude also known as a circle graph. It can be created using the px.pie() method.
Syntax:
plotly.express.pie(data_frame=None, names=None, values=None, color=None, color_discrete_sequence=None, color_discrete_map={}, hover_name=None, hover_data=None, custom_data=None, labels={}, title=None, template=None, width=None, height=None, opacity=None, hole=None)
import plotly.express as px
# Loading the tips dataset
df = px.data.tips()
fig = px.pie(df, values="total_bill", names="day")
fig.show()
import plotly.express as px
# Loading the tips dataset
df = px.data.tips()
fig = px.pie(df, values="total_bill", names="day",
color_discrete_sequence=px.colors.sequential.RdBu,
opacity=0.7, hole=0.5)
fig.show()
BOX pLOT IN PLOTLY
A Box Plot is also known as Whisker plot is created to display the summary of the set of data values having properties like minimum, first quartile, median, third quartile and maximum. In the box plot, a box is created from the first quartile to the third quartile, a vertical line is also there which goes through the box at the median. Here x-axis denotes the data to be plotted while the y-axis shows the frequency distribution. It can be created using the px.box() method
Syntax:
plotly.express.box(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, title=None, template=None, width=None, height=None, **kwargs)
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the boxplot
fig = px.box(df, x="day", y="tip")
# showing the plot
fig.show()
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the boxplot
fig = px.box(df, x="day", y="tip", color='sex',
facet_row='time', boxmode='group',
notched=True)
# showing the plot
fig.show()
VIOLIN PLOT IN PLOTLY
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. It can be created using the px.violin() method.
Syntax:
violin(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, hover_name=None, hover_data=None, title=None, template=None, width=None, height=None, **kwargs)
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the violin plot
fig = px.violin(df, x="day", y="tip")
# showing the plot
fig.show()
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the violin plot
fig = px.violin(df, x="day", y="tip", color='sex',
facet_row='time', box=True)
# showing the plot
fig.show()
A histogram is representation of the distribution of numerical data, where the data are binned and the count for each bin is represented. More generally, in Plotly a histogram is an aggregated bar chart, with several possible aggregation functions (e.g. sum, average, count...) which can be used to visualize data on categorical and date axes as well as linear axes.
Alternative to a histogram plot is a VIOLIN plot.
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the histogram
fig = px.histogram(df, x="total_bill")
# showing the plot
fig.show()
import plotly.express as px
# using the dataset
df = px.data.tips()
# plotting the histogram
fig = px.histogram(df, x="total_bill", color='sex',
nbins=50, histnorm='percent',
barmode='overlay')
# showing the plot
fig.show()
**3D
# This is formatted as code
3D SCATTER PLOT IN PLOTLY
3D Scatter Plot 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. All the parameter control visual semantic which are used to identify the different subsets. Using redundant semantics can be helpful for making graphics more accessible. It can be created using the scatter_3d function of plotly.express class.
Syntax:
plotly.express.scatter_3d(data_frame=None, x=None, y=None, z=None, color=None, symbol=None, size=None, range_x=None, range_y=None, range_z=None, title=None, template=None, width=None, height=None, **kwargs)
import plotly.express as px
# data to be plotted
df = px.data.tips()
# plotting the figure
fig = px.scatter_3d(df, x="total_bill", y="sex", z="tip")
fig.show()
import plotly.express as px
# data to be plotted
df = px.data.tips()
# plotting the figure
fig = px.scatter_3d(df, x="total_bill", y="sex", z="tip", color='day',
size='total_bill', symbol='time')
fig.show()
HEATMAP IN PLOTLY
Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors basically reddish colors are used and to represent less common or activity values, darker colors are preferred.
NOTE: We usually use correlation function to build the matrix.
Features of the heatmap plot in Plotly Graph Objects The following are some key features of heatmap plots using Plotly Graph Objects:
Data input: Heatmap plots typically require a 2D array or a data frame where each cell represents a value that will be color-coded.
Color scale: We can choose a color scale to map values to colors. Plotly provides a variety of predefined color scales, and you can also define custom color scales.
Annotations: We can add text annotations to individual cells, which can be useful for displaying the exact values associated with each cell.
Axes labels: Heatmap plots often have labeled axes for rows and columns, which makes understanding the represented data easier.
Title: We can add a title to our heatmap plot to provide context or to clarify what the plot is illustrating.
Color bar: A color bar can be added to the plot to show the correspondence between colors and values. This is particularly useful when conveying the data's numeric meaning.
Customization of colors: We can customize the colors used in the heatmap, including specifying the color for missing or out-of-range values.
Row and column order: We can control the order of rows and columns, which can be important when you want to emphasize certain patterns in the data.
Scaling: We can scale the color mapping based on various criteria, such as row-wise, column-wise, or globally, which can help highlight different aspects of the data.
Hover text: We can include hover text that appears when you hover over individual cells, providing additional information or context.
Z-score normalization: We can normalize the values in the heatmap using Z-score normalization to standardize data and highlight relative differences.
Subplots: We can include multiple heatmaps in a single subplot, easily comparing related datasets.
###Plotting simple heatmap without color scale
import plotly.figure_factory as ff
z = [[.1, .3, .5, .7, .9],
[1, .8, .6, .4, .2],
[.2, 0, .5, .7, .9],
[.9, .8, .4, .2, 0],
[.3, .4, .5, .7, 1]]
fig = ff.create_annotated_heatmap(z, zauto=True)
fig.show()
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
"""For creating a heatmap with colorbar use the code given below."""
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
# Load the tips dataset
df = px.data.tips()
# Display the first five rows of the data
print(df.head())
# Heatmap
heatmap_trace = go.Heatmap(
z=df.corr(numeric_only=True),
x=df.columns,
y=df.columns,
colorscale='Blues',
zmin=-1,
zmax=1
)
# Create figure and add trace
fig = go.Figure(heatmap_trace)
# Update layout
fig.update_layout(title='Heatmap of Correlation Matrix')
# Display the figure
fig.show()