Open In App

Bubble chart using Plotly in Python

Last Updated : 03 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library.

Bubble Chart

The bubble chart in Plotly is created using the scatter plot. It can be created using the scatter() method of plotly.express. A bubble chart is a data visualization which helps to displays multiple circles (bubbles) in a two-dimensional plot as same in scatter plot. A bubble chart is primarily used to depict and show relationships between numeric variables.

Example:

Python3




import plotly.express as px
  
df = px.data.iris()
  
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="species",
                 size='petal_length'
                 hover_data=['petal_width'])
  
fig.show()


Output:

Set Marker Size

Marker size and color are used to control the overall size of the marker. Marker size helps to maintain the color inside the bubble in the graph. Scatter is used to actually scale the marker sizes and color based on data.

Example:

Python3




import plotly.graph_objects 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.Figure(data=[px.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers',
    marker_size = [115, 20, 30])
])
                   
plot.show()


Output:

Scaling the Size of Bubble Charts

To scale the bubble size,use the parameter sizeref. To calculate the values of sizeref use:

sizeref = 2. * max(array of size values) / (desired maximum marker size ** 2)

Example:

Python3




import plotly.graph_objects 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)
  
size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40]
  
plot = px.Figure(data=[px.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers',
    marker=dict(
        size=size,
        sizemode='area',
        sizeref=2.*max(size)/(40.**2),
        sizemin=4
    )
)])
                   
plot.show()


Output:

Showing Color scale

The color scale is a specialized label which helps to displays a color map with its scale, the color scale is used to display a color palette and its numerical scale for color mapped.

Example:

Python3




import plotly.graph_objects 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.Figure(data=[px.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers',
     marker=dict(
       color = [10, 20, 30, 50], 
       size = [10, 30, 50, 80],
       showscale=True
     )
)])
                   
plot.show()


Output:



Previous Article
Next Article

Similar Reads

3D Bubble chart using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. 3D Bubble Chart in Plotly A bubble chart can be
2 min read
How to hide legend with Plotly Express and Plotly in Python?
In this article, we will learn How to hide legend with Plotly Express and Plotly. Here we will discuss two different methods for hiding legend in plotly and plotly express, using two different examples for each to make it more clear. Syntax: For legend: fig.update_traces(showlegend=False)fig.update(layout_showlegend=False) Example 1: In this exampl
2 min read
Plotly - How to show legend in single-trace scatterplot with plotly express?
In this article let's see how to show legend in single-trace scatterplot with plotly express. A 'trace' is the name given to each plot inside the chart. Generally in plotly legend is not visible for single trace scatter plots. Example: In the below, example packages and data are imported and a single trace scatter plot is plotted using px.scatter()
1 min read
Bar chart using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Bar Chart in Plotly Bar chart with plotly expre
2 min read
Bar chart using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library. Bar Chart In a bar chart the data categories ar
4 min read
Line Chart using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Line Chart in Plotly Line plot in Plotly is much
4 min read
Filled area chart using plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library.  Filled Area Chart Syntax Syntax: plotly.
6 min read
How to create Stacked bar chart in Python-Plotly?
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Stack bar chart A stacked bar chart or graph is
2 min read
Python - Change legend size in Plotly chart
The data on the graph's Y-axis, also known as the graph series, is reflected in the legend of the graph. This is the information that comes from the columns of the relevant grid report, and it usually consists of metrics. A graph legend is usually displayed as a box on the right or left side of your graph. Plotly's update_layout() function is used
2 min read
Gantt Chart in plotly
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Gantt Chart A Gantt chart is parallel to bar cha
2 min read