Today we're going to talk about how to use the Python plotting tool, Plotly, to plot bubbles.
Bubble charts are implemented in a similar way to scatter plots. Modify the size of the points in the scatter plot and it becomes a bubble plot.
The realization code is as follows:
import plotly as py import plotly.graph_objs as go pyplt = trace0 = ( x=[1, 2, 3, 4, 5, 6, 7], y=[8, 10, 12, 14, 16, 18, 20], mode='markers', marker=dict( size=[10, 14, 16, 18, 20, 42, 64], #Set the bubble size ) ) data = [trace0] pyplt(data, filename='tmp/')
You will get the bubble map as shown below:
The following example explains how to set the size and color of the bubble dots, as well as the hint text and whether or not to display the color bar.
The code is as follows:
import plotly as py import plotly.graph_objs as go pyplt = trace0 = ( x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='markers', text=['1st bubble<br>size: 40<br> here you can fill in the content', '2nd bubble <br>size: 60', '3rd bubble <br>size: 80', '4th bubble <br>size: 100'], marker=dict( color= [120, 125, 130, 135], opacity=[1, 0.8, 0.6, 0.4], size=[40, 60, 80, 100], showscale= True, ) ) data = [trace0] pyplt(data, filename='tmp/')
Run the program to get the following figure:
Explain.
text can be specified for each point, corresponding to the hover window text information (<br> that is, line feed)
color specifies the color of each point, and opacity specifies the amount of transparency of the point.
size Specifies the size of each point.
showscale = True indicates that the right color bar is displayed.
Next, we'll explain how to zoom in and out of the bubble map.
Adjustment of the size is set by setting the parameter sizeref. When this parameter is greater than 1, the size of the bubble will be reduced.
When this parameter is less than 1, it will increase the size of the bubble.
import plotly as py import plotly.graph_objs as go pyplt = trace0 = ( x=[1, 2, 3, 4], y=[10, 11, 12, 13], text=['A</br>size: 40</br>default', 'B</br>size: 60</br>default', 'C</br>size: 80</br>default', 'D</br>size: 100</br>default'], mode='markers', name='default', marker=dict( size=[400, 600, 800, 1000], sizemode='area', ) ) trace1 = ( x=[1, 2, 3, 4], y=[14, 15, 16, 17], text=['A</br>size: 40</br>sizeref: 0.2', 'B</br>size: 60</br>sizeref: 0.2', 'C</br>size: 80</br>sizeref: 0.2', 'D</br>size: 100</br>sizeref: 0.2'], mode='markers', name = 'ref0.2', marker=dict( size=[400, 600, 800, 1000], sizeref=0.2, sizemode='area', ) ) trace2 = ( x=[1, 2, 3, 4], y=[20, 21, 22, 23], text=['A</br>size: 40</br>sizeref: 2', 'B</br>size: 60</br>sizeref: 2', 'C</br>size: 80</br>sizeref: 2', 'D</br>size: 100</br>sizeref: 2'], mode='markers', name='ref2', marker=dict( size=[400, 600, 800, 1000], sizeref=2, sizemode='area', ) ) data = [trace0, trace1, trace2] pyplt(data, filename='tmp/')
The following figure is obtained:
Parameter sizeref=2 Sets the size of the bubble to 1/2 of its original size.
The parameter sizemodes has two values and the value area.
The 1 value is scaled by diameter and the 2 value is scaled by representation area.
This is the whole content of this article.