Guide to Create Interactive Plots with Plotly Python

prateekmaj21 Last Updated : 05 Jul, 2024
18 min read

Introduction

Data visualization, a pivotal element in data science, simplifies complex information, allowing quick comprehension and pattern recognition. Charts and graphs make data analysis effortless, aiding in strategic decisions and business growth. Versatile in application, they support sales forecasts, stock analysis, project management, and more. Visuals, like technology, are integral to modern life, enhancing our understanding of the world. Our eyes are adept at extracting insights from visuals, which communicate efficiently and captivate attention. While spreadsheets house data, visual representations transform it into accessible, meaningful narratives. In this article, we will be looking at creating interactive plots using Plotly python.

Learning Objectives

  • Understand the significance of data visualization and its various applications.
  • Learn how interactive visualization enhances data interactivity.
  • Gain practical knowledge of data visualization in Python using libraries like pandas, Plotly, and more.

This article was published as a part of the Data Science Blogathon

What Is Data Visualization?

Data visualization is the representation of data through the use of common graphics, such as charts, plots, infographics, and even animations. Findings and visuals of data need to be properly presented as well. Data visuals must attract the attention of the audience and convey the appropriate message. It doesn’t matter which department or sector; good data analysis and visualization are great things and will be in high demand in the days to come. Whether in finance, marketing, sales, technology, engineering, research, or human resources, data analysis and visualization will be more important in the coming days. Data must convey a specific message and explain things clearly; good visuals often make the process easy and simple.

Machine Learning helps in making predictions, doing predictive analytics, and other tasks; similarly, good visualizations help in data exploration.

Some Practical Uses of Data Visualization

There are various types of data visualizations, and all of them have different purposes and needs.

Tracking Changes in Data Overtime

Data that has a timestamp associated with it is considered to be time-series data. Examples of such data can be stock prices, sales data over time, rainfall and temperature at a place along with the time, road traffic at a particular place, and so on. Such data helps in tracking trends and changes over time. We might want to analyze stock prices or see which day of the week traffic is highest, and so on. So, we check the data trends over time.

Understanding Correlations

Data is highly related. The sales of a supermarket are highly dependent on the vehicular traffic on the road in front of the supermarket. The number of hours of study in the case of students might lead to higher test scores and so on. So, data visualizations must be such that analysts and users can be aware of relationships in data.

Determining Frequency

The count and frequency of items are important, and we need to keep track of them. We need to keep track of how frequently something happens. Many data visualizations help in determining frequency.

Analysing Importance/ Risk/ Value

Many data visualizations are made to analyze the distribution of a particular variable and see any importance, risk or value the data might hold. Data can be tested with various metrics and plotted to understand all the important parameters. Effective data visualization is a crucial step in analytics.

Increasing Importance of Data Analytics

The amount of data and information on the internet is increasing day by day. Our every action online is stored as data. Be it our website login, our purchase, an uber trip taken, or online food delivery – everything is tracked. We can confirm that the age of Big Data is almost here. The vast amount of data needs processing and analysis. Data has to be made more understandable, readable, and interpretable. The real-life applications and uses of good data visualization methods are immense. A data-driven organization leverages data to make efficient decisions and will surely perform better than an organization that does not leverage data.

The advent of large data storage facilities has made data available for various purposes. Large organizations like Google, Facebook, Amazon, etc., leverage their data for a wide variety of purposes. Improved business decisions are a direct outcome of data-driven decision-making.

How Can Data Visualization Help?

  • Good visuals help in capturing the attention of the audience, and they can understand stuff better.
  • Makes data exploration more accessible and easy.
  • Data insights and visuals can be shared easily.
  • Understand the information more easily.
  • Gain better insights.
  • Make swift data-driven decisions.

Sample Problem Statement

Let us consider a hypothetical scenario. A teacher has the marks of all students in her class. Along with it, she has data for students’ past marks and other grades. All of this data is, however, in spreadsheets. Now, she wants to analyze from her data which students are performing the best, which students’ exam performance has improved, and which students’ performance has decreased, and so on. All this might be possible with spreadsheets, but the amount of effort that needs to be given is too high.

Thankfully, excel has in-built data visualization tools, and the data can be analyzed simply and easily. The teacher can easily check all the data, find out who had the highest score, etc. Data Analysis tools are there to help us in such aspects. Nowadays, we are at liberty to use Excel, Power BI, and Tableau as no-code solutions, and we can also use Python and R if we want custom solutions and data pipelines. These tools serve the purpose of processing the data and making our desired visuals. The use of such tools helps us in automating the data visualization process.

History of Data Visualization

Data tells its tale: properly presented data can explain a lot of things. One of the first known graphs of statistical data was made by Dutch astronomer Michael Florent van Langren. Napoleon Bonaparte’s Russian campaign of 1812 was mapped by Charles Joseph Minard. He used statistical graphs to map the campaign, and he combined multiple metrics: the number of troops, temperature, distance, directions, and more to make a proper visual.

Over time, data visualization kept on improving. The advent of computers and displays meant that data could be processed and presented efficiently. Data Visualization tools and software can analyze vast amounts of data at a very high speed.

Use of Python in Data Visualization

Python is an excellent tool for data visualization. Python has been around for a long time and can be used for a wide variety of tasks. It can be used for statistical analysis, machine learning, deep learning, web development, and so on. The easy-to-use nature of Python and a lot of libraries make Python useful for complex numeric and scientific calculations. The uses of Python are immense. The popularity of using Python is also increasing.

Python is open-source and free to use, and there are a lot of libraries and support available for Python. Python can also be used on many platforms. It has many support forums and helps available all over the internet. The great community support for Python and the large number of resources make learning Python for data analysis a great investment. It is flexible, scalable, and has a wide range of libraries and regular updates available. This can lower the data analysis budget and costs. Purchasing licenses for Power BI and Tableau can be expensive in earlier stages. The libraries in Python are constantly evolving, making the process easier and simpler. The data-oriented packages in Python can speed up and simplify the entire data process.

The data analysts’ toolbox can have many tools: Power BI, Tableau, Excel, R, etc., but Python must also be a part of it. The hyper flexibility of Python makes it very useful, and it is highly popular among data analysts and data scientists. Python has many IDEs and environments where data can be visualized. One can use Google Colab, Kaggle Kernel, Jupyter Notebooks, and so on. The graphical options in Python make using Python very easy for data analysis. Python is evolving constantly, is multi-featured, and is highly functional.

Libraries in Python for Data Analysis

Python started as a general-purpose programming language. But, the improved readability of Python made it a good tool for data analysis.

Matplotlib

One of the best tools for data analysis is Matplotlib. It is used for 2-dimensional data analysis and basic plotting, charting, and data representation. It was introduced in 2002 by John Hunter. The introduction of Matplotlib propelled the growth of Python as a tool for research, data analysis, and engineering. The visuals are easy to plot and interpret. For example, you can plot bar graphs, line charts, etc.

Seaborn

Seaborn is a great visualization library in Python used for plotting statistical models and complex relations among data. It can plot complex plots like Heatmaps, Relational Plots, Categorical Plots, Regression Plots, etc. Seaborn made complex data analysis and visualization easy and simple to execute.

Now, if we consider the limitations of Seaborn and Matplotlib, first of all, they are static plots. The plots are produced as images, and they are not interactive. We cannot hover our cursor over the plots and get exact values. We cannot also use them to make interactive plots on websites. A good solution to all this is using Plotly.

Plotly

Plotly is a Montreal-based AI and Analytics company. They focus on the development of Analytics tools, mainly Dash and Chart Studio. They have also released the free and open-source plotting library “Plotly” for Python, R, MatLab, and Julia.

Plotly produces interactive graphs, can be embedded on websites, and provides a wide variety of complex plotting options. The graphs and plots are robust, and a wide variety of people can use them. The visuals are of high quality and easy to read and interpret.

Plotly makes a wide variety of charts, including basic and statistical charts, maps, 3D charts, subplots, and more.

Getting Started With Plotting in Plotly

I have prepared and kept the syntax in a Kaggle Notebook, I will leave the link for GitHub later. Please refer to it later so that you are able to understand it. First, we import the necessary libraries.

import numpy as np 
import pandas as pd 
import plotly.express as px

Now, we read some data we will be using.

The two datasets used here are:

  • Melbourne Housing Snapshot
  • Superstore Sales Dataset

Both datasets are good beginner datasets, with a lot of information and data fields. The Melbourne Housing data has various real estate data points and deals with the housing sector. The data pertains to the housing and commercial property sector.

The superstore data concerned with sales and the retail sector. Various aspects of sales and retail are present in the data.

Now, we proceed with reading the data.

melb= pd.read_csv("/kaggle/input/melbourne-housing-snapshot/melb_data.csv")
sales=pd.read_csv("/kaggle/input/sales-forecasting/train.csv")

The Melbourne data is a bit large. For the sake of simplicity, we are taking only 1000 data points from the dataset.

melb=melb[0:1000]

Scatter Plots Using Plotly

Scatterplots are a great way to analyze data distribution and the relation between various data fields. Various trends in data can be analyzed and plotted on the x-axis and y-axis. Plotting scatter plots with Plotly is very easy.

x=[0, 1, 2, 3, 4, 5, 6]
y=[0, 2, 4, 5, 5.5, 7, 9]
fig = px.scatter(x, y)
fig.show()
Scatter Plots using Plotly

The good thing about Plotly is that the plots are interactive. We can hover over the plots and see exact data values and other information. I will share the link to the notebook, where you can have a look. Also, do upvote the Kaggle Notebook if you like it.

We take the iris dataset now.

Let us make a scatter plot to understand the data distribution.

Making some changes to the parameters.

fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_width')
fig.show()
Scatter Plots using Plotly on iris data figure

Adding some styles to the plots.

fig = px.scatter(df, x="sepal_width", y="sepal_length", color='species')
fig.show()
sepal length Scatter Plots using Plotly on iris data

Start plotting some data using the Melbourne dataset.

fig = px.scatter(df, y="petal_length", x="petal_width", color="species", symbol="species")
fig.update_traces(marker_size=10)
update traces Scatter Plots using Plotly on iris data

Adding some columns to the plots.

fig = px.scatter(melb, x="Lattitude", y="Longtitude", marginal_x="histogram", marginal_y="rug",color="Type")
fig.show()
Scatter Plot on Melbourne dataset using Plotly

Now, let’s change the parameters.

fig = px.scatter(melb, x="Price", y="YearBuilt", color="Type", facet_col="Rooms", )
fig.show()
Scatter Plot using multiple columns using Plotly

We will now change the parameters.

fig = px.scatter(melb, x="Price", y="YearBuilt", color="Rooms", facet_col="Type", )
fig.show()
Changed parameters in scatter plot using Plotly
fig = px.scatter(melb, x="BuildingArea", y="Distance", color="Rooms", facet_col="Type", )
fig.show()
Changed parameters in scatter plot using Plotly
fig = px.scatter(melb, x="BuildingArea", y="Distance", color="Car", facet_col="Type", )
fig.show()
Changed parameters in scatter plot using Plotly

As we can see, all the plots in Plotly are really nice and well-designed. All the colors are great to look at and see.

Regarding scatterplots, we can also make Linear Regression plots using Plotly. We take the dips dataset, and we plot the linear relationship between total bills and tips.

#linear regression
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()
Linear plots using Plotly

We can see that the linear plot is quite well made, and all the plots are interactive.

Check the Kaggle notebook here: Link

Line Plots Using Plotly

Line plots are great for visualizing continuous data. Time series data, mathematical functions, etc., are some of the data which can be plotted using Line Plots. They reveal data trends, maxima, and minima. We can use them for time series data like stocks, sales over time, and so on. It is a great way to plot a 2D relationship.

Let us use a line plot to plot a mathematical function.

x = np.linspace(0, 10, 1000)
y= 3*x**2 - 2*x**2 + 4*x- 5
fig = px.line(x=x ,y =y,labels={'x':'x', 'y':'y'})
fig.show()
Line Plot in Plotly

The plot is interactive, so we can hover over it to understand the values.

Now, let us plot a sin() function.

 x = np.linspace(0, 10, 1000)
y= np.sin(x)
fig = px.line(x=x ,y =y,labels={'x':'x', 'y':'sin(x)'})
fig.show()
Plotting sin() function in Plotly

Now, we shall plot some time series data, starting with some stock data.

“MSFT” is the stock symbol for Microsoft.

df = px.data.stocks()
fig = px.line(df, x='date', y="MSFT")
fig.show()
Plotting time series data using Plotly

Now, I will include more stocks in the plot.

GOOG stands for Google, FB stands for Facebook, and AMZN stands for Amazon.

df = px.data.stocks()
fig = px.line(df, x='date', y=["MSFT","GOOG",'FB',"AMZN"])
fig.show()
Plotting Multivariate Time Series data in Plotly

We can see that all the plots are visually appealing and look nice with contrasting colors.

Now, we use some data from the Plotly library for some sample plotting.

df = px.data.gapminder().query("continent == 'Oceania'")

Let us check what the data looks like.

df.head()
Data from the Plotly library

We plot the data on a line plot now.

fig = px.line(df, x='year', y='pop', color='country')
fig.show()

We can see that the plot card also shows the data and other parameters on a convenient line plot. Now, add some markers so that the data is easily visible.

fig = px.line(df, x='year', y='pop', color='country',markers=True)
fig.show()
Adding markers to the Line plot using Plotly

The plot has been made!

Now, a plot with different types of visuals will be made.

import plotly.graph_objects as go
#combined plots
N=100
random_x = np.linspace(0, 5, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
                    mode='lines+markers',
                    name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
                    mode='markers',
                    name='markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
                    mode='lines',
                    name='lines'))
fig.show()

Such a type of plot is called a combined plot.

Combination plot using Plotly

Such combined plots are a great way to understand the data from different perspectives.

Bar Plots Using Plotly

Barplots are used to provide a straightforward comparison of data. They represent categorical data with rectangular bars of variable height. Plotting bar charts in a graphing library like Plotly is very easy and simple. Let us start by plotting the population of Australia over time.

df = px.data.gapminder().query("country == 'Australia'")
fig = px.bar(df, x='year', y='pop')
fig.show()
Bar Plot using Plotly

Let us work on the sales data we had taken earlier. But, for the sake of simplicity, we take only the initial 100 data points.

sales=sales[0:100]

Let us plot sales in each US State.

fig = px.bar(sales, x="State", y="Sales")
fig.show()
Ploting Sale data

It also individually shows the sales figure of each sale.

Now, we analyze the sales category, and for that, we bring in another parameter.

fig = px.bar(sales, x="State", y="Sales",color='Category')
fig.show()
ploting multiple categories on a Bar Plot using Plotly

Now, we plot the sales of each category and add a parameter to distinguish segments.

fig = px.bar(sales, x="Category", y="Sales",color='Segment')
fig.show()
Ploting the sales of each category and adding parameter

Next, we give a pattern shape to the plots.

fig = px.bar(sales, x="Category", y="Sales",color="Segment",pattern_shape="Segment", pattern_shape_sequence=[".", "x", "+"])
fig.show()
Adding patterns to the plot using Plotly

Now, let us add hues and more advanced colour interpretations to a plot. These improve the readability of the plot.

data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
fig = px.bar(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
             labels={'pop':'population of Canada'}, height=400)
fig.show()
Adding hues and advanced colour interpretations to a plot usig Plotly

We can clearly see here that with time, the population of Canada has increased, and also has the life expectancy. Better healthcare, improved medicines, and increased quality of life lead to this.

The hue of life expectancy becomes brighter, as shown in the color bar to the right.

Now, let us check the GDP per capita.

fig = px.bar(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='gdpPercap',
             labels={'pop':'population of Canada'}, height=400)
fig.show()
Checking GDP per capita with the help of Bar Plot

The GDP per capita improved over time, and we can take that as an indication that general life quality improved with time.

Let us make some stacked bar charts. One important thing to be considered while plotting and data representation are that we need to understand when to plot which data, and which data is important and when. Choosing the right type of chart is very important. This prevents any visualization mistakes.

Let us take into consideration some new data.

df = px.data.gapminder().query("continent == 'Oceania'")
df.head()
dataset

How we can plot the stacked bar charts.

fig = px.bar(df, x='year', y='pop',barmode='stack',color='country')
fig.show()
Plotting Stacked Bar chart using Plotly

Stacked bar charts show the summation of individual entries as well as the entire plot. So, it is a good way to understand the contribution of each individual factor toward a complete entity.

Let us see the life expectancy data.

fig = px.bar(df, x='year', y='lifeExp',barmode='stack',color='country')
fig.show()
Country wise life expectancy data

Now we will see custom visuals.

x = ['Suzuki', 'Honda', 'Tata']
y = [100, 40, 60]
# Use the hovertext kw argument for hover text
fig = go.Figure(data=[go.Bar(x=x, y=y,
            hovertext=['50 % Share', '20 % Share', '30 % Share'])])
fig.update_layout(title_text='Sales Data')
fig.show()
Adding hover text on a plot using Plotly

Let us plot the populations of the most populous nations in Asia.

#uniform text size
df = px.data.gapminder().query("continent == 'Asia' and year == 2007and pop > 8000000")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.show()
Ploting country wise population

So, we plotted a wide variety of bar plots and analyzed data. Let us try a different type of plot now.

Pie Chart Using Plotly

Pie charts are used to understand the composition of data and analyze part-to-whole relationships in data. Piecharts ( and doughnut charts) plot the percentage composition of a value as compared to the entire data/value.

Let us take into consideration the sales dataset again. We plot a pie chart of the sales from each state. The percentage contribution of each state will get plotted. This will show many valuable insights.

fig = px.pie(sales, values='Sales', names='State', title='Sales Per State in US')
fig.show()
Pie Chart using Plotly

So, we can see that the majority of the sales are from California.

Now, we plot the sales segments and their contribution.

Ploting Sales Segment in US

Now, we see the sales per category.

fig = px.pie(sales, values='Sales', names='Category', title='Sales Per Category in US')
fig.show()
Ploting Sales per Category in US

So, we can see that Furniture was sold the highest.

Now, we will make some more advanced plots, and we shall be using the tips dataset.

#setting colours
df = px.data.tips()
fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
Customized Pie Chart in Plotly

So, the plots are entirely customizable.

labels = ['Apple','Microsoft','Amazon','Alphabet']
values = [2252, 1966, 1711, 1538]
fig = go.Figure(data=[go.Pie(labels=labels, values=values, textinfo='label+percent',
                             insidetextorientation='radial'
                            )])
fig.show()
Pie chart in Plotly

Let us make a doughnut chart now.

#donut chart
labels = ['CAR','BIKE','BUS','TRAIN']
values = [1500, 2500, 6800, 9000]
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig.show()
Donut Chart in Plotly

The real difference between a doughnut chart and a pie chart is mainly the appearance and the way someone wants to plot the data.

Let us now make the chart a little bit customised.

#donut chart
labels = ['CAR','BIKE','BUS','TRAIN']
values = [1500, 2500, 6800, 9000]
fig = go.Figure(data=[go.Pie(labels=labels, values=values, pull=[0.1, 0.1, 0.2, 0.1])])
fig.show()
Customized Donut Chart using Plotly

So, we can see that Plotly offers a high level of customization and visually appealing plots.

Check out the code here: Kaggle

Bubble Charts Using Plotly

These Charts are a great way to show magnitude by adjusting the size of the circle. Bubble Charts can be easily made in Python.

fig = go.Figure(data=[go.Scatter(
    x=[1, 2, 3, 4], y=[10, 12, 15, 16],
    mode='markers',
    marker_size=[20, 40, 50, 60])
])
fig.show()
Bubble Chart

The plot is made easily.

df = px.data.gapminder()
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp",    size="pop", color="continent",
                 hover_name="country", log_x=True, size_max=60)
fig.show()
Output

Let us use the tips data again.

fig = px.scatter(tips, x="total_bill", y="size",    size="tip", color="tip",
                  size_max=20)
fig.show()
Ploting Tips data

Bubble charts are a great way to visualise data and understand insights.

Dot Plots Using Plotly

Dot Plots are a different way of presenting scatter plots and showing the data distribution properly.

We are taking a new dataset.

stud= pd.read_csv("/kaggle/input/students-performance-in-exams/StudentsPerformance.csv")

I will share the link to all codes in the end; please have a look there.

fig = px.scatter(stud, x="math score", y="parental level of education", color="gender",
                 title="Student Performance in Exams"
                )
fig.show()
Output

Let us try another plot.

fig = px.scatter(stud, x="writing score", y="parental level of education", color="lunch",
                 title="Student Performance in Exams"
                )
fig.show()
Output

Horizontal Bar Chart Using Plotly

Horizontal bar charts are just a way to interpret the traditional bar chart.

fig = px.bar(stud, x="reading score", y="parental level of education",color='gender', orientation='h')
fig.show()
Output

Gantt Chart

Gantt Chart is a special type of bar chart that shows the progress of a project or work. Different sections of a bigger project can be plotted based on their timelines and progress.

Let us plot some sample Gantt Charts.

df = pd.DataFrame([
    dict(Task="Development", Start='2012-01-20', Finish='2012-02-20'),
    dict(Task="Website Design", Start='2012-01-10', Finish='2012-01-30'),
    dict(Task="Deployment", Start='2012-02-20', Finish='2012-03-30'),
    dict(Task="Marketing", Start='2012-02-25', Finish='2012-04-15')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") 
fig.show()
Output

Let us add a few more features.

df = pd.DataFrame([
    dict(Task="Development", Start='2012-01-20', Finish='2012-02-20', Team="Team A"),
    dict(Task="Website Design", Start='2012-01-10', Finish='2012-01-30', Team="Team B"),
    dict(Task="Deployment", Start='2012-02-20', Finish='2012-03-30', Team="Team A"),
    dict(Task="Marketing", Start='2012-02-25', Finish='2012-04-15', Team="Team C")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Team")
fig.update_yaxes(autorange="reversed") 
fig.show()
Output

Now, let us add hues based on team size.

df = pd.DataFrame([
    dict(Task="Development", Start='2012-01-20', Finish='2012-02-20', Team="Team A",Team_Size=20),
    dict(Task="Website Design", Start='2012-01-10', Finish='2012-01-30', Team="Team B",Team_Size=15),
    dict(Task="Deployment", Start='2012-02-20', Finish='2012-03-30', Team="Team A",Team_Size=20),
    dict(Task="Marketing", Start='2012-02-25', Finish='2012-04-15', Team="Team C",Team_Size=32)
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task",color="Team_Size")
fig.update_yaxes(autorange="reversed") 
fig.show()
Adding hue to the Gantt chart

Box Plots Using Plotly

Box Plots are a great way to understand data distribution. They depict numerical data using quartiles.

fig = px.box(stud, y="math score")
fig.show()
Box plot

The minimum on a box plot shows the lowest data point except for some of the outliers.
The maximum shows the largest numerical data point.
The median is the middle value of the data distribution.
Then, the lower quartile is the 25 percentile, and the upper quartile is the 75 percentile.
Let us try some customized box plots.

fig = px.box(stud, x='gender',y="math score")
fig.show()
Customized Box Plot using Plotly
fig = px.box(stud, x='gender',y="math score", points="all")
fig.show()
Box Plot
fig = px.box(stud, x='gender',y="math score", color="test preparation course")
fig.show()
output

Now, let us add a notch.

fig = px.box(stud, x='gender',y="math score", color="test preparation course", notched=True)
fig.show()
output

Histograms

Histogram widgets are an excellent plot to understand the frequency distribution of numerical data.

fig = px.histogram(stud, x="math score", nbins=20, color="gender")
fig.show()
output

Let us customize it.

fig = px.histogram(stud, x="math score", nbins=20, color="gender", marginal="rug")
fig.show()
Output

Let us make a data visual to show the proper representation of data by adding a box plot as well.

fig = px.histogram(stud, x="reading score", y="math score", color="gender", marginal="box",
                   hover_data=stud.columns)
fig.show()
output

Such visuals are really great for understanding how the data is spread, and we can interact with the plots.

fig = px.histogram(stud, x="reading score", y="writing score", color="parental level of education", marginal="box",
                   hover_data=stud.columns)
fig.show()
Output

We had a look at major visualization methods in Plotly.

Code (Kaggle Notebooks):

Conclusion

Plotly Python library is an open-source module that is used for data visualization and supports various graphs like line charts, scatter plots, bar charts, histograms, area plots, etc. Plotly Python library produces interactive graphs, can be embedded on websites, and provides a wide variety of complex plotting options. The graphs and plots are robust, and a wide variety of people can use them. The interactivity also offers a number of advantages over static matplotlib plots, such as saving time when initially exploring your dataset.

Key Takeaways

  • Plotly, an open-source Python module, supports data visualization with various graphs like line charts, scatter plots, bar charts, histograms, area plots, and more.
  • Plotly’s Python graphing library makes it easy to create interactive graphs.

Frequently Asked Questions

Q1. What is the Plotly Python library?

A. The Plotly Python library is an open-source plotting library that covers statistical, financial, geographic, scientific, and 3D use cases.

Q2. Is Plotly Python interactive?

A. Plotly has several advantages. One of the main advantages of interactive plots.

Q3. How do you install the Plotly library in Python?

A. We can access this API in python using the plot.ly package. To install, open up a terminal and type $ pip install plotly or you can type $ sudo pip install Plotly.

Prateek is a dynamic professional with a strong foundation in Artificial Intelligence and Data Science, currently pursuing his PGP at Jio Institute. He holds a Bachelor's degree in Electrical Engineering and has hands-on experience as a System Engineer at TCS Digital, where he excelled in API management and data integration. Prateek also has a background in product marketing and analytics from his time with start-ups like AppleX and Milkie Way, Inc., where he was involved in growth campaigns and technical blog management. Recognized for his structured thinking and problem-solving abilities, he has received accolades like the Dr. Sudarshan Chakraborty Award for Best Student Performance. Fluent in multiple languages and passionate about technology, Prateek continues to expand his expertise in the rapidly evolving AI and tech landscape.

Responses From Readers

Clear

YesNoSpin
YesNoSpin

This is a great guide for using Plotly in Python! I'm a big fan of the platform and this guide makes it even more useful.

vallabh
vallabh

can you please also show how to store static plotly image locall?

todays tv schedule
todays tv schedule

This guide is really helpful! I'm a beginner in Python and this guide has helped me a lot.

4pics
4pics

This guide is really helpful! I'm a beginner in Python and this guide has helped me a lot.

sound of text
sound of text

This guide looks very helpful in creating interactive plots using the Plotly library in Python. It's great that you are sharing this knowledge with your readers, and I am looking forward to learning more about it!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details