Python Anaconda 3.6 plotly模块使用图
Python Anaconda 3.6 plotly module using Figure
似乎无法正常工作:
import plotly.offline as pyo
from plotly.graph_objs import *
import plotly.plotly as py
import pandas as pd
from pandas import DataFrame
pyo.offline.init_notebook_mode()
trace1 = {'type' : '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'}
layout = {'title' : 'my first plotly chart',
'xaxis' : {'X Values'},
'yaxis' : {'Y Values'}
}
data = Data([trace1])
fig = Figure(data = data, layout = layout)
python 指出数据已折旧,并要求我使用其他东西,但我似乎无法弄清楚。
看起来您在创建 figure
后错过了绘图命令。 Data([trace1])
错误地寻找我的意见。因此,如果您想使用 Python3 创建一个简单的散点图,您可以通过两种方式进行(并给出相同的结果)。
1.First方式:
# 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.Second方式:
# 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。
输出:
似乎无法正常工作:
import plotly.offline as pyo
from plotly.graph_objs import *
import plotly.plotly as py
import pandas as pd
from pandas import DataFrame
pyo.offline.init_notebook_mode()
trace1 = {'type' : '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'}
layout = {'title' : 'my first plotly chart',
'xaxis' : {'X Values'},
'yaxis' : {'Y Values'}
}
data = Data([trace1])
fig = Figure(data = data, layout = layout)
python 指出数据已折旧,并要求我使用其他东西,但我似乎无法弄清楚。
看起来您在创建 figure
后错过了绘图命令。 Data([trace1])
错误地寻找我的意见。因此,如果您想使用 Python3 创建一个简单的散点图,您可以通过两种方式进行(并给出相同的结果)。
1.First方式:
# 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.Second方式:
# 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。
输出: