How to create visualizations using Plotly

Data visualization is a scientific method of displaying essential information/insights from datasets in a visual format. This makes the findings more interactive and allows an organization/group to understand data more vividly and discover deeper patterns in the provided information.

Benefits of using Plotly

The most notable difference between Plotly and other plotting libraries of Python (pyplot, seaborn, etc.) is that they can only generate static visualizations. As a result, we lose crucial information. Plotly resolves this issue and allows us to add interactivity, such as hovering over or zooming in on the visualization created.

Here are some significant benefits of using Plotly:

  • Plotly is an open-source library providing increased agility and the ability to generate high-quality visualizations.
  • Plotly enables us to create a diverse range of visualizations, such as scatter plots, box plots, density plots, histograms, etc.
  • Plotly provides support to multiple popular coding languages such as Python, R, etc.

Now, we will create some commonly used graphs/charts using Plotly.

Note: We will be using a sample dataset from Kaggle to create our visualizations.

Our sample data is displayed in the widget below:

df = pd.read_csv("Cars93.csv")
df.head()

Installing Plotly

Execute the following command in the terminal to install the Plotly module:

pip install plotly

We are now ready to generate different graphs/charts using the Plotly module and dataset we imported earlier.

Box plot

A box plot is a visual representation that distributes data according to five metrics: the minimum value, the first quartile (Q1), the median value (Q2), the third quartile (Q3), and the maximum value.

The following working example creates a box plot for the Price feature of the cars dataset:

fig = px.box(df, y="Price")
fig.layout.title='Box Plot of Price'
fig.show()

  • Line 1: This command creates the box plot based on the Price feature using the dataset we displayed earlier.

  • Line 2: We add a title to our box plot.

  • Line 3: We display our box plot.

Note: We can interact with the box plot created above and view the quartile values by hovering over the plot.

Bar chart

Bar charts are commonly used to display discrete and categorical data. The x-axis lists the categorical variables being compared, and the y-axis measures the count or frequency of the respective variables.

The following example creates a bar chart for the Type feature of the dataset:

df = df.groupby('Type')['Type'].agg(Frequency='count').reset_index()
fig = px.bar(df, x="Type", y = "Frequency")
fig.layout.title='Barplot of car Type'
fig.layout.xaxis.title = 'Type'
fig.layout.yaxis.title='count'
fig.show()
  • Line 1: We group the entries in the dataset based on the Type feature.

  • Line 2: We create our bar chart with the Type feature on the x-axis and the count feature on the y-axis.

  • Lines 3–5: We add the chart, x-axis, and y-axis titles.

  • Line 6: We display our bar chart.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved