看起来您在创建 figure 后错过了绘图命令。而Data([trace1]) 不正确地寻找我的意见。因此,如果您想使用 Python3 创建一个简单的散点图,您可以通过两种方式进行(并给出相同的结果)。
1.第一种方式:
# Import all the necessaries libraries
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
# In trace you specify what kind of plot you want (such scatter, bar, pie or so)
trace = go.Scatter(
x=[1,2,3,4,5,6,7,8,9],
y=[1,2,3,4,5,6,7,8,9],
name="trace1",
mode="lines")
# Data is just the list of your traces
data = [trace]
# Create layout if you want to add title to plot, to axis, choose position etc
layout = go.Layout(
title="My first plotly chart",
xaxis=dict(title="X Values"),
yaxis=dict(title="Y Values"))
# Figure need to gather data and layout together
fig = go.Figure(data=data, layout=layout)
# This commandd plot the plot and create HTML file in your Python script directory
py.iplot(fig, filename="first plot.html")
2.第二种方式:
# Import all the necessaries libraries
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
# This code have the same result as previous, but it is more unreadable as I think
fig = {
"data": [
{
"x": [1,2,3,4],
"y": [1,2,3,4],
"name": "trace1",
"type": "scatter"
}],
"layout": {
"title":"My first plotly chart",
"xaxis": {
"title": "X Values"
},
"yaxis": {
"title": "Y Values"
}
}
}
# Just plot
py.iplot(fig, filename="first plot.html")
如果你想定制你的情节,我建议你检查plotly docs about scatter plot。
输出: