在Python中使用 Plotly 的气泡图
Plotly是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 Plotly 是一个交互式可视化库。
气泡图
Plotly 中的气泡图是使用散点图创建的。它可以使用plotly.express
的scatter()方法创建。气泡图是一种数据可视化,有助于在二维图中显示多个圆圈(气泡),就像在散点图中一样。气泡图主要用于描述和显示数值变量之间的关系。
例子:
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()
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()
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()
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()
输出:
设置标记大小
标记大小和颜色用于控制标记的整体大小。标记大小有助于保持图表中气泡内的颜色。 Scatter 用于根据数据实际缩放标记大小和颜色。
例子:
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()
输出:
缩放气泡图的大小
要缩放气泡大小,请使用参数sizeref 。要计算 sizeref 的值,请使用:
sizeref = 2. * max(array of size values) / (desired maximum marker size ** 2)
例子:
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()
输出:
显示色标
色标是一个专门的标签,它有助于显示带有标尺的色标,色标用于显示调色板及其用于色标的数字标尺。
例子:
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()
输出: