本文中介绍的是利用plotly_express
绘制散点图,使用的是scatter()
方法。
With
px.scatter
, each data point is represented as a marker point, whose location is given by thex
andy
columns.
- 通过
plotly_express
库来实现 - 通过
plotly.graph_objects
实现
1 基于px的散点图
1.1 模拟数据
直接将数据传进来
1 | import plotly_express as px |
1.2 内置数据gapminder
1.3 内置数据iris
1 | df = px.data.iris() |
1.4 连续型的点图line-scatter
连续型的点图,比如:三角函数的图形、线性图形等
1 | x = np.linspace(0,10,100) # 0-10的100个数 |
2 基于go.Scatter的散点图
2.1 demo
go.Figure
确定画布go.Scatter
画图,传入需要的数据
1 | t = np.linspace(0, 10, 50) |
2.2 子图制作
在一个画布figure
中画多个图
go.figure
确定画布go.add_trace()
:将不同的图形画在一个画布上fig.show()
:显示图形
1 | np.random.seed(2) |
2.3 冒泡散点-bubble scatter
冒泡散点图:随着坐标轴数值的变化,点的大小随着变化
1 | fig = go.Figure(go.Scatter( |
1 | t = np.linspace(0, 10, 100) |
1 | t = np.linspace(0, 10, 100) |
2.4 数据悬停Data Labels on Hover
在使用go.Scatter的时候,如何实现悬停时候数据的显示
1 | df = px.data.iris() |
2.5 Scatter with a Color Dimension
指的是在图形右边实现颜色的不断变化
1 | x = np.linspace(0,10,500) |
默认的颜色
1 | x = np.linspace(0,10,500) |