Creating Different Types of Charts

With plotly we can create more than 40 charts and every plot can be created using the plotly.express and plotly.graph_objects class. Let’s see some commonly used charts with the help of Plotly.

Line Chart

Line plot in Plotly is much accessible and illustrious annexation to plotly which manage a variety of types of data and assemble easy-to-style statistic. With px.line 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.

Example:

Python3




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


Output:

Refer to the below articles to get detailed information about the line charts.

Bar Chart

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.

Example:

Python3




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()


Output:

Refer to the below articles to get detailed information about the bar chart.

Histograms

A histogram contains a rectangular area to display the statistical information which is proportional to the frequency of a variable and its width in successive numerical intervals. A graphical representation that manages a group of data points into different specified ranges. It has a special feature that shows no gaps between the bars and similar to a vertical bar graph.

Example:

Python3




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


Output:

Refer to the below articles to get detailed information about the histograms.

Scatter Plot and Bubble charts

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.

A bubble plot is a scatter plot with bubbles (color-filled circles). Bubbles have various sizes dependent on another variable in the data. It can be created using the scatter() method of plotly.express.

Example 1: Scatter Plot

Python3




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


Output:

Example 2: Bubble Plot

Python3




import plotly.express as px
 
# using the iris dataset
df = px.data.iris()
 
# plotting the bubble chart
fig = px.scatter(df, x="species", y="petal_width",
                 size="petal_length", color="species")
 
# showing the plot
fig.show()


Output:

Refer to the below articles to get detailed information about the scatter plots and bubble plots.

Pie Charts

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 circle graph.

Example:

Python3




import plotly.express as px
 
# using the tips dataset
df = px.data.tips()
 
# plotting the pie chart
fig = px.pie(df, values="total_bill", names="day")
 
# showing the plot
fig.show()


Output:

Refer to the below articles to get detailed information about the pie charts.

Box Plots

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.

Example:

Python3




import plotly.express as px
 
# using the tips dataset
df = px.data.tips()
 
# plotting the box chart
fig = px.box(df, x="day", y="total_bill")
 
# showing the plot
fig.show()


Output:

Refer to the below articles to get detailed information about box plots.

Violin plots

A 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.

Example:

Python3




import plotly.express as px
 
# using the tips dataset
df = px.data.tips()
 
# plotting the violin chart
fig = px.violin(df, x="day", y="total_bill")
 
# showing the plot
fig.show()


Output:

Refer to the below articles to get detailed information about the violin plots

Gantt Charts

Generalized Activity Normalization Time Table (GANTT) chart is type of chart in which series of horizontal lines are present that show the amount of work done or production completed in given period of time in relation to amount planned for those projects. 

Example:

Python3




import plotly.figure_factory as ff
 
# Data to be plotted
df = [dict(Task="A", Start='2020-01-01', Finish='2009-02-02'),
    dict(Task="Job B", Start='2020-03-01', Finish='2020-11-11'),
    dict(Task="Job C", Start='2020-08-06', Finish='2020-09-21')]
 
# Creating the plot
fig = ff.create_gantt(df)
fig.show()


Output:

Refer to the below articles to get detailed information about the Gantt Charts.

Contour Plots

A Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables we want to plot then the response Z will be plotted as slices on the X-Y plane due to which contours are sometimes referred as Z-slices or iso-response.

A contour plots is used in the case where you want to see the changes in some value (Z) as a function with respect to the two values (X, Y). Consider the below example.

Example:

Python3




import plotly.graph_objects as go
 
 
# Creating the X, Y value that will
# change the values of Z as a function
feature_x = np.arange(0, 50, 2)
feature_y = np.arange(0, 50, 3)
 
# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)
 
Z = np.cos(X / 2) + np.sin(Y / 4)
 
# plotting the figure
fig = go.Figure(data =
    go.Contour(x = feature_x, y = feature_y, z = Z))
 
fig.show()


Output:

Refer to the below articles to get detailed information about contour plots.

Plotly tutorial

Plotly library in Python 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, box plots, etc. So you all must be wondering why Plotly is over other visualization tools or libraries. So here are some reasons :

  • 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 and can be accepted by a wide range of audiences.
  • Plotly generally allows us endless customization of our graphs and makes our plot more meaningful and understandable for others.

This tutorial aims at providing you the insight about Plotly with the help of the huge dataset explaining the Plotly from basics to advance and covering all the popularly used charts.

Table of Content

  • How to install Plotly?
  • Package Structure of Plotly
  • Getting Started
  • Creating Different Types of Charts
  • Heatmaps
  • Error Bars
  • 3D Line Plots
  • 3D Scatter Plot Plotly
  • 3D Surface Plots
  • Interacting with the Plots
  • Adding Buttons to the Plot
  • Creating Sliders and Selectors to the Plot
  • More Plots using Plotly

Similar Reads

How to install Plotly?

Before installing Plotly in system, you need to install pip in your system, Refer to –...

Package Structure of Plotly

There are three main modules in Plotly. They are:...

Getting Started

...

Creating Different Types of Charts

After learning the installation and basic structure of the Plotly, let’s create a simple plot using the pre-defined data sets defined by the plotly....

Heatmaps

...

Error Bars

With plotly we can create more than 40 charts and every plot can be created using the plotly.express and plotly.graph_objects class. Let’s see some commonly used charts with the help of Plotly....

3D Line Plots

...

3D Scatter Plot Plotly

...

3D Surface Plots

...

Interacting with the Plots

...

Adding Buttons to the Plot

...

Creating Sliders and Selectors to the Plot

...

More Plots using Plotly

...

Contact Us