【问题标题】:iplot() is not working. Why ? If it need account? [duplicate]iplot() 不工作。为什么 ?如果需要账号? [复制]
【发布时间】:2018-06-21 07:35:56
【问题描述】:

我刚刚尝试了https://plot.ly/python/gauge-charts/ 的代码,但即使它不起作用。你能指导我哪里出错了吗? 我是 python 和 plotly 的新手。 这是代码:

import plotly.plotly as py

import plotly.graph_objs as go
from plotly.offline import *

base_chart = {
    "values": [40, 10, 10, 10, 10, 10, 10],
    "labels": ["-", "0", "20", "40", "60", "80", "100"],
    "domain": {"x": [0, .48]},
    "marker": {
        "colors": [
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)',
            'rgb(255, 255, 255)'
        ],
        "line": {
            "width": 1
        }
    },
    "name": "Gauge",
    "hole": .4,
    "type": "pie",
    "direction": "clockwise",
    "rotation": 108,
    "showlegend": False,
    "hoverinfo": "none",
    "textinfo": "label",
    "textposition": "outside"
}
meter_chart = {
    "values": [50, 10, 10, 10, 10, 10],
    "labels": ["Log Level", "Debug", "Info", "Warn", "Error", "Fatal"],
    "marker": {
        'colors': [
            'rgb(255, 255, 255)',
            'rgb(232,226,202)',
            'rgb(226,210,172)',
            'rgb(223,189,139)',
            'rgb(223,162,103)',
            'rgb(226,126,64)'
        ]
    },
    "domain": {"x": [0, 0.48]},
    "name": "Gauge",
    "hole": .3,
    "type": "pie",
    "direction": "clockwise",
    "rotation": 90,
    "showlegend": False,
    "textinfo": "label",
    "textposition": "inside",
    "hoverinfo": "none"
}
layout = {
    'xaxis': {
        'showticklabels': False,
        'autotick': False,
        'showgrid': False,
        'zeroline': False,
    },
    'yaxis': {
        'showticklabels': False,
        'autotick': False,
        'showgrid': False,
        'zeroline': False,
    },
    'shapes': [
        {
            'type': 'path',
            'path': 'M 0.235 0.5 L 0.24 0.65 L 0.245 0.5 Z',
            'fillcolor': 'rgba(44, 160, 101, 0.5)',
            'line': {
                'width': 0.5
            },
            'xref': 'paper',
            'yref': 'paper'
        }
    ],
    'annotations': [
        {
            'xref': 'paper',
            'yref': 'paper',
            'x': 0.23,
            'y': 0.45,
            'text': '50',
            'showarrow': False
        }
    ]
}

# we don't want the boundary now
base_chart['marker']['line']['width'] = 0

fig = {"data": [base_chart, meter_chart],
       "layout": layout}
# fig.show()

py.iplot(fig, filename='gauge-meter-chart')

错误信息是:

Aw, snap! We didn't get a username with your request.

Don't have an account? https://plot.ly/api_signup

Questions? accounts@plot.ly
Traceback (most recent call last):
  File "graph.py", line 101, in <module>
    py.iplot(fig, filename='gauge-meter-chart')
  File "C:\Users\Sam\Anaconda2\envs\py35\lib\site-packages\plotly\plotly\plotly.py", line 164, in iplot
    return tools.embed(url, **embed_options)
  File "C:\Users\Sam\Anaconda2\envs\py35\lib\site-packages\plotly\tools.py", line 396, in embed
    return PlotlyDisplay(url, width, height)
  File "C:\Users\Sam\Anaconda2\envs\py35\lib\site-packages\plotly\tools.py", line 1440, in __init__
    self.embed_code = get_embed(url, width=width, height=height)
  File "C:\Users\Sam\Anaconda2\envs\py35\lib\site-packages\plotly\tools.py", line 301, in get_embed
    "".format(url, plotly_rest_url))
plotly.exceptions.PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'.
Run help on this function for more information.

实际上我想建立一个有一些笔画的量规,它们的值写在上面。所以任何人都可以帮助我,我将不胜感激。提前致谢。

【问题讨论】:

  • 错误信息是什么?
  • 我编辑了我的问题。有错误信息请看一下。 @eyllanesc

标签: python python-3.x plotly


【解决方案1】:

您可以将 Plotly 设置为在线或离线模式下工作。 要在线工作,您需要一个用户名

要离线工作,只需将代码编辑为

import plotly
import plotly.graph_objs as go
from plotly.offline import *

...
...

plotly.offline.plot(fig, filename='gauge-meter-chart.html')

【讨论】:

  • 谢谢它的工作,我可以知道如何将它嵌入到我的 gui 中。我不需要弹出新窗口。 @Sunitha
  • 试试 plotly.offline.iplot(fig, filename='gauge-meter-chart')。
  • 该文档建议“使用 plotly.offline.plot() 创建独立的 HTML,该 HTML 保存在本地并在 Web 浏览器中打开。在 Jupyter 中离线工作时使用 plotly.offline.iplot()用于在笔记本中显示绘图的笔记本。”
猜你喜欢
  • 2017-08-24
  • 1970-01-01
  • 2014-11-13
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 2016-03-19
相关资源
最近更新 更多