Plotly 是一个交互式图表库,可以让用户在 Python 中创建丰富的图表。以下是一个简单的教程,帮助您入门使用 Plotly。

安装 Plotly

首先,您需要在您的 Python 环境中安装 Plotly。您可以使用 pip 命令来安装:

pip install plotly

创建基础图表

以下是一个创建散点图的简单示例:

import plotly.graph_objs as go
import plotly.offline as offline

# 创建数据
x = [1, 2, 3, 4, 5]
y = [10, 11, 12, 13, 14]

# 创建散点图
trace = go.Scatter(x=x, y=y, mode='markers')

# 创建图表
fig = go.Figure(data=[trace])

# 显示图表
offline.plot(fig, filename='scatter.html')

交互式图表

Plotly 的一个强大之处在于其交互性。以下是一个创建交互式图表的示例:

# 创建交互式图表
fig = go.Figure(data=[
    go.Scatter(
        x=[1, 2, 3, 4, 5],
        y=[10, 11, 12, 13, 14],
        mode='markers',
        name='Scatter'
    ),
    go.Scatter(
        x=[1, 2, 3, 4, 5],
        y=[5, 6, 7, 8, 9],
        mode='lines',
        name='Line'
    )
])

fig.update_layout(title='Interactive Plotly Chart', xaxis_title='X Axis', yaxis_title='Y Axis')

offline.plot(fig, filename='interactive.html')

更多资源

如果您想要了解更多关于 Plotly 的信息,请访问我们的官方文档

返回首页