简体   繁体   English

Python Anaconda 3.6 plotly模块使用图

[英]Python Anaconda 3.6 plotly module using Figure

can't seem to have this working: 似乎无法正常工作:

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 dtates that Data in depreciated, and asks me to use something else, but I can't seem to figure it out. python贬低了折旧的数据,并要求我使用其他东西,但我似乎无法弄清楚。

Looks like you missed command to plot after creating figure . 好像您在创建figure后错过了要绘制的命令。 And Data([trace1]) looking incorrectly for my opinion. 而且Data([trace1])观点不正确。 So if you want create a simple scatter plot using Python3, you can do it in two ways (and give the same result). 因此,如果您想使用Python3创建一个简单的散点图,则可以通过两种方式来实现(并给出相同的结果)。
1.First way: 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.Second way: 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")

If you want to customize your plot, I am suggest you to check plotly docs about scatter plot . 如果您想自定义绘图,建议您查看有关散点图的绘图文档

Output: 输出: 产量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM